UNPKG

@tonaljs/collection

Version:

Utility functions to work with collections (arrays)

93 lines (92 loc) 2.37 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // index.ts var collection_exports = {}; __export(collection_exports, { compact: () => compact, default: () => collection_default, permutations: () => permutations, range: () => range, rotate: () => rotate, shuffle: () => shuffle }); module.exports = __toCommonJS(collection_exports); 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 }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { compact, permutations, range, rotate, shuffle }); //# sourceMappingURL=index.js.map