Skip to content

Commit

Permalink
add range js helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed May 19, 2022
1 parent 7a4c2ad commit 772efda
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
3 changes: 2 additions & 1 deletion scripts/generate/templates/SafeCast.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const assert = require('assert');
const format = require('../format-lines');
const { range } = require('../../helpers');

const LENGTHS = Array(31).fill().map((_, i) => (i + 1) * 8).reverse(); // 248 → 8 (in steps of 8)
const LENGTHS = range(8, 256, 8).reverse(); // 248 → 8 (in steps of 8)

// Returns the version of OpenZeppelin Contracts in which a particular function was introduced.
// This is used in the docs for each function.
Expand Down
5 changes: 3 additions & 2 deletions scripts/generate/templates/SafeCastMock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const format = require('../format-lines');
const { range } = require('../../helpers');

const LENGTHS = range(8, 256, 8).reverse(); // 248 → 8 (in steps of 8)

const header = `\
pragma solidity ^0.8.0;
Expand Down Expand Up @@ -31,8 +34,6 @@ function toUint${length}(uint256 a) public pure returns (uint${length}) {
`;

// GENERATE
const LENGTHS = Array(31).fill().map((_, i) => (i + 1) * 8).reverse(); // 248 → 8 (in steps of 8)

module.exports = format(
header,
'contract SafeCastMock {',
Expand Down
23 changes: 23 additions & 0 deletions scripts/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function chunk (array, size = 1) {
return Array.range(Math.ceil(array.length / size)).map(i => array.slice(i * size, i * size + size))
}

function range (start, stop = undefined, step = 1) {
if (!stop) { stop = start; start = 0; }
return start < stop ? Array(Math.ceil((stop - start) / step)).fill().map((_, i) => start + i * step) : [];
}

function unique (array, op = x => x) {
return array.filter((obj, i) => array.findIndex(entry => op(obj) === op(entry)) === i);
}

function zip (...args) {
return Array(Math.max(...args.map(arg => arg.length))).fill(null).map((_, i) => args.map(arg => arg[i]));
}

module.exports = {
chunk,
range,
unique,
zip,
}
6 changes: 3 additions & 3 deletions test/utils/math/SafeCast.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { BN, expectRevert } = require('@openzeppelin/test-helpers');

const { expect } = require('chai');
const { range } = require('../../../scripts/helpers');

const SafeCastMock = artifacts.require('SafeCastMock');

Expand Down Expand Up @@ -41,7 +41,7 @@ contract('SafeCast', async (accounts) => {
});
}

Array(31).fill().map((_, i) => (i + 1) * 8).forEach(bits => testToUint(bits));
range(8, 256, 8).forEach(bits => testToUint(bits));

describe('toUint256', () => {
const maxInt256 = new BN('2').pow(new BN(255)).subn(1);
Expand Down Expand Up @@ -129,7 +129,7 @@ contract('SafeCast', async (accounts) => {
});
}

Array(31).fill().map((_, i) => (i + 1) * 8).forEach(bits => testToInt(bits));
range(8, 256, 8).forEach(bits => testToInt(bits));

describe('toInt256', () => {
const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
Expand Down

0 comments on commit 772efda

Please sign in to comment.