UNPKG

@tonaljs/array

Version:

Functions to work with arrays of tonal objects

66 lines 1.5 kB
// index.ts import { note } from "@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) => 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; }) ); }, []); } export { compact, permutations, range, rotate, shuffle, sortedNoteNames, sortedUniqNoteNames }; //# sourceMappingURL=index.mjs.map