UNPKG

@tonaljs/array

Version:

Functions to work with arrays of tonal objects

92 lines (90 loc) 2.48 kB
/** * Creates a numeric range * * @param {number} from * @param {number} to * @return {Array<number>} * * @example * range(-2, 2) // => [-2, -1, 0, 1, 2] * range(2, -2) // => [2, 1, 0, -1, -2] */ declare function range(from: number, to: number): number[]; /** * Rotates a list a number of times. It's completely agnostic about the * contents of the list. * * @param {Integer} times - the number of rotations * @param {Array} array * @return {Array} the rotated array * * @example * rotate(1, [1, 2, 3]) // => [2, 3, 1] */ declare function rotate<T>(times: number, arr: T[]): T[]; /** * Return a copy of the array with the null values removed * @function * @param {Array} array * @return {Array} * * @example * compact(["a", "b", null, "c"]) // => ["a", "b", "c"] */ declare function compact(arr: any[]): any[]; /** * Sort an array of notes in ascending order. Pitch classes are listed * before notes. Any string that is not a note is removed. * * @param {string[]} notes * @return {string[]} sorted array of notes * * @example * sortedNoteNames(['c2', 'c5', 'c1', 'c0', 'c6', 'c']) * // => ['C', 'C0', 'C1', 'C2', 'C5', 'C6'] * sortedNoteNames(['c', 'F', 'G', 'a', 'b', 'h', 'J']) * // => ['C', 'F', 'G', 'A', 'B'] */ declare function sortedNoteNames(notes: string[]): string[]; /** * Get sorted notes with duplicates removed. Pitch classes are listed * before notes. * * @function * @param {string[]} array * @return {string[]} unique sorted notes * * @example * Array.sortedUniqNoteNames(['a', 'b', 'c2', '1p', 'p2', 'c2', 'b', 'c', 'c3' ]) * // => [ 'C', 'A', 'B', 'C2', 'C3' ] */ declare function sortedUniqNoteNames(arr: string[]): string[]; /** * Randomizes the order of the specified array in-place, using the Fisher–Yates shuffle. * * @function * @param {Array} array * @return {Array} the array shuffled * * @example * shuffle(["C", "D", "E", "F"]) // => [...] */ declare function shuffle(arr: any[], rnd?: () => number): any[]; /** * Get all permutations of an array * * @param {Array} array - the array * @return {Array<Array>} an array with all the permutations * @example * permutations(["a", "b", "c"])) // => * [ * ["a", "b", "c"], * ["b", "a", "c"], * ["b", "c", "a"], * ["a", "c", "b"], * ["c", "a", "b"], * ["c", "b", "a"] * ] */ declare function permutations(arr: any[]): any[]; export { compact, permutations, range, rotate, shuffle, sortedNoteNames, sortedUniqNoteNames };