atomic-fns 0.3.3 → 0.3.4
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 +17 -3
- package/core/collections/LRUCache.d.ts +22 -1
- package/core/collections/LRUCache.js +22 -1
- package/core/collections/LRUCache.ts +22 -1
- package/core/collections/SortedMap.d.ts +1 -1
- package/core/collections/SortedMap.js +1 -1
- package/core/collections/SortedMap.ts +1 -1
- package/core/collections/SortedSet.d.ts +6 -0
- package/core/collections/SortedSet.js +6 -0
- package/core/collections/SortedSet.ts +6 -0
- package/core/collections/SortedTree.d.ts +3 -3
- package/core/collections/SortedTree.js +3 -3
- package/core/collections/SortedTree.ts +3 -3
- package/core/collections/index.d.ts +2 -0
- package/core/collections/index.js +7 -5
- package/core/collections/index.ts +7 -5
- package/core/functools/index.d.ts +2 -2
- package/core/functools/index.ts +6 -3
- package/core/globals/index.d.ts +22 -3
- package/core/globals/index.js +28 -5
- package/core/globals/index.ts +39 -6
- package/core/i18n/date/format.d.ts +16 -0
- package/core/i18n/date/format.js +307 -0
- package/core/i18n/date/format.ts +343 -0
- package/core/i18n/date/index.d.ts +223 -0
- package/core/i18n/date/index.js +387 -0
- package/core/i18n/date/index.ts +471 -0
- package/core/i18n/date/test.html +67 -0
- package/core/i18n/index.d.ts +7 -0
- package/core/i18n/index.js +7 -0
- package/core/i18n/index.ts +8 -0
- package/core/index.d.ts +1 -0
- package/core/index.js +1 -0
- package/core/index.ts +1 -0
- package/core/operators/index.d.ts +1 -1
- package/core/operators/index.js +35 -5
- package/core/operators/index.ts +34 -6
- package/package.json +11 -11
- package/tests/globals.spec.ts +9 -9
- package/tsconfig.json +2 -1
- package/core/datetime/index.d.ts +0 -45
- package/core/datetime/index.js +0 -71
- package/core/datetime/index.ts +0 -128
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ You could also import any individual module directly from an html page like this
|
|
|
52
52
|
```html
|
|
53
53
|
<script type="module">
|
|
54
54
|
import {
|
|
55
|
-
namedtuple,
|
|
55
|
+
namedtuple, FrozenSet, SortedSet, SortedMap
|
|
56
56
|
} from 'https://unpkg.com/atomic-fns/core/collections';
|
|
57
57
|
// your code goes here...
|
|
58
58
|
</script>
|
|
@@ -66,7 +66,7 @@ This library features a data model similar to [Python](https://docs.python.org/3
|
|
|
66
66
|
For example in Python collections `size()` is a method while in JS its just a property. So this library uses `size` property instead of methods. Similarly, the `len` function implementation checks for both `length` and `size` since many JS objects have a `length` property.
|
|
67
67
|
|
|
68
68
|
## Special method names
|
|
69
|
-
This library adds support for operators
|
|
69
|
+
This library adds support for operators using object methods such as:
|
|
70
70
|
- `obj.eq(other)` adds custom `obj == other`.
|
|
71
71
|
- `obj.lt(other)` adds custom `obj < other`.
|
|
72
72
|
- `obj.lte(other)` adds custom `obj <= other`.
|
|
@@ -76,11 +76,25 @@ This library adds support for operators via object methods like:
|
|
|
76
76
|
|
|
77
77
|
When you implement those, you can also apply operators to values like `eq(x, y), lte(x, y), compare(x, y)`, etc. to get the result of `x.<operator>(y)`.
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
## New Types
|
|
80
|
+
|
|
81
|
+
Introduces some common base interfaces and typing patterns you may have seen like `Optional`, `Result`, `Comparable`, `Iterator`, and others.
|
|
82
|
+
|
|
83
|
+
### Sorted Containers
|
|
84
|
+
- [SortedSet](https://atomic-fns.dev/classes/Collections.SortedSet.html)
|
|
85
|
+
- [SortedMap](https://atomic-fns.dev/classes/Collections.SortedMap.html)
|
|
86
|
+
- [SortedTree](https://atomic-fns.dev/classes/Collections.SortedTree.html)
|
|
87
|
+
|
|
88
|
+
### Other Containers
|
|
89
|
+
- [Deque](https://atomic-fns.dev/classes/Collections.Deque.html)
|
|
90
|
+
- [FrozenSet](https://atomic-fns.dev/classes/Collections.FrozenSet.html)
|
|
91
|
+
- [LRUCache](https://atomic-fns.dev/classes/Collections.LRUCache.html)
|
|
92
|
+
|
|
80
93
|
|
|
81
94
|
## Contributing
|
|
82
95
|
If you want to contribute to the project and make it better, your help is very welcome. Contributing is also a great way to learn more about social coding on Github, new technologies and and their ecosystems and how to make constructive, helpful bug reports, feature requests and the noblest of all contributions: a good, clean pull request. You will be listed as a **Champion** on the official site as well.
|
|
83
96
|
|
|
97
|
+
|
|
84
98
|
## License
|
|
85
99
|
|
|
86
100
|
[Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0)
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { Iteratee } from '../globals/index.js';
|
|
2
2
|
import { Mapping } from './abc.js';
|
|
3
3
|
/**
|
|
4
|
-
* Base append-only Cache collection.
|
|
4
|
+
* Base append-only Cache collection that supports adding and accessing keys in O(1).
|
|
5
|
+
* The collection has a fixed given size (default 1024) and when full, it evicts keys using LRU approach. This operation is also O(1).
|
|
6
|
+
* @template K, V
|
|
5
7
|
*/
|
|
6
8
|
export declare class Cache<K, V = any> extends Mapping<K, V> {
|
|
7
9
|
capacity: number;
|
|
@@ -13,6 +15,12 @@ export declare class Cache<K, V = any> extends Mapping<K, V> {
|
|
|
13
15
|
protected backward: Uint8Array | Uint16Array | Uint32Array;
|
|
14
16
|
protected K: K[];
|
|
15
17
|
protected V: V[];
|
|
18
|
+
/**
|
|
19
|
+
* Create a new cache object of fixed size.
|
|
20
|
+
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
21
|
+
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
22
|
+
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
23
|
+
*/
|
|
16
24
|
constructor(capacity?: number, Keys?: ArrayConstructor, Values?: ArrayConstructor);
|
|
17
25
|
/**
|
|
18
26
|
* This is just an alias of {@link Cache.set}.
|
|
@@ -30,6 +38,9 @@ export declare class Cache<K, V = any> extends Mapping<K, V> {
|
|
|
30
38
|
* Method used to clear the structure.
|
|
31
39
|
*/
|
|
32
40
|
clear(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Returns the number of keys in the cache.
|
|
43
|
+
*/
|
|
33
44
|
get size(): number;
|
|
34
45
|
/**
|
|
35
46
|
* Method used to splay a value on top.
|
|
@@ -101,10 +112,20 @@ export declare class Cache<K, V = any> extends Mapping<K, V> {
|
|
|
101
112
|
*/
|
|
102
113
|
inspect(): Map<any, any>;
|
|
103
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Implements a Least Recently Used fixed-capacity cache which supports updating, removing, and accessing keys in O(1).
|
|
117
|
+
*/
|
|
104
118
|
export declare class LRUCache<K, V> extends Cache<K, V> {
|
|
105
119
|
deletedSize: number;
|
|
106
120
|
deleted: Uint8Array | Uint16Array | Uint32Array;
|
|
121
|
+
/**
|
|
122
|
+
* Create a new LRU cache object of fixed size.
|
|
123
|
+
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
124
|
+
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
125
|
+
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
126
|
+
*/
|
|
107
127
|
constructor(capacity?: number, Keys?: ArrayConstructor, Values?: ArrayConstructor);
|
|
128
|
+
/** Remove all elements in the cache. */
|
|
108
129
|
clear(): void;
|
|
109
130
|
/**
|
|
110
131
|
* Method used to set the value for the given key in the cache.
|
|
@@ -3,7 +3,9 @@ import { IterableIterator } from '../itertools/index.js';
|
|
|
3
3
|
import { Mapping } from './abc.js';
|
|
4
4
|
import { getPointerArray } from './typedArrays.js';
|
|
5
5
|
/**
|
|
6
|
-
* Base append-only Cache collection.
|
|
6
|
+
* Base append-only Cache collection that supports adding and accessing keys in O(1).
|
|
7
|
+
* The collection has a fixed given size (default 1024) and when full, it evicts keys using LRU approach. This operation is also O(1).
|
|
8
|
+
* @template K, V
|
|
7
9
|
*/
|
|
8
10
|
export class Cache extends Mapping {
|
|
9
11
|
capacity;
|
|
@@ -15,6 +17,12 @@ export class Cache extends Mapping {
|
|
|
15
17
|
backward;
|
|
16
18
|
K;
|
|
17
19
|
V;
|
|
20
|
+
/**
|
|
21
|
+
* Create a new cache object of fixed size.
|
|
22
|
+
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
23
|
+
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
24
|
+
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
25
|
+
*/
|
|
18
26
|
constructor(capacity = 1024, Keys = Array, Values = Array) {
|
|
19
27
|
super();
|
|
20
28
|
this.capacity = capacity;
|
|
@@ -53,6 +61,9 @@ export class Cache extends Mapping {
|
|
|
53
61
|
this.tail = 0;
|
|
54
62
|
this.items = new Map();
|
|
55
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Returns the number of keys in the cache.
|
|
66
|
+
*/
|
|
56
67
|
get size() {
|
|
57
68
|
return this.count;
|
|
58
69
|
}
|
|
@@ -264,14 +275,24 @@ export class Cache extends Mapping {
|
|
|
264
275
|
return this.inspect();
|
|
265
276
|
}
|
|
266
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* Implements a Least Recently Used fixed-capacity cache which supports updating, removing, and accessing keys in O(1).
|
|
280
|
+
*/
|
|
267
281
|
export class LRUCache extends Cache {
|
|
268
282
|
deletedSize = 0;
|
|
269
283
|
deleted;
|
|
284
|
+
/**
|
|
285
|
+
* Create a new LRU cache object of fixed size.
|
|
286
|
+
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
287
|
+
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
288
|
+
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
289
|
+
*/
|
|
270
290
|
constructor(capacity = 1024, Keys = Array, Values = Array) {
|
|
271
291
|
super(capacity, Keys, Values);
|
|
272
292
|
const PointerArray = getPointerArray(capacity);
|
|
273
293
|
this.deleted = new PointerArray(capacity);
|
|
274
294
|
}
|
|
295
|
+
/** Remove all elements in the cache. */
|
|
275
296
|
clear() {
|
|
276
297
|
super.clear();
|
|
277
298
|
this.deletedSize = 0;
|
|
@@ -4,7 +4,9 @@ import { Mapping } from './abc.js'
|
|
|
4
4
|
import { getPointerArray } from './typedArrays.js'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Base append-only Cache collection.
|
|
7
|
+
* Base append-only Cache collection that supports adding and accessing keys in O(1).
|
|
8
|
+
* The collection has a fixed given size (default 1024) and when full, it evicts keys using LRU approach. This operation is also O(1).
|
|
9
|
+
* @template K, V
|
|
8
10
|
*/
|
|
9
11
|
export class Cache<K, V = any> extends Mapping<K, V> {
|
|
10
12
|
capacity: number
|
|
@@ -17,6 +19,12 @@ export class Cache<K, V = any> extends Mapping<K, V> {
|
|
|
17
19
|
protected K: K[]
|
|
18
20
|
protected V: V[]
|
|
19
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Create a new cache object of fixed size.
|
|
24
|
+
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
25
|
+
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
26
|
+
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
27
|
+
*/
|
|
20
28
|
constructor(capacity = 1024, Keys = Array, Values = Array) {
|
|
21
29
|
super()
|
|
22
30
|
this.capacity = capacity
|
|
@@ -61,6 +69,9 @@ export class Cache<K, V = any> extends Mapping<K, V> {
|
|
|
61
69
|
this.items = new Map()
|
|
62
70
|
}
|
|
63
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Returns the number of keys in the cache.
|
|
74
|
+
*/
|
|
64
75
|
get size() {
|
|
65
76
|
return this.count
|
|
66
77
|
}
|
|
@@ -315,16 +326,26 @@ export class Cache<K, V = any> extends Mapping<K, V> {
|
|
|
315
326
|
}
|
|
316
327
|
}
|
|
317
328
|
|
|
329
|
+
/**
|
|
330
|
+
* Implements a Least Recently Used fixed-capacity cache which supports updating, removing, and accessing keys in O(1).
|
|
331
|
+
*/
|
|
318
332
|
export class LRUCache<K, V> extends Cache<K, V> {
|
|
319
333
|
deletedSize = 0
|
|
320
334
|
deleted: Uint8Array | Uint16Array | Uint32Array
|
|
321
335
|
|
|
336
|
+
/**
|
|
337
|
+
* Create a new LRU cache object of fixed size.
|
|
338
|
+
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
339
|
+
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
340
|
+
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
341
|
+
*/
|
|
322
342
|
constructor(capacity = 1024, Keys = Array, Values = Array) {
|
|
323
343
|
super(capacity, Keys, Values)
|
|
324
344
|
const PointerArray = getPointerArray(capacity)
|
|
325
345
|
this.deleted = new PointerArray(capacity)
|
|
326
346
|
}
|
|
327
347
|
|
|
348
|
+
/** Remove all elements in the cache. */
|
|
328
349
|
clear() {
|
|
329
350
|
super.clear()
|
|
330
351
|
this.deletedSize = 0
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Mapping } from './abc.js';
|
|
2
2
|
/**
|
|
3
|
-
* A `SortedMap` is a map that holds key value pairs
|
|
3
|
+
* A `SortedMap` is a map that holds key value pairs ordered by keys. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as keys or values.
|
|
4
4
|
* @see {@link Operators.compare}
|
|
5
5
|
* @see {@link Operators.lt}
|
|
6
6
|
* @template K, V
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Mapping } from './abc.js';
|
|
2
2
|
import { SplayTree } from './SplayTree.js';
|
|
3
3
|
/**
|
|
4
|
-
* A `SortedMap` is a map that holds key value pairs
|
|
4
|
+
* A `SortedMap` is a map that holds key value pairs ordered by keys. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as keys or values.
|
|
5
5
|
* @see {@link Operators.compare}
|
|
6
6
|
* @see {@link Operators.lt}
|
|
7
7
|
* @template K, V
|
|
@@ -2,7 +2,7 @@ import { Mapping } from './abc.js'
|
|
|
2
2
|
import { SplayTree } from './SplayTree.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* A `SortedMap` is a map that holds key value pairs
|
|
5
|
+
* A `SortedMap` is a map that holds key value pairs ordered by keys. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as keys or values.
|
|
6
6
|
* @see {@link Operators.compare}
|
|
7
7
|
* @see {@link Operators.lt}
|
|
8
8
|
* @template K, V
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { Collection } from './abc.js';
|
|
2
|
+
/**
|
|
3
|
+
* A `SortedSet` is a set that holds values in order. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as values.
|
|
4
|
+
* @see {@link Compare}
|
|
5
|
+
* @see {@link Ordered}
|
|
6
|
+
* @template K, V
|
|
7
|
+
*/
|
|
2
8
|
export declare class SortedSet<T> extends Collection {
|
|
3
9
|
private readonly store;
|
|
4
10
|
/**
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { Collection } from './abc.js';
|
|
2
2
|
import { SplayTree } from './SplayTree.js';
|
|
3
|
+
/**
|
|
4
|
+
* A `SortedSet` is a set that holds values in order. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as values.
|
|
5
|
+
* @see {@link Compare}
|
|
6
|
+
* @see {@link Ordered}
|
|
7
|
+
* @template K, V
|
|
8
|
+
*/
|
|
3
9
|
export class SortedSet extends Collection {
|
|
4
10
|
store;
|
|
5
11
|
/**
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { Collection } from './abc.js'
|
|
2
2
|
import { SplayTree } from './SplayTree.js'
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* A `SortedSet` is a set that holds values in order. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as values.
|
|
6
|
+
* @see {@link Compare}
|
|
7
|
+
* @see {@link Ordered}
|
|
8
|
+
* @template K, V
|
|
9
|
+
*/
|
|
4
10
|
export class SortedSet<T> extends Collection {
|
|
5
11
|
private readonly store: SplayTree<T>
|
|
6
12
|
/**
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Comparer } from '../operators/index.js';
|
|
2
2
|
import { Mapping } from './abc.js';
|
|
3
3
|
/**
|
|
4
|
-
* A `SortedTree` is a map that holds key value pairs
|
|
5
|
-
* @see {@link
|
|
6
|
-
* @see {@link
|
|
4
|
+
* A `SortedTree` is a map that holds key value pairs ordered by keys. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as keys or values.
|
|
5
|
+
* @see {@link Compare}
|
|
6
|
+
* @see {@link Ordered}
|
|
7
7
|
* @template K, V
|
|
8
8
|
*/
|
|
9
9
|
export declare class SortedTree<K, V> extends Mapping<K, V> {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Mapping } from './abc.js';
|
|
2
2
|
import { BTree } from './BTree.js';
|
|
3
3
|
/**
|
|
4
|
-
* A `SortedTree` is a map that holds key value pairs
|
|
5
|
-
* @see {@link
|
|
6
|
-
* @see {@link
|
|
4
|
+
* A `SortedTree` is a map that holds key value pairs ordered by keys. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as keys or values.
|
|
5
|
+
* @see {@link Compare}
|
|
6
|
+
* @see {@link Ordered}
|
|
7
7
|
* @template K, V
|
|
8
8
|
*/
|
|
9
9
|
export class SortedTree extends Mapping {
|
|
@@ -3,9 +3,9 @@ import { Mapping } from './abc.js'
|
|
|
3
3
|
import { BTree } from './BTree.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* A `SortedTree` is a map that holds key value pairs
|
|
7
|
-
* @see {@link
|
|
8
|
-
* @see {@link
|
|
6
|
+
* A `SortedTree` is a map that holds key value pairs ordered by keys. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as keys or values.
|
|
7
|
+
* @see {@link Compare}
|
|
8
|
+
* @see {@link Ordered}
|
|
9
9
|
* @template K, V
|
|
10
10
|
*/
|
|
11
11
|
export class SortedTree<K, V> extends Mapping<K, V> {
|
|
@@ -110,6 +110,8 @@ export declare function findRight(arr: any, fn: Iteratee | PropertyKey | Object)
|
|
|
110
110
|
/**
|
|
111
111
|
* Performs an efficient array insert operation in the given array. If the index or the array is invalid, it just returns the given array.
|
|
112
112
|
*
|
|
113
|
+
* **Note:** Uses the same behavior as `Array.splice`.
|
|
114
|
+
*
|
|
113
115
|
* @param {Array<*>} arr The given array to insert into
|
|
114
116
|
* @param {number} index The index of the array insert operation.
|
|
115
117
|
* @param {*} value The value to insert in the array at the given `index`.
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @module Collections
|
|
5
5
|
*/
|
|
6
6
|
import { partial } from '../functools/index.js';
|
|
7
|
-
import { call, get, isArray, isArrayLike, isBool, isEmpty,
|
|
7
|
+
import { call, get, isArray, isArrayLike, isBool, isEmpty, isFunction, isIterable, isNull, isNumber, isObject, isString, isSymbol, keys, set } from '../globals/index.js';
|
|
8
8
|
import { enumerate } from '../itertools/index.js';
|
|
9
9
|
import { compare, eq, id } from '../operators/index.js';
|
|
10
10
|
export * from './abc.js';
|
|
@@ -81,7 +81,7 @@ filter(users, 'active')
|
|
|
81
81
|
*/
|
|
82
82
|
export function filter(arr, fn = isNull) {
|
|
83
83
|
if (Array.isArray(arr)) {
|
|
84
|
-
if (
|
|
84
|
+
if (isFunction(fn))
|
|
85
85
|
return arr.filter(fn);
|
|
86
86
|
if (isNumber(fn) || isString(fn) || isSymbol(fn))
|
|
87
87
|
return arr.filter((x) => get(fn, x));
|
|
@@ -117,7 +117,7 @@ find(users, 'active')
|
|
|
117
117
|
*/
|
|
118
118
|
export function find(arr, fn) {
|
|
119
119
|
if (Array.isArray(arr)) {
|
|
120
|
-
if (
|
|
120
|
+
if (isFunction(fn))
|
|
121
121
|
return arr.find(fn);
|
|
122
122
|
if (isNumber(fn) || isString(fn) || isSymbol(fn))
|
|
123
123
|
return arr.find((x) => x?.[fn]);
|
|
@@ -159,6 +159,8 @@ export function findRight(arr, fn) {
|
|
|
159
159
|
/**
|
|
160
160
|
* Performs an efficient array insert operation in the given array. If the index or the array is invalid, it just returns the given array.
|
|
161
161
|
*
|
|
162
|
+
* **Note:** Uses the same behavior as `Array.splice`.
|
|
163
|
+
*
|
|
162
164
|
* @param {Array<*>} arr The given array to insert into
|
|
163
165
|
* @param {number} index The index of the array insert operation.
|
|
164
166
|
* @param {*} value The value to insert in the array at the given `index`.
|
|
@@ -642,7 +644,7 @@ export function clone(value, deep = false) {
|
|
|
642
644
|
return cloneArray(value, deep);
|
|
643
645
|
}
|
|
644
646
|
if (isObject(value)) {
|
|
645
|
-
if (
|
|
647
|
+
if (isFunction(value.clone)) {
|
|
646
648
|
return value.clone();
|
|
647
649
|
}
|
|
648
650
|
const copy = {};
|
|
@@ -974,7 +976,7 @@ export function remove(arr, func) {
|
|
|
974
976
|
return arr.filter((x, i) => {
|
|
975
977
|
if (isArray(func))
|
|
976
978
|
return !func.includes(x);
|
|
977
|
-
else if (
|
|
979
|
+
else if (isFunction(func))
|
|
978
980
|
return !func(x, i, arr);
|
|
979
981
|
return x !== func;
|
|
980
982
|
});
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
isArrayLike,
|
|
13
13
|
isBool,
|
|
14
14
|
isEmpty,
|
|
15
|
-
|
|
15
|
+
isFunction,
|
|
16
16
|
isIterable,
|
|
17
17
|
isNull,
|
|
18
18
|
isNumber,
|
|
@@ -114,7 +114,7 @@ filter(users, 'active')
|
|
|
114
114
|
*/
|
|
115
115
|
export function filter(arr, fn: Iteratee | PropertyKey | Object = isNull) {
|
|
116
116
|
if (Array.isArray(arr)) {
|
|
117
|
-
if (
|
|
117
|
+
if (isFunction(fn)) return arr.filter(fn)
|
|
118
118
|
if (isNumber(fn) || isString(fn) || isSymbol(fn)) return arr.filter((x) => get(fn, x))
|
|
119
119
|
if (isObject(fn)) return arr.filter(matches(fn))
|
|
120
120
|
}
|
|
@@ -148,7 +148,7 @@ find(users, 'active')
|
|
|
148
148
|
*/
|
|
149
149
|
export function find(arr, fn: Iteratee | PropertyKey | Object) {
|
|
150
150
|
if (Array.isArray(arr)) {
|
|
151
|
-
if (
|
|
151
|
+
if (isFunction(fn)) return arr.find(fn)
|
|
152
152
|
if (isNumber(fn) || isString(fn) || isSymbol(fn)) return arr.find((x) => x?.[fn])
|
|
153
153
|
if (isObject(fn)) return arr.find(matches(fn))
|
|
154
154
|
}
|
|
@@ -183,6 +183,8 @@ export function findRight(arr, fn: Iteratee | PropertyKey | Object) {
|
|
|
183
183
|
/**
|
|
184
184
|
* Performs an efficient array insert operation in the given array. If the index or the array is invalid, it just returns the given array.
|
|
185
185
|
*
|
|
186
|
+
* **Note:** Uses the same behavior as `Array.splice`.
|
|
187
|
+
*
|
|
186
188
|
* @param {Array<*>} arr The given array to insert into
|
|
187
189
|
* @param {number} index The index of the array insert operation.
|
|
188
190
|
* @param {*} value The value to insert in the array at the given `index`.
|
|
@@ -670,7 +672,7 @@ export function clone(value, deep = false) {
|
|
|
670
672
|
return cloneArray(value, deep)
|
|
671
673
|
}
|
|
672
674
|
if (isObject(value)) {
|
|
673
|
-
if (
|
|
675
|
+
if (isFunction(value.clone)) {
|
|
674
676
|
return value.clone()
|
|
675
677
|
}
|
|
676
678
|
const copy = {}
|
|
@@ -1013,7 +1015,7 @@ export function groupByMap<K = any, V = any>(
|
|
|
1013
1015
|
export function remove<T>(arr: T[], func: any) {
|
|
1014
1016
|
return arr.filter((x, i) => {
|
|
1015
1017
|
if (isArray(func)) return !func.includes(x)
|
|
1016
|
-
else if (
|
|
1018
|
+
else if (isFunction(func)) return !func(x, i, arr)
|
|
1017
1019
|
return x !== func
|
|
1018
1020
|
})
|
|
1019
1021
|
}
|
|
@@ -47,7 +47,7 @@ export declare const compose: (...args: any[]) => (x: any) => any;
|
|
|
47
47
|
* @param {Function} [resolver] The function to resolve the cache key.
|
|
48
48
|
* @return {Function} Returns the new memoized function.
|
|
49
49
|
*/
|
|
50
|
-
export declare function memoize(func: Function
|
|
50
|
+
export declare function memoize<T>(func: Function<T>, resolver?: Function): Function<ReturnType<typeof func>>;
|
|
51
51
|
export declare function cache(target: any, name: any, descriptor: any): any;
|
|
52
52
|
/**
|
|
53
53
|
* 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.
|
|
@@ -84,7 +84,7 @@ single(3) // => 1
|
|
|
84
84
|
// => `func` is invoked only once.
|
|
85
85
|
```
|
|
86
86
|
*/
|
|
87
|
-
export declare function once(func: Function): (...args: any[]) =>
|
|
87
|
+
export declare function once<T>(func: Function<T>): (...args: any[]) => T;
|
|
88
88
|
export declare type Maybe<T> = T | undefined;
|
|
89
89
|
export declare type Result<T, E = any> = [Maybe<T>, Maybe<E>];
|
|
90
90
|
/**
|
package/core/functools/index.ts
CHANGED
|
@@ -98,7 +98,10 @@ export const compose = (...args) => flow(args.reverse())
|
|
|
98
98
|
* @param {Function} [resolver] The function to resolve the cache key.
|
|
99
99
|
* @return {Function} Returns the new memoized function.
|
|
100
100
|
*/
|
|
101
|
-
export function memoize(
|
|
101
|
+
export function memoize<T>(
|
|
102
|
+
func: Function<T>,
|
|
103
|
+
resolver?: Function
|
|
104
|
+
): Function<ReturnType<typeof func>> {
|
|
102
105
|
const cache = new Map()
|
|
103
106
|
return (...args) => {
|
|
104
107
|
const key = resolver ? resolver(...args) : args[0]
|
|
@@ -188,14 +191,14 @@ single(3) // => 1
|
|
|
188
191
|
// => `func` is invoked only once.
|
|
189
192
|
```
|
|
190
193
|
*/
|
|
191
|
-
export function once(func: Function) {
|
|
194
|
+
export function once<T>(func: Function<T>) {
|
|
192
195
|
let result, isCalled
|
|
193
196
|
return (...args) => {
|
|
194
197
|
if (!isCalled) {
|
|
195
198
|
result = func(...args)
|
|
196
199
|
isCalled = true
|
|
197
200
|
}
|
|
198
|
-
return result
|
|
201
|
+
return result as T
|
|
199
202
|
}
|
|
200
203
|
}
|
|
201
204
|
|
package/core/globals/index.d.ts
CHANGED
|
@@ -92,10 +92,15 @@ 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;
|
|
95
96
|
/** Check if value is a boolean type. */
|
|
96
97
|
export declare const isBool: (x: any) => x is boolean;
|
|
98
|
+
/** Check if value is an `Error` type. */
|
|
99
|
+
export declare const isError: (x: any) => x is Error;
|
|
97
100
|
/** Check if value is an iterable type. */
|
|
98
101
|
export declare const isIterable: (x: any) => x is IterableIterator<any>;
|
|
102
|
+
/** Check if value is a native `Date` type. */
|
|
103
|
+
export declare const isDate: (x: any) => x is Date;
|
|
99
104
|
/** Check if value is an object type. */
|
|
100
105
|
export declare const isObject: (x: any) => x is Object;
|
|
101
106
|
/** Check if value is a string type. */
|
|
@@ -107,28 +112,42 @@ export declare const isArray: (x: any) => x is any[];
|
|
|
107
112
|
* A value is considered array-like if it's not a function and has a `.length` number property.
|
|
108
113
|
*/
|
|
109
114
|
export declare const isArrayLike: (x: any) => boolean;
|
|
110
|
-
/** Check if value is a function type. */
|
|
111
|
-
export declare const isFunc: (x: any) => x is Function<any>;
|
|
112
115
|
/** Check if value is a number type. */
|
|
113
116
|
export declare const isNumber: (x: any) => x is number;
|
|
117
|
+
/** Check if value is an integer number type. */
|
|
118
|
+
export declare const isInteger: (x: any) => x is number;
|
|
114
119
|
/** Check if value is a bigint type. */
|
|
115
120
|
export declare const isBigint: (x: any) => x is BigInt;
|
|
116
121
|
/** Check if value is NaN based on `Number.isNaN`. */
|
|
117
122
|
export declare const isNaN: (x: any) => x is number;
|
|
118
123
|
/** Check if value is a Promise type. */
|
|
119
124
|
export declare const isPromise: (x: any) => x is Promise<any>;
|
|
125
|
+
/** Check if value is a function type. */
|
|
126
|
+
export declare const isFunction: (x: any) => x is Function<any>;
|
|
120
127
|
/** Check if value is an async function type. */
|
|
121
|
-
export declare const
|
|
128
|
+
export declare const isAsyncFunction: (x: any) => boolean;
|
|
122
129
|
/** Check if value is a generator function type. */
|
|
123
130
|
export declare const isGenerator: (x: any) => x is Generator<unknown, any, unknown>;
|
|
124
131
|
/** Check if value is `null` or `undefined`. */
|
|
125
132
|
export declare const isNull: (x: any) => x is null | undefined;
|
|
133
|
+
/** Check if value === `undefined`. */
|
|
134
|
+
export declare function isUndefined(o: any): boolean;
|
|
126
135
|
/** Check if value is not `null` or `undefined`. */
|
|
127
136
|
export declare const notNull: (x: any) => boolean;
|
|
128
137
|
/** Returns `true` for objects without length or falsy values. */
|
|
129
138
|
export declare const isEmpty: (x: any) => boolean;
|
|
139
|
+
/** Check if value is a `RegExp` type */
|
|
140
|
+
export declare const isRegExp: (x: any) => x is RegExp;
|
|
130
141
|
/** Check if value is a `Symbol` type */
|
|
131
142
|
export declare const isSymbol: (x: any) => x is symbol;
|
|
143
|
+
/** Check if value is a `Set` type. */
|
|
144
|
+
export declare const isSet: <T>(x: any) => x is Set<T>;
|
|
145
|
+
/** Check if value is a `Map` type. */
|
|
146
|
+
export declare const isMap: <K, V>(x: any) => x is Map<K, V>;
|
|
147
|
+
/** Check if value is a `WeakSet` type. */
|
|
148
|
+
export declare const isWeakSet: <T extends object>(x: any) => x is WeakSet<T>;
|
|
149
|
+
/** Check if value is a `WeakMap` type. */
|
|
150
|
+
export declare const isWeakMap: <K extends object, V>(x: any) => x is WeakMap<K, V>;
|
|
132
151
|
/**
|
|
133
152
|
* Returns the number of elements in a collection type.
|
|
134
153
|
*
|
package/core/globals/index.js
CHANGED
|
@@ -122,14 +122,21 @@ 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
|
+
}
|
|
125
128
|
/** Check if value is a boolean type. */
|
|
126
129
|
export const isBool = (x) => type(x) === 'boolean';
|
|
130
|
+
/** Check if value is an `Error` type. */
|
|
131
|
+
export const isError = (x) => x instanceof Error;
|
|
127
132
|
/** Check if value is an iterable type. */
|
|
128
133
|
export const isIterable = (x) => {
|
|
129
134
|
if (x == null)
|
|
130
135
|
return false;
|
|
131
136
|
return typeof x[Symbol.iterator] === 'function';
|
|
132
137
|
};
|
|
138
|
+
/** Check if value is a native `Date` type. */
|
|
139
|
+
export const isDate = (x) => x instanceof Date;
|
|
133
140
|
/** Check if value is an object type. */
|
|
134
141
|
export const isObject = (x) => typeof x === 'object' && type(x) === 'object';
|
|
135
142
|
/** Check if value is a string type. */
|
|
@@ -144,30 +151,46 @@ export const isArrayLike = (x) => {
|
|
|
144
151
|
const T = type(x);
|
|
145
152
|
return T === 'array' || (T === 'object' && isNumber(x.length));
|
|
146
153
|
};
|
|
147
|
-
/** Check if value is a function type. */
|
|
148
|
-
export const isFunc = (x) => x?.constructor === Function;
|
|
149
154
|
/** Check if value is a number type. */
|
|
150
155
|
export const isNumber = (x) => type(x) === 'number';
|
|
156
|
+
/** Check if value is an integer number type. */
|
|
157
|
+
export const isInteger = (x) => type(x) === 'number' && Math.floor(x) === x;
|
|
151
158
|
/** Check if value is a bigint type. */
|
|
152
159
|
export const isBigint = (x) => type(x) === 'bigint';
|
|
153
160
|
/** Check if value is NaN based on `Number.isNaN`. */
|
|
154
161
|
export const isNaN = (x) => Number.isNaN(x);
|
|
155
162
|
/** Check if value is a Promise type. */
|
|
156
163
|
export const isPromise = (x) => type(x) === 'Promise';
|
|
164
|
+
/** Check if value is a function type. */
|
|
165
|
+
export const isFunction = (x) => x?.constructor === Function;
|
|
157
166
|
/** Check if value is an async function type. */
|
|
158
|
-
export const
|
|
167
|
+
export const isAsyncFunction = (x) => x?.constructor.name === 'AsyncFunction';
|
|
159
168
|
/** Check if value is a generator function type. */
|
|
160
169
|
export const isGenerator = (x) => {
|
|
161
170
|
return x?.constructor.constructor?.name === 'GeneratorFunction';
|
|
162
171
|
};
|
|
163
172
|
/** Check if value is `null` or `undefined`. */
|
|
164
173
|
export const isNull = (x) => x == null;
|
|
174
|
+
/** Check if value === `undefined`. */
|
|
175
|
+
export function isUndefined(o) {
|
|
176
|
+
return typeof o === 'undefined';
|
|
177
|
+
}
|
|
165
178
|
/** Check if value is not `null` or `undefined`. */
|
|
166
179
|
export const notNull = (x) => x != null;
|
|
167
180
|
/** Returns `true` for objects without length or falsy values. */
|
|
168
181
|
export const isEmpty = (x) => (len(x) === 0 ? true : !x);
|
|
182
|
+
/** Check if value is a `RegExp` type */
|
|
183
|
+
export const isRegExp = (x) => x instanceof RegExp;
|
|
169
184
|
/** Check if value is a `Symbol` type */
|
|
170
185
|
export const isSymbol = (x) => type(x) === 'symbol';
|
|
186
|
+
/** Check if value is a `Set` type. */
|
|
187
|
+
export const isSet = (x) => x instanceof Set;
|
|
188
|
+
/** Check if value is a `Map` type. */
|
|
189
|
+
export const isMap = (x) => x instanceof Map;
|
|
190
|
+
/** Check if value is a `WeakSet` type. */
|
|
191
|
+
export const isWeakSet = (x) => x instanceof WeakSet;
|
|
192
|
+
/** Check if value is a `WeakMap` type. */
|
|
193
|
+
export const isWeakMap = (x) => x instanceof WeakMap;
|
|
171
194
|
/**
|
|
172
195
|
* Returns the number of elements in a collection type.
|
|
173
196
|
*
|
|
@@ -182,7 +205,7 @@ export function len(value) {
|
|
|
182
205
|
if (isNumber(value.length)) {
|
|
183
206
|
return value.length;
|
|
184
207
|
}
|
|
185
|
-
if (
|
|
208
|
+
if (isFunction(value.size)) {
|
|
186
209
|
return value.size();
|
|
187
210
|
}
|
|
188
211
|
if (isNumber(value.size)) {
|
|
@@ -229,7 +252,7 @@ export const uuid = () => {
|
|
|
229
252
|
* @return `obj.key(...args)` or `undefined`.
|
|
230
253
|
*/
|
|
231
254
|
export function call(obj, key, ...args) {
|
|
232
|
-
if (
|
|
255
|
+
if (isFunction(get(key, obj))) {
|
|
233
256
|
return obj[key](...args);
|
|
234
257
|
}
|
|
235
258
|
}
|