atomic-fns 0.3.4 → 0.4.2
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 +3 -3
- 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 +1 -1
- package/{core → dist}/collections/abc.js +0 -0
- package/dist/collections/deque.d.ts +117 -0
- package/{core → dist}/collections/deque.js +61 -53
- package/{core → dist}/collections/frozenset.d.ts +0 -0
- package/{core → dist}/collections/frozenset.js +3 -6
- package/{core → dist}/collections/index.d.ts +4 -4
- package/{core → dist}/collections/index.js +0 -0
- package/{core → dist}/functools/index.d.ts +28 -13
- package/{core → dist}/functools/index.js +34 -12
- package/{core → dist}/globals/index.d.ts +8 -11
- package/{core → dist}/globals/index.js +8 -15
- 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 +312 -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 +1 -1
- package/{core → dist}/i18n/index.js +1 -1
- package/{core → dist}/index.d.ts +0 -0
- package/{core → dist}/index.js +0 -0
- package/{core → dist}/itertools/index.d.ts +27 -27
- package/{core → dist}/itertools/index.js +24 -24
- package/{core → dist}/math/index.d.ts +3 -3
- package/{core → dist}/math/index.js +0 -0
- package/{core → dist}/operators/index.d.ts +2 -2
- package/{core → dist}/operators/index.js +2 -2
- package/{core → dist}/random/index.d.ts +1 -1
- package/{core → dist}/random/index.js +3 -3
- 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.d.ts +0 -110
- package/core/collections/deque.ts +0 -329
- 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 -258
- 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 -223
- 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
|
@@ -4,36 +4,39 @@ import { eq } from '../operators/index.js';
|
|
|
4
4
|
import { Sequence } from './abc.js';
|
|
5
5
|
/**
|
|
6
6
|
* Fixed capacity double-ended queue implemented as ring deque.
|
|
7
|
-
*
|
|
8
|
-
* @constructor
|
|
7
|
+
* @template T
|
|
9
8
|
*/
|
|
10
9
|
export class Deque extends Sequence {
|
|
11
10
|
items;
|
|
12
11
|
start = 0;
|
|
13
12
|
count = 0;
|
|
14
13
|
capacity;
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
ArrayConstructor;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new `Deque` instance of a fixed size.
|
|
17
|
+
* @param {Array} [arrType = []] The initial elements or array type to use (accepts any kind of typed array).
|
|
18
|
+
* @param {number} [capacity=1024] The fixed size of the deque. Defaults to `1024`.
|
|
19
|
+
*/
|
|
20
|
+
constructor(arrType = [], capacity = 1024) {
|
|
17
21
|
super();
|
|
18
22
|
if (typeof capacity !== 'number' || capacity <= 0)
|
|
19
23
|
throw new ValueError(`Deque capacity should be a positive integer.`);
|
|
20
|
-
this.
|
|
24
|
+
this.ArrayConstructor = arrType.constructor;
|
|
21
25
|
this.capacity = capacity;
|
|
22
|
-
this.items = new this.
|
|
26
|
+
this.items = new this.ArrayConstructor(arrType.length || this.capacity);
|
|
23
27
|
this.clear();
|
|
24
|
-
if (
|
|
25
|
-
for (const item of
|
|
28
|
+
if (arrType.length) {
|
|
29
|
+
for (const item of arrType) {
|
|
26
30
|
this.append(item);
|
|
27
31
|
}
|
|
28
32
|
}
|
|
29
33
|
}
|
|
34
|
+
/** Returns the total number of elements in the deque. */
|
|
30
35
|
get size() {
|
|
31
36
|
return this.count;
|
|
32
37
|
}
|
|
33
38
|
/**
|
|
34
39
|
* Method used to clear the structure.
|
|
35
|
-
*
|
|
36
|
-
* @return {undefined}
|
|
37
40
|
*/
|
|
38
41
|
clear() {
|
|
39
42
|
// Properties
|
|
@@ -42,18 +45,16 @@ export class Deque extends Sequence {
|
|
|
42
45
|
}
|
|
43
46
|
/**
|
|
44
47
|
* Alias of {@link Deque.append}
|
|
45
|
-
*
|
|
46
|
-
* @
|
|
47
|
-
* @return {number} - Returns the new size of the deque.
|
|
48
|
+
* @param {T} item Item to append.
|
|
49
|
+
* @returns {number} Returns the new size of the deque.
|
|
48
50
|
*/
|
|
49
51
|
add(item) {
|
|
50
52
|
return this.append(item);
|
|
51
53
|
}
|
|
52
54
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @
|
|
56
|
-
* @return {number} - Returns the new size of the deque.
|
|
55
|
+
* Appends a new element to the end of the deque.
|
|
56
|
+
* @param {T} item Item to append.
|
|
57
|
+
* @returns {number} Returns the new size of the deque.
|
|
57
58
|
*/
|
|
58
59
|
append(item) {
|
|
59
60
|
if (this.count === this.capacity)
|
|
@@ -63,10 +64,9 @@ export class Deque extends Sequence {
|
|
|
63
64
|
return ++this.count;
|
|
64
65
|
}
|
|
65
66
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* @
|
|
69
|
-
* @return {number} - Returns the new size of the deque.
|
|
67
|
+
* Appends a new element to the beginning of the deque.
|
|
68
|
+
* @param {T} item Item to prepend.
|
|
69
|
+
* @returns {number} Returns the new size of the deque.
|
|
70
70
|
*/
|
|
71
71
|
appendleft(item) {
|
|
72
72
|
if (this.count === this.capacity)
|
|
@@ -79,9 +79,8 @@ export class Deque extends Sequence {
|
|
|
79
79
|
return ++this.count;
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
* @return {any} - Returns the popped item.
|
|
82
|
+
* Removes and returns the element at the end of the deque.
|
|
83
|
+
* @returns {?T} Returns the popped item.
|
|
85
84
|
*/
|
|
86
85
|
pop() {
|
|
87
86
|
if (this.count === 0)
|
|
@@ -92,14 +91,14 @@ export class Deque extends Sequence {
|
|
|
92
91
|
}
|
|
93
92
|
/**
|
|
94
93
|
* Alias of {@link Deque.pop}
|
|
94
|
+
* @returns {?T} Returns the popped item.
|
|
95
95
|
*/
|
|
96
96
|
remove() {
|
|
97
97
|
return this.pop();
|
|
98
98
|
}
|
|
99
99
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* @return {any} - Returns the shifted item.
|
|
100
|
+
* Removes and returns the element at the beginning of the deque.
|
|
101
|
+
* @returns {?T} Returns the removed deque element.
|
|
103
102
|
*/
|
|
104
103
|
popleft() {
|
|
105
104
|
if (this.count === 0)
|
|
@@ -112,9 +111,8 @@ export class Deque extends Sequence {
|
|
|
112
111
|
return this.items[index];
|
|
113
112
|
}
|
|
114
113
|
/**
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* @return {any}
|
|
114
|
+
* Returns the element at the beginning of the deque.
|
|
115
|
+
* @returns {?T} The element if exists
|
|
118
116
|
*/
|
|
119
117
|
first() {
|
|
120
118
|
if (this.count === 0)
|
|
@@ -122,10 +120,9 @@ export class Deque extends Sequence {
|
|
|
122
120
|
return this.items[this.start];
|
|
123
121
|
}
|
|
124
122
|
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* @
|
|
128
|
-
* @return {any}
|
|
123
|
+
* Returns the element at a specified index position in the deque.
|
|
124
|
+
* @param {number} index The given index
|
|
125
|
+
* @returns {?T} The element if exists
|
|
129
126
|
*/
|
|
130
127
|
get(index) {
|
|
131
128
|
if (this.count === 0)
|
|
@@ -138,9 +135,8 @@ export class Deque extends Sequence {
|
|
|
138
135
|
return this.items[index];
|
|
139
136
|
}
|
|
140
137
|
/**
|
|
141
|
-
* Update the
|
|
142
|
-
* @param index The given index
|
|
143
|
-
* @param value The new value to set at `index`.
|
|
138
|
+
* Update the element at a given `index`.
|
|
139
|
+
* @param {number} index The given index
|
|
144
140
|
*/
|
|
145
141
|
set(index, value) {
|
|
146
142
|
if (index < 0)
|
|
@@ -149,6 +145,11 @@ export class Deque extends Sequence {
|
|
|
149
145
|
throw new KeyError(`Invalid index: ${index} not in Deque.`);
|
|
150
146
|
this.items[index] = value;
|
|
151
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Check if a given element is in the deque.
|
|
150
|
+
* @param {T} x The value to find
|
|
151
|
+
* @returns {boolean} `true` if the value is found
|
|
152
|
+
*/
|
|
152
153
|
contains(x) {
|
|
153
154
|
for (const value of this.values()) {
|
|
154
155
|
if (eq(x, value))
|
|
@@ -156,6 +157,11 @@ export class Deque extends Sequence {
|
|
|
156
157
|
}
|
|
157
158
|
return false;
|
|
158
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* Returns the first element that matches the `iteratee` function.
|
|
162
|
+
* @param {Iteratee<T>} iteratee A function that will be invoked per element
|
|
163
|
+
* @returns {?T} The element if exists
|
|
164
|
+
*/
|
|
159
165
|
find(iteratee) {
|
|
160
166
|
for (const [key, value] of this.entries()) {
|
|
161
167
|
if (iteratee(value, key, this))
|
|
@@ -163,11 +169,8 @@ export class Deque extends Sequence {
|
|
|
163
169
|
}
|
|
164
170
|
}
|
|
165
171
|
/**
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* @param {function} iteratee - Function to call for each item.
|
|
169
|
-
* @param {object} scope - Optional scope.
|
|
170
|
-
* @return {undefined}
|
|
172
|
+
* Calls `iteratee` function for every element in the deque.
|
|
173
|
+
* @param {Iteratee<T>} iteratee A function that will be invoked per element
|
|
171
174
|
*/
|
|
172
175
|
forEach(iteratee) {
|
|
173
176
|
const c = this.capacity;
|
|
@@ -182,20 +185,23 @@ export class Deque extends Sequence {
|
|
|
182
185
|
i = 0;
|
|
183
186
|
}
|
|
184
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Returns a new array with all the elements in reverse order.
|
|
190
|
+
* @returns {Array<T>} The elements reversed
|
|
191
|
+
*/
|
|
185
192
|
reversed() {
|
|
186
193
|
return this.toArray().reverse();
|
|
187
194
|
}
|
|
188
195
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
* @return {array}
|
|
196
|
+
* Returns a new array with all the elements in the order they were added to the deque.
|
|
197
|
+
* @returns {Array<T>} The elements array
|
|
192
198
|
*/
|
|
193
199
|
toArray() {
|
|
194
200
|
// Optimization
|
|
195
201
|
const offset = this.start + this.count;
|
|
196
202
|
if (offset < this.capacity)
|
|
197
203
|
return this.items.slice(this.start, offset);
|
|
198
|
-
const array = new this.
|
|
204
|
+
const array = new this.ArrayConstructor(this.count);
|
|
199
205
|
const c = this.capacity;
|
|
200
206
|
const l = this.count;
|
|
201
207
|
let i = this.start;
|
|
@@ -210,9 +216,8 @@ export class Deque extends Sequence {
|
|
|
210
216
|
return array;
|
|
211
217
|
}
|
|
212
218
|
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
* @return {Iterator}
|
|
219
|
+
* Returns an iterator over the deque's values.
|
|
220
|
+
* @returns {Iterator}
|
|
216
221
|
*/
|
|
217
222
|
values() {
|
|
218
223
|
const items = this.items;
|
|
@@ -236,13 +241,15 @@ export class Deque extends Sequence {
|
|
|
236
241
|
};
|
|
237
242
|
});
|
|
238
243
|
}
|
|
244
|
+
/**
|
|
245
|
+
* Alias of {@link Deque.values}. Returns iterator of values.
|
|
246
|
+
*/
|
|
239
247
|
keys() {
|
|
240
248
|
return this.values();
|
|
241
249
|
}
|
|
242
250
|
/**
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
* @return {Iterator}
|
|
251
|
+
* Creates an iterator of `[index, value]` pairs for all elements in the deque.
|
|
252
|
+
* @returns {Iterator}
|
|
246
253
|
*/
|
|
247
254
|
entries() {
|
|
248
255
|
const items = this.items;
|
|
@@ -271,9 +278,10 @@ export class Deque extends Sequence {
|
|
|
271
278
|
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
272
279
|
return this.inspect();
|
|
273
280
|
}
|
|
281
|
+
// This is just for debugging in nodejs
|
|
274
282
|
inspect() {
|
|
275
283
|
const array = this.toArray();
|
|
276
|
-
array.type = this.
|
|
284
|
+
array.type = this.ArrayConstructor.name;
|
|
277
285
|
array.capacity = this.capacity;
|
|
278
286
|
// Trick so that node displays the name of the constructor
|
|
279
287
|
Object.defineProperty(array, 'constructor', {
|
|
File without changes
|
|
@@ -13,18 +13,15 @@ export class FrozenSet extends Set {
|
|
|
13
13
|
}
|
|
14
14
|
/** @private */
|
|
15
15
|
add() {
|
|
16
|
-
|
|
17
|
-
return this; // eslint-disable-line
|
|
16
|
+
return this;
|
|
18
17
|
}
|
|
19
18
|
/** @private */
|
|
20
19
|
delete() {
|
|
21
|
-
|
|
22
|
-
return false; // eslint-disable-line
|
|
20
|
+
return false;
|
|
23
21
|
}
|
|
24
22
|
/** @private */
|
|
25
23
|
clear() {
|
|
26
|
-
|
|
27
|
-
return this; // eslint-disable-line
|
|
24
|
+
return this;
|
|
28
25
|
}
|
|
29
26
|
/** @private */
|
|
30
27
|
freeze() {
|
|
@@ -38,7 +38,7 @@ export declare function count<T>(iterable: Iterable<T> | Object, func: Iteratee<
|
|
|
38
38
|
* @param fields - A list of field names
|
|
39
39
|
* @returns A function that can be called with the field values
|
|
40
40
|
*/
|
|
41
|
-
export declare function namedtuple(...fields: string[]): (...args: any[]) =>
|
|
41
|
+
export declare function namedtuple(...fields: string[]): (...args: any[]) => any;
|
|
42
42
|
/**
|
|
43
43
|
* Iterates over elements of collection, returning an array of all elements where predicate returns truthy value.
|
|
44
44
|
*
|
|
@@ -66,7 +66,7 @@ filter(users, 'active')
|
|
|
66
66
|
* @param {Function} fn The predicate function invoked for each item
|
|
67
67
|
* @returns {Array} The new filtered array
|
|
68
68
|
*/
|
|
69
|
-
export declare function filter(arr: any, fn?: Iteratee | PropertyKey | Object): any[]
|
|
69
|
+
export declare function filter(arr: any, fn?: Iteratee | PropertyKey | Object): any[];
|
|
70
70
|
/**
|
|
71
71
|
* Iterates over elements of collection, returning the first element where predicate returns truthy value. The predicate is invoked with three arguments: `(value, index|key, collection)`.
|
|
72
72
|
* @example
|
|
@@ -242,7 +242,7 @@ map(users, 'user')
|
|
|
242
242
|
* @returns Returns the new mapped array.
|
|
243
243
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map Array.map()}
|
|
244
244
|
*/
|
|
245
|
-
export declare function map(arr: any, fn: Iteratee | PropertyKey): any[]
|
|
245
|
+
export declare function map(arr: any, fn: Iteratee | PropertyKey): any[];
|
|
246
246
|
/**
|
|
247
247
|
* Creates an object composed of the picked object properties.
|
|
248
248
|
* @param {Object} obj The source object.
|
|
@@ -309,7 +309,7 @@ index(users, 'active')
|
|
|
309
309
|
* @see {@link lastIndex}
|
|
310
310
|
*
|
|
311
311
|
*/
|
|
312
|
-
export declare function index(obj: any, fn: Iteratee | string | Object, start?: number): number
|
|
312
|
+
export declare function index(obj: any, fn: Iteratee | string | Object, start?: number): number;
|
|
313
313
|
/**
|
|
314
314
|
* This method is like {@link index} except that it searches for a given value directly, instead of using a predicate function.
|
|
315
315
|
* @param {Array} obj The array to inspect.
|
|
File without changes
|
|
@@ -12,7 +12,7 @@ import { Function } from '../globals/index.js';
|
|
|
12
12
|
* @returns {Function} Returns the new bound function.
|
|
13
13
|
* @see {@link partial}
|
|
14
14
|
*/
|
|
15
|
-
export declare const bind: (fn: Function, thisArg: any, ...partials: any[]) =>
|
|
15
|
+
export declare const bind: (fn: Function, thisArg: any, ...partials: any[]) => any;
|
|
16
16
|
/**
|
|
17
17
|
* 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.
|
|
18
18
|
* @param {Function} func The function to curry.
|
|
@@ -27,7 +27,7 @@ export declare const curry: (func: Function, arity?: number) => Function<any>;
|
|
|
27
27
|
* @param {...*} [args] The arguments to apply to `fn`.
|
|
28
28
|
* @returns {Function} Returns the new partially applied function
|
|
29
29
|
*/
|
|
30
|
-
export declare const partial: (fn: any, ...args: any[]) =>
|
|
30
|
+
export declare const partial: (fn: any, ...args: any[]) => any;
|
|
31
31
|
/**
|
|
32
32
|
* 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.
|
|
33
33
|
* @param {...*} [args] The set of functions to apply.
|
|
@@ -48,6 +48,14 @@ export declare const compose: (...args: any[]) => (x: any) => any;
|
|
|
48
48
|
* @return {Function} Returns the new memoized function.
|
|
49
49
|
*/
|
|
50
50
|
export declare function memoize<T>(func: Function<T>, resolver?: Function): Function<ReturnType<typeof func>>;
|
|
51
|
+
/**
|
|
52
|
+
* Can be used as a decorator on any function or method to memoize its results.
|
|
53
|
+
* **NOTE:** By default the given function is memoized using it's first argument, regardless of others.
|
|
54
|
+
* @param target
|
|
55
|
+
* @param name
|
|
56
|
+
* @param descriptor
|
|
57
|
+
* @returns A memoized version of the given function
|
|
58
|
+
*/
|
|
51
59
|
export declare function cache(target: any, name: any, descriptor: any): any;
|
|
52
60
|
/**
|
|
53
61
|
* 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.
|
|
@@ -77,31 +85,38 @@ export declare function throttle(func: Function, wait: number): Function;
|
|
|
77
85
|
```js
|
|
78
86
|
let func = x => x
|
|
79
87
|
let single = once(func)
|
|
80
|
-
single(1) //
|
|
81
|
-
single(2) //
|
|
82
|
-
single(3) //
|
|
88
|
+
single(1) // 1
|
|
89
|
+
single(2) // 1
|
|
90
|
+
single(3) // 1
|
|
83
91
|
// ...
|
|
84
|
-
//
|
|
92
|
+
// `func` is invoked only once.
|
|
85
93
|
```
|
|
86
94
|
*/
|
|
87
95
|
export declare function once<T>(func: Function<T>): (...args: any[]) => T;
|
|
88
96
|
export declare type Maybe<T> = T | undefined;
|
|
89
97
|
export declare type Result<T, E = any> = [Maybe<T>, Maybe<E>];
|
|
98
|
+
export declare type Optional<T> = Maybe<T>;
|
|
90
99
|
/**
|
|
91
100
|
* Wraps a function to catch any exceptions inside and return a `go` style error-return type {@link Result}.
|
|
92
|
-
*
|
|
101
|
+
* @template T,E
|
|
93
102
|
* @param {Function} fun The function to wrap and catch if it throws.
|
|
94
|
-
* @param {
|
|
103
|
+
* @param {Function} [onFinally] Optional callback that will be invoked with `(err?, value)` on the `finally` clause.
|
|
95
104
|
* @returns {Function} A new function that wraps `fun`.
|
|
96
|
-
* @template T,E
|
|
97
105
|
*/
|
|
98
106
|
export declare function result<T = any, E = unknown>(fun: Function<T>, onFinally?: (err: E, res: T) => any): (...args: any[]) => Result<T, E>;
|
|
99
107
|
/**
|
|
100
108
|
* 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.
|
|
101
|
-
*
|
|
102
|
-
* @param {Function|Promise} awaitable The function or promise to await.
|
|
103
|
-
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
104
|
-
* @returns {Function} A new function that awaits the `awaitable` param and returns a {@link Result} type.
|
|
105
109
|
* @template T,E
|
|
110
|
+
* @param {Function<T>|Promise<T>} awaitable The function or promise to await.
|
|
111
|
+
* @param {Function} [onFinally] Optional callback that will be invoked with `(err?, value)` on the `finally` clause.
|
|
112
|
+
* @returns {Function<Promise<Result<T, E>>>} A new function that awaits the `awaitable` param and returns a {@link Result} type.
|
|
106
113
|
*/
|
|
107
114
|
export declare function resultAsync<T = any, E = unknown>(awaitable: Promise<T> | Function<Promise<T>>, onFinally?: (err: E, res: T) => any): (...args: any[]) => Promise<Result<T, E>>;
|
|
115
|
+
/**
|
|
116
|
+
* Converts a function that expects a node-style callback argument like `(err, result)` to return a Promise instead.
|
|
117
|
+
* @template T
|
|
118
|
+
* @param {Function<T>} fun The given function to convert from callback style to Promise.
|
|
119
|
+
* @param {*} thisArg
|
|
120
|
+
* @returns {Function<Promise<T>>} A function that wraps `fun` and returns a Promise.
|
|
121
|
+
*/
|
|
122
|
+
export declare function promisify<T = any>(fun: Function<T>, thisArg: any): (...args: any[]) => Promise<T>;
|
|
@@ -89,7 +89,14 @@ export function memoize(func, resolver) {
|
|
|
89
89
|
return value;
|
|
90
90
|
};
|
|
91
91
|
}
|
|
92
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Can be used as a decorator on any function or method to memoize its results.
|
|
94
|
+
* **NOTE:** By default the given function is memoized using it's first argument, regardless of others.
|
|
95
|
+
* @param target
|
|
96
|
+
* @param name
|
|
97
|
+
* @param descriptor
|
|
98
|
+
* @returns A memoized version of the given function
|
|
99
|
+
*/
|
|
93
100
|
export function cache(target, name, descriptor) {
|
|
94
101
|
const original = descriptor.value;
|
|
95
102
|
if (typeof original === 'function') {
|
|
@@ -153,11 +160,11 @@ export function throttle(func, wait) {
|
|
|
153
160
|
```js
|
|
154
161
|
let func = x => x
|
|
155
162
|
let single = once(func)
|
|
156
|
-
single(1) //
|
|
157
|
-
single(2) //
|
|
158
|
-
single(3) //
|
|
163
|
+
single(1) // 1
|
|
164
|
+
single(2) // 1
|
|
165
|
+
single(3) // 1
|
|
159
166
|
// ...
|
|
160
|
-
//
|
|
167
|
+
// `func` is invoked only once.
|
|
161
168
|
```
|
|
162
169
|
*/
|
|
163
170
|
export function once(func) {
|
|
@@ -172,11 +179,10 @@ export function once(func) {
|
|
|
172
179
|
}
|
|
173
180
|
/**
|
|
174
181
|
* Wraps a function to catch any exceptions inside and return a `go` style error-return type {@link Result}.
|
|
175
|
-
*
|
|
182
|
+
* @template T,E
|
|
176
183
|
* @param {Function} fun The function to wrap and catch if it throws.
|
|
177
|
-
* @param {
|
|
184
|
+
* @param {Function} [onFinally] Optional callback that will be invoked with `(err?, value)` on the `finally` clause.
|
|
178
185
|
* @returns {Function} A new function that wraps `fun`.
|
|
179
|
-
* @template T,E
|
|
180
186
|
*/
|
|
181
187
|
export function result(fun, onFinally) {
|
|
182
188
|
return (...args) => {
|
|
@@ -195,11 +201,10 @@ export function result(fun, onFinally) {
|
|
|
195
201
|
}
|
|
196
202
|
/**
|
|
197
203
|
* 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.
|
|
198
|
-
*
|
|
199
|
-
* @param {Function|Promise} awaitable The function or promise to await.
|
|
200
|
-
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
201
|
-
* @returns {Function} A new function that awaits the `awaitable` param and returns a {@link Result} type.
|
|
202
204
|
* @template T,E
|
|
205
|
+
* @param {Function<T>|Promise<T>} awaitable The function or promise to await.
|
|
206
|
+
* @param {Function} [onFinally] Optional callback that will be invoked with `(err?, value)` on the `finally` clause.
|
|
207
|
+
* @returns {Function<Promise<Result<T, E>>>} A new function that awaits the `awaitable` param and returns a {@link Result} type.
|
|
203
208
|
*/
|
|
204
209
|
export function resultAsync(awaitable, onFinally) {
|
|
205
210
|
return async (...args) => {
|
|
@@ -217,3 +222,20 @@ export function resultAsync(awaitable, onFinally) {
|
|
|
217
222
|
return [value, err];
|
|
218
223
|
};
|
|
219
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* Converts a function that expects a node-style callback argument like `(err, result)` to return a Promise instead.
|
|
227
|
+
* @template T
|
|
228
|
+
* @param {Function<T>} fun The given function to convert from callback style to Promise.
|
|
229
|
+
* @param {*} thisArg
|
|
230
|
+
* @returns {Function<Promise<T>>} A function that wraps `fun` and returns a Promise.
|
|
231
|
+
*/
|
|
232
|
+
export function promisify(fun, thisArg) {
|
|
233
|
+
const original = fun.bind(thisArg);
|
|
234
|
+
return async (...args) => {
|
|
235
|
+
return await new Promise((resolve, reject) => original(...args, (err, result) => {
|
|
236
|
+
if (err)
|
|
237
|
+
return reject(err);
|
|
238
|
+
resolve(result);
|
|
239
|
+
}));
|
|
240
|
+
};
|
|
241
|
+
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @module Globals
|
|
5
5
|
*/
|
|
6
6
|
/** This is just a noop function. */
|
|
7
|
-
export declare const
|
|
7
|
+
export declare const noop: () => void;
|
|
8
8
|
/**
|
|
9
9
|
* A generic function type with arbitrary arguments and return.
|
|
10
10
|
*/
|
|
@@ -92,7 +92,6 @@ export declare function type(value: any): any;
|
|
|
92
92
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString | Object.toString }
|
|
93
93
|
*/
|
|
94
94
|
export declare const str: (obj: any) => any;
|
|
95
|
-
export declare function hasOwnProperty(obj: any, prop: any): boolean;
|
|
96
95
|
/** Check if value is a boolean type. */
|
|
97
96
|
export declare const isBool: (x: any) => x is boolean;
|
|
98
97
|
/** Check if value is an `Error` type. */
|
|
@@ -118,8 +117,6 @@ export declare const isNumber: (x: any) => x is number;
|
|
|
118
117
|
export declare const isInteger: (x: any) => x is number;
|
|
119
118
|
/** Check if value is a bigint type. */
|
|
120
119
|
export declare const isBigint: (x: any) => x is BigInt;
|
|
121
|
-
/** Check if value is NaN based on `Number.isNaN`. */
|
|
122
|
-
export declare const isNaN: (x: any) => x is number;
|
|
123
120
|
/** Check if value is a Promise type. */
|
|
124
121
|
export declare const isPromise: (x: any) => x is Promise<any>;
|
|
125
122
|
/** Check if value is a function type. */
|
|
@@ -129,9 +126,9 @@ export declare const isAsyncFunction: (x: any) => boolean;
|
|
|
129
126
|
/** Check if value is a generator function type. */
|
|
130
127
|
export declare const isGenerator: (x: any) => x is Generator<unknown, any, unknown>;
|
|
131
128
|
/** Check if value is `null` or `undefined`. */
|
|
132
|
-
export declare const isNull: (x: any) => x is null
|
|
129
|
+
export declare const isNull: (x: any) => x is null;
|
|
133
130
|
/** Check if value === `undefined`. */
|
|
134
|
-
export declare
|
|
131
|
+
export declare const isUndefined: (x: any) => x is undefined;
|
|
135
132
|
/** Check if value is not `null` or `undefined`. */
|
|
136
133
|
export declare const notNull: (x: any) => boolean;
|
|
137
134
|
/** Returns `true` for objects without length or falsy values. */
|
|
@@ -164,10 +161,10 @@ export declare function len(value: any): any;
|
|
|
164
161
|
* @example
|
|
165
162
|
```js
|
|
166
163
|
uniqueId()
|
|
167
|
-
//
|
|
164
|
+
// 3445556877
|
|
168
165
|
|
|
169
166
|
uniqueId('user_')
|
|
170
|
-
//
|
|
167
|
+
// 'user_1033763188'
|
|
171
168
|
```
|
|
172
169
|
*/
|
|
173
170
|
export declare const uniqueId: (pre?: string) => any;
|
|
@@ -177,7 +174,7 @@ export declare const uniqueId: (pre?: string) => any;
|
|
|
177
174
|
* @example
|
|
178
175
|
```js
|
|
179
176
|
uuid()
|
|
180
|
-
//
|
|
177
|
+
// '183f8ef3-b8f0-4000-81f5-89234c836d00'
|
|
181
178
|
```
|
|
182
179
|
*/
|
|
183
180
|
export declare const uuid: () => string;
|
|
@@ -213,7 +210,7 @@ export declare const values: (object: any) => unknown[];
|
|
|
213
210
|
* @param attr A property key name.
|
|
214
211
|
* @returns `true` if the object has the property name.
|
|
215
212
|
*/
|
|
216
|
-
export declare const has: (obj: any, attr: PropertyKey) =>
|
|
213
|
+
export declare const has: (obj: any, attr: PropertyKey) => boolean;
|
|
217
214
|
/**
|
|
218
215
|
* Check if the attribute is present in the object or return a default value instead.
|
|
219
216
|
* @param {PropertyKey} key
|
|
@@ -293,7 +290,7 @@ export declare const int: (x: any, base?: number) => number;
|
|
|
293
290
|
*/
|
|
294
291
|
export declare const list: (value?: any) => unknown[];
|
|
295
292
|
/**
|
|
296
|
-
* Retrieve the next item from the iterator by calling
|
|
293
|
+
* Retrieve the next item from the iterator by calling its `next()` method.
|
|
297
294
|
* @param iter The iterator.
|
|
298
295
|
* @returns The next value from the iterator.
|
|
299
296
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @module Globals
|
|
5
5
|
*/
|
|
6
6
|
/** This is just a noop function. */
|
|
7
|
-
export const
|
|
7
|
+
export const noop = () => { };
|
|
8
8
|
/** A function that always returns `true`. */
|
|
9
9
|
export const True = () => true;
|
|
10
10
|
/** A function that always returns `false`. */
|
|
@@ -122,9 +122,6 @@ export function type(value) {
|
|
|
122
122
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString | Object.toString }
|
|
123
123
|
*/
|
|
124
124
|
export const str = (obj) => (obj != null ? obj.toString() : '');
|
|
125
|
-
export function hasOwnProperty(obj, prop) {
|
|
126
|
-
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
127
|
-
}
|
|
128
125
|
/** Check if value is a boolean type. */
|
|
129
126
|
export const isBool = (x) => type(x) === 'boolean';
|
|
130
127
|
/** Check if value is an `Error` type. */
|
|
@@ -138,7 +135,7 @@ export const isIterable = (x) => {
|
|
|
138
135
|
/** Check if value is a native `Date` type. */
|
|
139
136
|
export const isDate = (x) => x instanceof Date;
|
|
140
137
|
/** Check if value is an object type. */
|
|
141
|
-
export const isObject = (x) =>
|
|
138
|
+
export const isObject = (x) => type(x) === 'object';
|
|
142
139
|
/** Check if value is a string type. */
|
|
143
140
|
export const isString = (x) => type(x) === 'string';
|
|
144
141
|
/** Check if value is an Array type. */
|
|
@@ -154,11 +151,9 @@ export const isArrayLike = (x) => {
|
|
|
154
151
|
/** Check if value is a number type. */
|
|
155
152
|
export const isNumber = (x) => type(x) === 'number';
|
|
156
153
|
/** Check if value is an integer number type. */
|
|
157
|
-
export const isInteger = (x) => type(x) === 'number' && Math.
|
|
154
|
+
export const isInteger = (x) => type(x) === 'number' && Math.trunc(x) === x;
|
|
158
155
|
/** Check if value is a bigint type. */
|
|
159
156
|
export const isBigint = (x) => type(x) === 'bigint';
|
|
160
|
-
/** Check if value is NaN based on `Number.isNaN`. */
|
|
161
|
-
export const isNaN = (x) => Number.isNaN(x);
|
|
162
157
|
/** Check if value is a Promise type. */
|
|
163
158
|
export const isPromise = (x) => type(x) === 'Promise';
|
|
164
159
|
/** Check if value is a function type. */
|
|
@@ -172,9 +167,7 @@ export const isGenerator = (x) => {
|
|
|
172
167
|
/** Check if value is `null` or `undefined`. */
|
|
173
168
|
export const isNull = (x) => x == null;
|
|
174
169
|
/** Check if value === `undefined`. */
|
|
175
|
-
export
|
|
176
|
-
return typeof o === 'undefined';
|
|
177
|
-
}
|
|
170
|
+
export const isUndefined = (x) => x === undefined;
|
|
178
171
|
/** Check if value is not `null` or `undefined`. */
|
|
179
172
|
export const notNull = (x) => x != null;
|
|
180
173
|
/** Returns `true` for objects without length or falsy values. */
|
|
@@ -222,10 +215,10 @@ export function len(value) {
|
|
|
222
215
|
* @example
|
|
223
216
|
```js
|
|
224
217
|
uniqueId()
|
|
225
|
-
//
|
|
218
|
+
// 3445556877
|
|
226
219
|
|
|
227
220
|
uniqueId('user_')
|
|
228
|
-
//
|
|
221
|
+
// 'user_1033763188'
|
|
229
222
|
```
|
|
230
223
|
*/
|
|
231
224
|
export const uniqueId = (pre = '') =>
|
|
@@ -237,7 +230,7 @@ export const uniqueId = (pre = '') =>
|
|
|
237
230
|
* @example
|
|
238
231
|
```js
|
|
239
232
|
uuid()
|
|
240
|
-
//
|
|
233
|
+
// '183f8ef3-b8f0-4000-81f5-89234c836d00'
|
|
241
234
|
```
|
|
242
235
|
*/
|
|
243
236
|
export const uuid = () => {
|
|
@@ -407,7 +400,7 @@ export const int = (x, base = 10) => parseInt(x, base);
|
|
|
407
400
|
*/
|
|
408
401
|
export const list = (value) => (value ? Array.from(value) : []);
|
|
409
402
|
/**
|
|
410
|
-
* Retrieve the next item from the iterator by calling
|
|
403
|
+
* Retrieve the next item from the iterator by calling its `next()` method.
|
|
411
404
|
* @param iter The iterator.
|
|
412
405
|
* @returns The next value from the iterator.
|
|
413
406
|
*/
|