forked from determined-ai/shared-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototypes.ts
70 lines (60 loc) · 1.74 KB
/
prototypes.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
export {};
/*
* Generally adding to top level JS prototypes is a bad idea,
* due to potentially polluting top level namespaces.
* However, Array is an exception because it is unlikely to
* happen and provides a lot of value.
*/
/*
* Note: When requesting an item from an Array with an
* out of range index, JS will return `undefined`.
*/
Array.prototype.first = function() {
return this[0];
};
Array.prototype.last = function() {
return this[this.length - 1];
};
Array.prototype.random = function() {
const index = Math.floor((Math.random() * this.length));
return this[index];
};
function swap<T>(arr: T[], i: number, j: number): T[] {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
function partition<T>(arr: T[], low: number, high: number, compareFn: (a: T, b: T) => number) {
let q = low;
let i;
for (i = low; i < high; i++) {
if (compareFn(arr[i], arr[high]) === -1) {
swap(arr, i, q);
q += 1;
}
}
swap(arr, i, q);
return q;
}
function quickSort<T>(arr: T[], low: number, high: number, compareFn: (a: T, b: T) => number) {
if (low < high) {
const pivot = partition(arr, low, high, compareFn);
quickSort(arr, low, pivot - 1, compareFn);
quickSort(arr, pivot + 1, high, compareFn);
return arr;
}
return [];
}
/*
* Native Array.prototype.sort ignores `undefined` values inside the array,
* so they always end up at the end of the list regardless of the sorting function.
* Array.prototype.sortAll will sort `undefined` also and treat them the same as
* how `null` values are treated.
*/
Array.prototype.sortAll = function(compareFn) {
return quickSort(this, 0, this.length - 1, compareFn);
};
Storage.prototype.keys = function() {
return Object.keys(this);
};