-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharr.ts
42 lines (36 loc) · 1.33 KB
/
arr.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
/**
* Array utilities.
*/
import '#@initialize.ts';
import { type $type } from '#index.ts';
/**
* Randomly shuffles an array.
*
* By subtracting 0.5, the result of Math.random() will be in the range of -0.5 to 0.5. This means roughly half the
* time, the comparison function will return a negative value (indicating that the first element should come before the
* second), and the other half of the time, it will return a positive value (indicating the reverse).
*
* @param arr Array to shuffle.
*
* @returns Shallow clone; shuffled randomly.
*/
export const shuffle = <Type extends unknown[]>(arr: Type): Type => {
return [...arr].sort(() => Math.random() - 0.5) as Type;
};
/**
* Finds a sequence index in an array.
*
* @param arr Array to search.
* @param sequence Sequence to locate.
*
* @returns Index position of sequence, else `-1`.
*/
export const indexOfSequence = (arr: unknown[] | $type.TypedArray, sequence: unknown[] | $type.TypedArray): number => {
if (sequence.length > arr.length) return -1;
// Improves performance by calculating max possible index.
const maxPossibleIndex = arr.length - sequence.length;
return arr.findIndex((unusedꓺvalue, index) => {
if (index > maxPossibleIndex) return false;
return sequence.every((v, i) => arr[index + i] === v);
});
};