Deduplicate code in scripts/helpers and test/helpers/iterate (#4895)

Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
Hadrien Croubois
2024-02-16 00:40:48 +01:00
committed by GitHub
parent 4e7e6e54da
commit 96e5c0830a
5 changed files with 44 additions and 45 deletions

View File

@ -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,
};