UNPKG

@tonaljs/array

Version:

Functions to work with arrays of tonal objects

97 lines (96 loc) 2.74 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 array_exports = {}; __export(array_exports, { compact: () => compact, permutations: () => permutations, range: () => range, rotate: () => rotate, shuffle: () => shuffle, sortedNoteNames: () => sortedNoteNames, sortedUniqNoteNames: () => sortedUniqNoteNames }); module.exports = __toCommonJS(array_exports); var import_pitch_note = require("@tonaljs/pitch-note"); 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 sortedNoteNames(notes) { const valid = notes.map((n) => (0, import_pitch_note.note)(n)).filter((n) => !n.empty); return valid.sort((a, b) => a.height - b.height).map((n) => n.name); } function sortedUniqNoteNames(arr) { return sortedNoteNames(arr).filter((n, i, a) => i === 0 || n !== a[i - 1]); } 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; }) ); }, []); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { compact, permutations, range, rotate, shuffle, sortedNoteNames, sortedUniqNoteNames }); //# sourceMappingURL=index.js.map