UNPKG

@tonaljs/collection

Version:

Utility functions to work with collections (arrays)

64 lines 1.24 kB
// index.ts function ascR(b, n) { const a = []; for (; n--; a[n] = n + b) ; return a; } function descR(b, n) { const a = []; for (; n--; a[n] = b - n) ; return a; } function range(from, to) { return from < to ? ascR(from, to - from + 1) : descR(from, from - to + 1); } function rotate(times, arr) { const len = arr.length; const n = (times % len + len) % len; return arr.slice(n, len).concat(arr.slice(0, n)); } function compact(arr) { return arr.filter((n) => n === 0 || n); } function shuffle(arr, rnd = Math.random) { let i; let t; let m = arr.length; while (m) { i = Math.floor(rnd() * m--); t = arr[m]; arr[m] = arr[i]; arr[i] = t; } return arr; } function permutations(arr) { if (arr.length === 0) { return [[]]; } return permutations(arr.slice(1)).reduce((acc, perm) => { return acc.concat( arr.map((e, pos) => { const newPerm = perm.slice(); newPerm.splice(pos, 0, arr[0]); return newPerm; }) ); }, []); } var collection_default = { compact, permutations, range, rotate, shuffle }; export { compact, collection_default as default, permutations, range, rotate, shuffle }; //# sourceMappingURL=index.mjs.map