Deduplicate code in scripts/helpers and test/helpers/iterate (#4895)
Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
@ -1,31 +1,10 @@
|
||||
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.from({ length: Math.ceil((stop - start) / step) }, (_, 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.from({ length: Math.max(...args.map(arg => arg.length)) }, (_, i) => args.map(arg => arg[i]));
|
||||
}
|
||||
|
||||
function capitalize(str) {
|
||||
return str.charAt(0).toUpperCase() + str.slice(1);
|
||||
}
|
||||
const iterate = require('../test/helpers/iterate');
|
||||
|
||||
module.exports = {
|
||||
chunk,
|
||||
range,
|
||||
unique,
|
||||
zip,
|
||||
capitalize,
|
||||
// Capitalize the first char of a string
|
||||
// Example: capitalize('uint256') → 'Uint256'
|
||||
capitalize: str => str.charAt(0).toUpperCase() + str.slice(1),
|
||||
|
||||
// Iterate tools for the test helpers
|
||||
...iterate,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user