-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy patharray.ts
More file actions
37 lines (35 loc) · 898 Bytes
/
array.ts
File metadata and controls
37 lines (35 loc) · 898 Bytes
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
/**
* Array random slice with items count
*/
export function randomSlice<T = any>(arr: T[], num?: number): T[] {
if (!num || num >= arr.length) {
return arr.slice();
}
const index = Math.floor(Math.random() * arr.length);
const a: T[] = [];
for (let i = 0, j = index; i < num; i++) {
a.push(arr[j++]);
if (j === arr.length) {
j = 0;
}
}
return a;
}
/**
* Remove one exists element from an array
* @param {Array} arr input array
* @param {Number} index - remove element index
* @return {Array} the array instance
*/
export function spliceOne<T = any>(arr: T[], index: number): T[] {
const idx = index < 0 ? arr.length + index : index;
// Not found or out of bounds
if (idx < 0 || idx >= arr.length) {
return arr;
}
for (let i = idx, k = i + 1, n = arr.length; k < n; i += 1, k += 1) {
arr[i] = arr[k];
}
arr.pop();
return arr;
}