@tonaljs/collection
Version:
Utility functions to work with collections (arrays)
73 lines (71 loc) • 1.86 kB
text/typescript
/**
* 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 completly agnostic about the
* contents of the list.
*
* @param {Integer} times - the number of rotations
* @param {Array} collection
* @return {Array} the rotated collection
*
* @example
* rotate(1, [1, 2, 3]) // => [2, 3, 1]
*/
declare function rotate<T>(times: number, arr: T[]): T[];
/**
* Return a copy of the collection with the null values removed
* @function
* @param {Array} collection
* @return {Array}
*
* @example
* compact(["a", "b", null, "c"]) // => ["a", "b", "c"]
*/
declare function compact(arr: any[]): any[];
/**
* Randomizes the order of the specified collection in-place, using the Fisher–Yates shuffle.
*
* @function
* @param {Array} collection
* @return {Array} the collection shuffled
*
* @example
* shuffle(["C", "D", "E", "F"]) // => [...]
*/
declare function shuffle(arr: any[], rnd?: () => number): any[];
/**
* Get all permutations of an collection
*
* @param {Array} collection - the collection
* @return {Array<Array>} an collection 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[];
/** @deprecated */
declare const _default: {
compact: typeof compact;
permutations: typeof permutations;
range: typeof range;
rotate: typeof rotate;
shuffle: typeof shuffle;
};
export { compact, _default as default, permutations, range, rotate, shuffle };