atomic-fns 0.3.5 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -12
- package/{core → dist}/collections/BTree.d.ts +6 -6
- package/{core → dist}/collections/BTree.js +17 -17
- package/{core → dist}/collections/Heap.d.ts +3 -3
- package/{core → dist}/collections/Heap.js +0 -0
- package/dist/collections/LRUCache.d.ts +80 -0
- package/dist/collections/LRUCache.js +126 -0
- package/{core → dist}/collections/SortedMap.d.ts +2 -2
- package/{core → dist}/collections/SortedMap.js +0 -0
- package/{core → dist}/collections/SortedSet.d.ts +3 -3
- package/{core → dist}/collections/SortedSet.js +1 -1
- package/{core → dist}/collections/SortedTree.d.ts +3 -3
- package/{core → dist}/collections/SortedTree.js +1 -1
- package/{core → dist}/collections/SplayTree.d.ts +5 -5
- package/{core → dist}/collections/SplayTree.js +0 -0
- package/{core → dist}/collections/abc.d.ts +0 -0
- package/{core → dist}/collections/abc.js +0 -0
- package/{core → dist}/collections/deque.d.ts +7 -7
- package/{core → dist}/collections/deque.js +6 -8
- package/{core → dist}/collections/frozenset.d.ts +0 -0
- package/{core → dist}/collections/frozenset.js +3 -6
- package/{core → dist}/collections/index.d.ts +207 -105
- package/{core → dist}/collections/index.js +179 -436
- package/{core → dist}/functools/index.d.ts +30 -16
- package/{core → dist}/functools/index.js +34 -12
- package/dist/globals/decimal.d.ts +142 -0
- package/dist/globals/decimal.js +371 -0
- package/{core → dist}/globals/index.d.ts +26 -25
- package/{core → dist}/globals/index.js +16 -37
- package/dist/i18n/currency.d.ts +24 -0
- package/dist/i18n/currency.js +85 -0
- package/dist/i18n/date/index.d.ts +204 -0
- package/dist/i18n/date/index.js +530 -0
- package/dist/i18n/date/utils.d.ts +65 -0
- package/dist/i18n/date/utils.js +290 -0
- package/dist/i18n/duration.d.ts +112 -0
- package/dist/i18n/duration.js +392 -0
- package/dist/i18n/format.d.ts +26 -0
- package/dist/i18n/format.js +495 -0
- package/{core → dist}/i18n/index.d.ts +2 -1
- package/{core → dist}/i18n/index.js +2 -1
- package/{core → dist}/index.d.ts +0 -0
- package/{core → dist}/index.js +0 -0
- package/{core → dist}/itertools/index.d.ts +74 -46
- package/{core → dist}/itertools/index.js +28 -56
- package/{core → dist}/math/index.d.ts +9 -10
- package/{core → dist}/math/index.js +3 -1
- package/{core → dist}/operators/index.d.ts +4 -4
- package/{core → dist}/operators/index.js +2 -8
- package/{core → dist}/random/index.d.ts +1 -1
- package/{core → dist}/random/index.js +5 -5
- package/package.json +28 -42
- package/.editorconfig +0 -12
- package/.esdoc.json +0 -27
- package/.eslintignore +0 -1
- package/.eslintrc.json +0 -35
- package/.firebaserc +0 -5
- package/.prettierrc +0 -19
- package/core/collections/BTree.ts +0 -1340
- package/core/collections/Heap.ts +0 -189
- package/core/collections/LRUCache.d.ts +0 -152
- package/core/collections/LRUCache.js +0 -409
- package/core/collections/LRUCache.ts +0 -486
- package/core/collections/SortedMap.ts +0 -146
- package/core/collections/SortedSet.ts +0 -110
- package/core/collections/SortedTree.ts +0 -160
- package/core/collections/SplayTree.ts +0 -354
- package/core/collections/abc.ts +0 -176
- package/core/collections/deque.ts +0 -338
- package/core/collections/frozenset.ts +0 -37
- package/core/collections/index.ts +0 -1024
- package/core/collections/test.html +0 -10
- package/core/collections/typedArrays.d.ts +0 -31
- package/core/collections/typedArrays.js +0 -143
- package/core/collections/typedArrays.ts +0 -163
- package/core/functools/index.ts +0 -260
- package/core/globals/index.ts +0 -481
- package/core/i18n/date/format.d.ts +0 -16
- package/core/i18n/date/format.js +0 -307
- package/core/i18n/date/format.ts +0 -343
- package/core/i18n/date/index.d.ts +0 -224
- package/core/i18n/date/index.js +0 -387
- package/core/i18n/date/index.ts +0 -471
- package/core/i18n/date/test.html +0 -67
- package/core/i18n/index.ts +0 -8
- package/core/index.ts +0 -8
- package/core/itertools/index.ts +0 -494
- package/core/math/index.ts +0 -152
- package/core/operators/index.ts +0 -310
- package/core/random/index.ts +0 -66
- package/firebase.json +0 -10
- package/jasmine.json +0 -14
- package/tests/BTree.spec.ts +0 -143
- package/tests/Cache.spec.ts +0 -161
- package/tests/Heap.spec.ts +0 -66
- package/tests/SortedMap.spec.ts +0 -144
- package/tests/SortedSet.spec.ts +0 -84
- package/tests/SortedTree.spec.ts +0 -164
- package/tests/SplayTree.spec.ts +0 -121
- package/tests/collections.spec.ts +0 -288
- package/tests/deque.spec.ts +0 -107
- package/tests/globals.spec.ts +0 -270
- package/tests/itertools.spec.ts +0 -266
- package/tests/math.spec.ts +0 -25
- package/tests/operators.spec.ts +0 -40
- package/tests/random.spec.ts +0 -46
- package/tsconfig.json +0 -126
- package/tsconfig.node.json +0 -18
- package/tsconfig.xo.json +0 -9
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export declare function getPointerArray(size: any): Uint8ArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor;
|
|
2
|
-
export declare function getSignedPointerArray(size: any): Int8ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor | Float64ArrayConstructor;
|
|
3
|
-
/**
|
|
4
|
-
* Function returning the minimal type able to represent the given number.
|
|
5
|
-
*
|
|
6
|
-
* @param {number} value - Value to test.
|
|
7
|
-
* @return {TypedArrayClass}
|
|
8
|
-
*/
|
|
9
|
-
export declare function getNumberType(value: any): Uint8ArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor | Int8ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor | Float64ArrayConstructor;
|
|
10
|
-
export declare function getMinimalRepresentation(array: any, getter: any): null;
|
|
11
|
-
/**
|
|
12
|
-
* Function returning whether the given value is a typed array.
|
|
13
|
-
*
|
|
14
|
-
* @param {any} value - Value to test.
|
|
15
|
-
* @return {boolean}
|
|
16
|
-
*/
|
|
17
|
-
export declare function isTypedArray(value: any): boolean;
|
|
18
|
-
/**
|
|
19
|
-
* Function used to concat byte arrays.
|
|
20
|
-
*
|
|
21
|
-
* @param {...ByteArray}
|
|
22
|
-
* @return {ByteArray}
|
|
23
|
-
*/
|
|
24
|
-
export declare function concat(): any;
|
|
25
|
-
/**
|
|
26
|
-
* Function used to initialize a byte array of indices.
|
|
27
|
-
*
|
|
28
|
-
* @param {number} length - Length of target.
|
|
29
|
-
* @return {ByteArray}
|
|
30
|
-
*/
|
|
31
|
-
export declare function indices(length: any): any;
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* When using an unsigned integer array to store pointers, one might want to
|
|
3
|
-
* choose the optimal word size in regards to the actual numbers of pointers
|
|
4
|
-
* to store.
|
|
5
|
-
*
|
|
6
|
-
* This helpers does just that.
|
|
7
|
-
*
|
|
8
|
-
* @param {number} size - Expected size of the array to map.
|
|
9
|
-
* @return {TypedArray}
|
|
10
|
-
*/
|
|
11
|
-
const MAX_8BIT_INTEGER = Math.pow(2, 8) - 1;
|
|
12
|
-
const MAX_16BIT_INTEGER = Math.pow(2, 16) - 1;
|
|
13
|
-
const MAX_32BIT_INTEGER = Math.pow(2, 32) - 1;
|
|
14
|
-
const MAX_SIGNED_8BIT_INTEGER = Math.pow(2, 7) - 1;
|
|
15
|
-
const MAX_SIGNED_16BIT_INTEGER = Math.pow(2, 15) - 1;
|
|
16
|
-
const MAX_SIGNED_32BIT_INTEGER = Math.pow(2, 31) - 1;
|
|
17
|
-
export function getPointerArray(size) {
|
|
18
|
-
const maxIndex = size - 1;
|
|
19
|
-
if (maxIndex <= MAX_8BIT_INTEGER)
|
|
20
|
-
return Uint8Array;
|
|
21
|
-
if (maxIndex <= MAX_16BIT_INTEGER)
|
|
22
|
-
return Uint16Array;
|
|
23
|
-
// if (maxIndex <= MAX_32BIT_INTEGER) return Uint32Array
|
|
24
|
-
return Uint32Array;
|
|
25
|
-
}
|
|
26
|
-
export function getSignedPointerArray(size) {
|
|
27
|
-
const maxIndex = size - 1;
|
|
28
|
-
if (maxIndex <= MAX_SIGNED_8BIT_INTEGER)
|
|
29
|
-
return Int8Array;
|
|
30
|
-
if (maxIndex <= MAX_SIGNED_16BIT_INTEGER)
|
|
31
|
-
return Int16Array;
|
|
32
|
-
if (maxIndex <= MAX_SIGNED_32BIT_INTEGER)
|
|
33
|
-
return Int32Array;
|
|
34
|
-
return Float64Array;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Function returning the minimal type able to represent the given number.
|
|
38
|
-
*
|
|
39
|
-
* @param {number} value - Value to test.
|
|
40
|
-
* @return {TypedArrayClass}
|
|
41
|
-
*/
|
|
42
|
-
export function getNumberType(value) {
|
|
43
|
-
// <= 32 bits itnteger?
|
|
44
|
-
if (value === (value | 0)) {
|
|
45
|
-
// Negative
|
|
46
|
-
if (Math.sign(value) === -1) {
|
|
47
|
-
if (value <= 127 && value >= -128)
|
|
48
|
-
return Int8Array;
|
|
49
|
-
if (value <= 32767 && value >= -32768)
|
|
50
|
-
return Int16Array;
|
|
51
|
-
return Int32Array;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
if (value <= 255)
|
|
55
|
-
return Uint8Array;
|
|
56
|
-
if (value <= 65535)
|
|
57
|
-
return Uint16Array;
|
|
58
|
-
return Uint32Array;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
// 53 bits integer & floats
|
|
62
|
-
// NOTE: it's kinda hard to tell whether we could use 32bits or not...
|
|
63
|
-
return Float64Array;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Function returning the minimal type able to represent the given array
|
|
67
|
-
* of JavaScript numbers.
|
|
68
|
-
*
|
|
69
|
-
* @param {array} array - Array to represent.
|
|
70
|
-
* @param {function} getter - Optional getter.
|
|
71
|
-
* @return {TypedArrayClass}
|
|
72
|
-
*/
|
|
73
|
-
const TYPE_PRIORITY = {
|
|
74
|
-
Uint8Array: 1,
|
|
75
|
-
Int8Array: 2,
|
|
76
|
-
Uint16Array: 3,
|
|
77
|
-
Int16Array: 4,
|
|
78
|
-
Uint32Array: 5,
|
|
79
|
-
Int32Array: 6,
|
|
80
|
-
Float32Array: 7,
|
|
81
|
-
Float64Array: 8
|
|
82
|
-
};
|
|
83
|
-
export function getMinimalRepresentation(array, getter) {
|
|
84
|
-
let maxType = null;
|
|
85
|
-
let maxPriority = 0;
|
|
86
|
-
let p;
|
|
87
|
-
let t;
|
|
88
|
-
let v;
|
|
89
|
-
let i;
|
|
90
|
-
let l;
|
|
91
|
-
for (i = 0, l = array.length; i < l; i++) {
|
|
92
|
-
v = getter ? getter(array[i]) : array[i];
|
|
93
|
-
t = exports.getNumberType(v);
|
|
94
|
-
p = TYPE_PRIORITY[t.name];
|
|
95
|
-
if (p > maxPriority) {
|
|
96
|
-
maxPriority = p;
|
|
97
|
-
maxType = t;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return maxType;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Function returning whether the given value is a typed array.
|
|
104
|
-
*
|
|
105
|
-
* @param {any} value - Value to test.
|
|
106
|
-
* @return {boolean}
|
|
107
|
-
*/
|
|
108
|
-
export function isTypedArray(value) {
|
|
109
|
-
return ArrayBuffer.isView(value);
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Function used to concat byte arrays.
|
|
113
|
-
*
|
|
114
|
-
* @param {...ByteArray}
|
|
115
|
-
* @return {ByteArray}
|
|
116
|
-
*/
|
|
117
|
-
export function concat() {
|
|
118
|
-
let length = 0;
|
|
119
|
-
let i;
|
|
120
|
-
let o;
|
|
121
|
-
let l;
|
|
122
|
-
for (i = 0, l = arguments.length; i < l; i++)
|
|
123
|
-
length += arguments[i].length;
|
|
124
|
-
const array = new arguments[0].constructor(length);
|
|
125
|
-
for (i = 0, o = 0; i < l; i++) {
|
|
126
|
-
array.set(arguments[i], o);
|
|
127
|
-
o += arguments[i].length;
|
|
128
|
-
}
|
|
129
|
-
return array;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Function used to initialize a byte array of indices.
|
|
133
|
-
*
|
|
134
|
-
* @param {number} length - Length of target.
|
|
135
|
-
* @return {ByteArray}
|
|
136
|
-
*/
|
|
137
|
-
export function indices(length) {
|
|
138
|
-
const PointerArray = exports.getPointerArray(length);
|
|
139
|
-
const array = new PointerArray(length);
|
|
140
|
-
for (let i = 0; i < length; i++)
|
|
141
|
-
array[i] = i;
|
|
142
|
-
return array;
|
|
143
|
-
}
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* When using an unsigned integer array to store pointers, one might want to
|
|
3
|
-
* choose the optimal word size in regards to the actual numbers of pointers
|
|
4
|
-
* to store.
|
|
5
|
-
*
|
|
6
|
-
* This helpers does just that.
|
|
7
|
-
*
|
|
8
|
-
* @param {number} size - Expected size of the array to map.
|
|
9
|
-
* @return {TypedArray}
|
|
10
|
-
*/
|
|
11
|
-
const MAX_8BIT_INTEGER = Math.pow(2, 8) - 1
|
|
12
|
-
const MAX_16BIT_INTEGER = Math.pow(2, 16) - 1
|
|
13
|
-
const MAX_32BIT_INTEGER = Math.pow(2, 32) - 1
|
|
14
|
-
|
|
15
|
-
const MAX_SIGNED_8BIT_INTEGER = Math.pow(2, 7) - 1
|
|
16
|
-
const MAX_SIGNED_16BIT_INTEGER = Math.pow(2, 15) - 1
|
|
17
|
-
const MAX_SIGNED_32BIT_INTEGER = Math.pow(2, 31) - 1
|
|
18
|
-
|
|
19
|
-
export function getPointerArray(size) {
|
|
20
|
-
const maxIndex = size - 1
|
|
21
|
-
|
|
22
|
-
if (maxIndex <= MAX_8BIT_INTEGER) return Uint8Array
|
|
23
|
-
|
|
24
|
-
if (maxIndex <= MAX_16BIT_INTEGER) return Uint16Array
|
|
25
|
-
|
|
26
|
-
// if (maxIndex <= MAX_32BIT_INTEGER) return Uint32Array
|
|
27
|
-
|
|
28
|
-
return Uint32Array
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function getSignedPointerArray(size) {
|
|
32
|
-
const maxIndex = size - 1
|
|
33
|
-
|
|
34
|
-
if (maxIndex <= MAX_SIGNED_8BIT_INTEGER) return Int8Array
|
|
35
|
-
|
|
36
|
-
if (maxIndex <= MAX_SIGNED_16BIT_INTEGER) return Int16Array
|
|
37
|
-
|
|
38
|
-
if (maxIndex <= MAX_SIGNED_32BIT_INTEGER) return Int32Array
|
|
39
|
-
|
|
40
|
-
return Float64Array
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Function returning the minimal type able to represent the given number.
|
|
45
|
-
*
|
|
46
|
-
* @param {number} value - Value to test.
|
|
47
|
-
* @return {TypedArrayClass}
|
|
48
|
-
*/
|
|
49
|
-
export function getNumberType(value) {
|
|
50
|
-
// <= 32 bits itnteger?
|
|
51
|
-
if (value === (value | 0)) {
|
|
52
|
-
// Negative
|
|
53
|
-
if (Math.sign(value) === -1) {
|
|
54
|
-
if (value <= 127 && value >= -128) return Int8Array
|
|
55
|
-
|
|
56
|
-
if (value <= 32767 && value >= -32768) return Int16Array
|
|
57
|
-
|
|
58
|
-
return Int32Array
|
|
59
|
-
} else {
|
|
60
|
-
if (value <= 255) return Uint8Array
|
|
61
|
-
|
|
62
|
-
if (value <= 65535) return Uint16Array
|
|
63
|
-
|
|
64
|
-
return Uint32Array
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// 53 bits integer & floats
|
|
69
|
-
// NOTE: it's kinda hard to tell whether we could use 32bits or not...
|
|
70
|
-
return Float64Array
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Function returning the minimal type able to represent the given array
|
|
75
|
-
* of JavaScript numbers.
|
|
76
|
-
*
|
|
77
|
-
* @param {array} array - Array to represent.
|
|
78
|
-
* @param {function} getter - Optional getter.
|
|
79
|
-
* @return {TypedArrayClass}
|
|
80
|
-
*/
|
|
81
|
-
const TYPE_PRIORITY = {
|
|
82
|
-
Uint8Array: 1,
|
|
83
|
-
Int8Array: 2,
|
|
84
|
-
Uint16Array: 3,
|
|
85
|
-
Int16Array: 4,
|
|
86
|
-
Uint32Array: 5,
|
|
87
|
-
Int32Array: 6,
|
|
88
|
-
Float32Array: 7,
|
|
89
|
-
Float64Array: 8
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export function getMinimalRepresentation(array, getter) {
|
|
93
|
-
let maxType = null
|
|
94
|
-
let maxPriority = 0
|
|
95
|
-
let p
|
|
96
|
-
let t
|
|
97
|
-
let v
|
|
98
|
-
let i
|
|
99
|
-
let l
|
|
100
|
-
|
|
101
|
-
for (i = 0, l = array.length; i < l; i++) {
|
|
102
|
-
v = getter ? getter(array[i]) : array[i]
|
|
103
|
-
t = exports.getNumberType(v)
|
|
104
|
-
p = TYPE_PRIORITY[t.name]
|
|
105
|
-
|
|
106
|
-
if (p > maxPriority) {
|
|
107
|
-
maxPriority = p
|
|
108
|
-
maxType = t
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return maxType
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Function returning whether the given value is a typed array.
|
|
117
|
-
*
|
|
118
|
-
* @param {any} value - Value to test.
|
|
119
|
-
* @return {boolean}
|
|
120
|
-
*/
|
|
121
|
-
export function isTypedArray(value) {
|
|
122
|
-
return ArrayBuffer.isView(value)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Function used to concat byte arrays.
|
|
127
|
-
*
|
|
128
|
-
* @param {...ByteArray}
|
|
129
|
-
* @return {ByteArray}
|
|
130
|
-
*/
|
|
131
|
-
export function concat() {
|
|
132
|
-
let length = 0
|
|
133
|
-
let i
|
|
134
|
-
let o
|
|
135
|
-
let l
|
|
136
|
-
|
|
137
|
-
for (i = 0, l = arguments.length; i < l; i++) length += arguments[i].length
|
|
138
|
-
|
|
139
|
-
const array = new arguments[0].constructor(length)
|
|
140
|
-
|
|
141
|
-
for (i = 0, o = 0; i < l; i++) {
|
|
142
|
-
array.set(arguments[i], o)
|
|
143
|
-
o += arguments[i].length
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return array
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Function used to initialize a byte array of indices.
|
|
151
|
-
*
|
|
152
|
-
* @param {number} length - Length of target.
|
|
153
|
-
* @return {ByteArray}
|
|
154
|
-
*/
|
|
155
|
-
export function indices(length) {
|
|
156
|
-
const PointerArray = exports.getPointerArray(length)
|
|
157
|
-
|
|
158
|
-
const array = new PointerArray(length)
|
|
159
|
-
|
|
160
|
-
for (let i = 0; i < length; i++) array[i] = i
|
|
161
|
-
|
|
162
|
-
return array
|
|
163
|
-
}
|
package/core/functools/index.ts
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This module includes functions that promote a more functional programming style.
|
|
3
|
-
*
|
|
4
|
-
* @module Functions
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { Function, isPromise } from '../globals/index.js'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Creates a function that invokes `fn` with the this binding of `thisArg` and `partials` as partially applied arguments.
|
|
11
|
-
* @param {Function} fn The function to bind.
|
|
12
|
-
* @param {*} thisArg The `this` value of `fn`.
|
|
13
|
-
* @param {...*} [partials] The arguments to prepend to `fn`.
|
|
14
|
-
* @returns {Function} Returns the new bound function.
|
|
15
|
-
* @see {@link partial}
|
|
16
|
-
*/
|
|
17
|
-
export const bind = (fn: Function, thisArg, ...partials) => fn.bind(thisArg, ...partials)
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least arity number of arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on. The arity of `func` may be specified if `func.length` is not sufficient.
|
|
21
|
-
* @param {Function} func The function to curry.
|
|
22
|
-
* @param {number} [arity=func.length]
|
|
23
|
-
* @returns Returns the new curried function.
|
|
24
|
-
*/
|
|
25
|
-
export const curry = (func: Function, arity?: number) => {
|
|
26
|
-
if (typeof func !== 'function' || func.length < 2) return func
|
|
27
|
-
if (typeof arity !== 'number') arity = func.length
|
|
28
|
-
switch (arity) {
|
|
29
|
-
case 2:
|
|
30
|
-
return (arg0) => (arg1) => func(arg0, arg1)
|
|
31
|
-
case 3:
|
|
32
|
-
return (arg0) => (arg1) => (arg2) => func(arg0, arg1, arg2)
|
|
33
|
-
case 4:
|
|
34
|
-
return (arg0) => (arg1) => (arg2) => (arg3) => func(arg0, arg1, arg2, arg3)
|
|
35
|
-
case 5:
|
|
36
|
-
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => func(arg0, arg1, arg2, arg3, arg4)
|
|
37
|
-
case 6:
|
|
38
|
-
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => (arg5) =>
|
|
39
|
-
func(arg0, arg1, arg2, arg3, arg4, arg5)
|
|
40
|
-
case 7:
|
|
41
|
-
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => (arg5) => (arg6) =>
|
|
42
|
-
func(arg0, arg1, arg2, arg3, arg4, arg5, arg6)
|
|
43
|
-
case 8:
|
|
44
|
-
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => (arg5) => (arg6) => (arg7) =>
|
|
45
|
-
func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
|
|
46
|
-
case 9:
|
|
47
|
-
return (arg0) =>
|
|
48
|
-
(arg1) =>
|
|
49
|
-
(arg2) =>
|
|
50
|
-
(arg3) =>
|
|
51
|
-
(arg4) =>
|
|
52
|
-
(arg5) =>
|
|
53
|
-
(arg6) =>
|
|
54
|
-
(arg7) =>
|
|
55
|
-
(arg8) =>
|
|
56
|
-
func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
|
|
57
|
-
|
|
58
|
-
default:
|
|
59
|
-
throw Error('The specified function uses too many arguments. Consider refactoring it.')
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Creates a function that invokes `fn` with partials prepended to the arguments it receives.
|
|
65
|
-
* **Note:** The given function will have `this` bound to `undefined`. If using `this` inside `fn`, consider {@link bind} instead.
|
|
66
|
-
* @param {Function} fn The function to bind.
|
|
67
|
-
* @param {...*} [args] The arguments to apply to `fn`.
|
|
68
|
-
* @returns {Function} Returns the new partially applied function
|
|
69
|
-
*/
|
|
70
|
-
export const partial = (fn, ...args) => bind(fn, undefined, ...args)
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Creates a function composition from a given set of functions that will be each applied on the result of the previous one from left to right.
|
|
74
|
-
* @param {...*} [args] The set of functions to apply.
|
|
75
|
-
* @returns {Function} A new function that applies each given function on the result of the previous one.
|
|
76
|
-
*/
|
|
77
|
-
export const flow =
|
|
78
|
-
(...args) =>
|
|
79
|
-
(x) => {
|
|
80
|
-
for (const fn of args) {
|
|
81
|
-
x = fn(x)
|
|
82
|
-
}
|
|
83
|
-
return x
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* This is just an alias for the {@link flow} function.
|
|
88
|
-
* @see {@link flow}
|
|
89
|
-
*/
|
|
90
|
-
export const pipe = flow
|
|
91
|
-
|
|
92
|
-
export const compose = (...args) => flow(args.reverse())
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Creates a function that memoizes the result of `func`. If `resolver` is provided, it determines the cache key for storing the result based on the arguments provided to the memoized function. By default, the first argument provided to the memoized function is used as the map cache key.
|
|
96
|
-
*
|
|
97
|
-
* @param {Function} func The function to have its output memoized.
|
|
98
|
-
* @param {Function} [resolver] The function to resolve the cache key.
|
|
99
|
-
* @return {Function} Returns the new memoized function.
|
|
100
|
-
*/
|
|
101
|
-
export function memoize<T>(
|
|
102
|
-
func: Function<T>,
|
|
103
|
-
resolver?: Function
|
|
104
|
-
): Function<ReturnType<typeof func>> {
|
|
105
|
-
const cache = new Map()
|
|
106
|
-
return (...args) => {
|
|
107
|
-
const key = resolver ? resolver(...args) : args[0]
|
|
108
|
-
const prev = cache.get(key)
|
|
109
|
-
if (prev !== undefined) return prev
|
|
110
|
-
const value = func(...args)
|
|
111
|
-
cache.set(key, value)
|
|
112
|
-
return value
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// TODO: also sized lru_cache
|
|
117
|
-
export function cache(target, name, descriptor) {
|
|
118
|
-
const original = descriptor.value
|
|
119
|
-
if (typeof original === 'function') {
|
|
120
|
-
descriptor.value = memoize(original)
|
|
121
|
-
}
|
|
122
|
-
return descriptor
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Creates a debounced function that delays invoking `func` until after `wait` milliseconds have elapsed since the last time the debounced function was invoked. Subsequent calls to the debounced function return the result of the last successful invocation. Provide options to set the `maxWait` milliseconds, regardless of when the function was last invoked.
|
|
127
|
-
*
|
|
128
|
-
* @param {Function} func The function to debounce.
|
|
129
|
-
* @param {number} wait The number of milliseconds to delay.
|
|
130
|
-
* @param {{ maxWait: number }} [opts] The maximum time `func` is allowed to be delayed before it's invoked.
|
|
131
|
-
* @returns Returns the new debounced function.
|
|
132
|
-
*/
|
|
133
|
-
export function debounce(
|
|
134
|
-
func: Function,
|
|
135
|
-
wait: number,
|
|
136
|
-
{ maxWait }: { maxWait?: number } = {}
|
|
137
|
-
): Function {
|
|
138
|
-
let timer, maxTimer, lastResult
|
|
139
|
-
return (...args) => {
|
|
140
|
-
if (timer) clearTimeout(timer)
|
|
141
|
-
timer = setTimeout(() => {
|
|
142
|
-
clearTimeout(maxTimer)
|
|
143
|
-
maxTimer = null
|
|
144
|
-
lastResult = func(...args)
|
|
145
|
-
}, wait)
|
|
146
|
-
|
|
147
|
-
if (maxWait && !maxTimer) {
|
|
148
|
-
maxTimer = setTimeout(() => {
|
|
149
|
-
clearTimeout(timer)
|
|
150
|
-
maxTimer = null
|
|
151
|
-
lastResult = func(...args)
|
|
152
|
-
}, maxWait)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return lastResult
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Creates a throttled function that only invokes `func` at most once per every `wait` milliseconds. Subsequent calls to the throttled function return the result of the last successful invocation.
|
|
161
|
-
*
|
|
162
|
-
* @param {Function} func The function to throttle.
|
|
163
|
-
* @param {number} wait The number of milliseconds to throttle invocations to.
|
|
164
|
-
* @return {Function} Returns the new throttled function.
|
|
165
|
-
*/
|
|
166
|
-
export function throttle(func: Function, wait: number): Function {
|
|
167
|
-
let pending = false
|
|
168
|
-
return (...args) => {
|
|
169
|
-
if (pending) return
|
|
170
|
-
func(...args)
|
|
171
|
-
pending = true
|
|
172
|
-
setTimeout(() => {
|
|
173
|
-
pending = false
|
|
174
|
-
}, wait)
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Creates a function that is restricted to invoking `func` once. Repeat calls to the function return the value of the first invocation. The `func` is invoked with the arguments of the created function.
|
|
180
|
-
*
|
|
181
|
-
* @param {Function} func The function to restrict
|
|
182
|
-
* @returns {Function} Returns the new restricted function.
|
|
183
|
-
* @example
|
|
184
|
-
```js
|
|
185
|
-
let func = x => x
|
|
186
|
-
let single = once(func)
|
|
187
|
-
single(1) // => 1
|
|
188
|
-
single(2) // => 1
|
|
189
|
-
single(3) // => 1
|
|
190
|
-
// ...
|
|
191
|
-
// => `func` is invoked only once.
|
|
192
|
-
```
|
|
193
|
-
*/
|
|
194
|
-
export function once<T>(func: Function<T>) {
|
|
195
|
-
let result, isCalled
|
|
196
|
-
return (...args) => {
|
|
197
|
-
if (!isCalled) {
|
|
198
|
-
result = func(...args)
|
|
199
|
-
isCalled = true
|
|
200
|
-
}
|
|
201
|
-
return result as T
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export type Maybe<T> = T | undefined
|
|
206
|
-
|
|
207
|
-
export type Result<T, E = any> = [Maybe<T>, Maybe<E>]
|
|
208
|
-
|
|
209
|
-
export type Optional<T> = Maybe<T>
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Wraps a function to catch any exceptions inside and return a `go` style error-return type {@link Result}.
|
|
213
|
-
*
|
|
214
|
-
* @param {Function} fun The function to wrap and catch if it throws.
|
|
215
|
-
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
216
|
-
* @returns {Function} A new function that wraps `fun`.
|
|
217
|
-
* @template T,E
|
|
218
|
-
*/
|
|
219
|
-
export function result<T = any, E = unknown>(
|
|
220
|
-
fun: Function<T>,
|
|
221
|
-
onFinally?: (err: E, res: T) => any
|
|
222
|
-
) {
|
|
223
|
-
return (...args): Result<T, E> => {
|
|
224
|
-
let value, err
|
|
225
|
-
try {
|
|
226
|
-
value = fun(...args)
|
|
227
|
-
} catch (error) {
|
|
228
|
-
err = error
|
|
229
|
-
} finally {
|
|
230
|
-
onFinally?.(err, value)
|
|
231
|
-
}
|
|
232
|
-
return [value as T, err]
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Wraps a function or promise to catch any exceptions inside and return a `go` style error-return type. This is like {@link result} but it deals with async functions or promises.
|
|
238
|
-
*
|
|
239
|
-
* @param {Function|Promise} awaitable The function or promise to await.
|
|
240
|
-
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
241
|
-
* @returns {Function} A new function that awaits the `awaitable` param and returns a {@link Result} type.
|
|
242
|
-
* @template T,E
|
|
243
|
-
*/
|
|
244
|
-
export function resultAsync<T = any, E = unknown>(
|
|
245
|
-
awaitable: Promise<T> | Function<Promise<T>>,
|
|
246
|
-
onFinally?: (err: E, res: T) => any
|
|
247
|
-
) {
|
|
248
|
-
return async (...args): Promise<Result<T, E>> => {
|
|
249
|
-
let value, err
|
|
250
|
-
try {
|
|
251
|
-
const promise = isPromise(awaitable) ? awaitable : awaitable(...args)
|
|
252
|
-
value = await promise
|
|
253
|
-
} catch (error) {
|
|
254
|
-
err = error
|
|
255
|
-
} finally {
|
|
256
|
-
onFinally?.(err, value)
|
|
257
|
-
}
|
|
258
|
-
return [value as T, err]
|
|
259
|
-
}
|
|
260
|
-
}
|