-
Notifications
You must be signed in to change notification settings - Fork 220
/
index.ts
123 lines (117 loc) · 2.69 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* eslint-disable @typescript-eslint/no-explicit-any */
// ascending range
function ascR(b: number, n: number) {
const a = [];
// tslint:disable-next-line:curly
for (; n--; a[n] = n + b);
return a;
}
// descending range
function descR(b: number, n: number) {
const a = [];
// tslint:disable-next-line:curly
for (; n--; a[n] = b - n);
return a;
}
/**
* 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]
*/
export function range(from: number, to: number): number[] {
return from < to ? ascR(from, to - from + 1) : descR(from, from - to + 1);
}
/**
* 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]
*/
export function rotate<T>(times: number, arr: T[]): T[] {
const len = arr.length;
const n = ((times % len) + len) % len;
return arr.slice(n, len).concat(arr.slice(0, n));
}
/**
* 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"]
*/
export function compact(arr: any[]): any[] {
return arr.filter((n) => n === 0 || n);
}
/**
* 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"]) // => [...]
*/
export function shuffle(arr: any[], rnd = Math.random): any[] {
let i: number;
let t: any;
let m: number = arr.length;
while (m) {
i = Math.floor(rnd() * m--);
t = arr[m];
arr[m] = arr[i];
arr[i] = t;
}
return arr;
}
/**
* 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"]
* ]
*/
export function permutations(arr: any[]): any[] {
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;
}),
);
}, []);
}
/** @deprecated */
export default {
compact,
permutations,
range,
rotate,
shuffle,
};