atomic-fns 0.3.1 → 0.3.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.
Files changed (87) hide show
  1. package/.esdoc.json +2 -2
  2. package/README.md +48 -13
  3. package/core/collections/BTree.d.ts +229 -0
  4. package/core/collections/BTree.js +1165 -0
  5. package/core/collections/BTree.ts +1340 -0
  6. package/core/collections/Heap.d.ts +69 -0
  7. package/core/collections/Heap.js +181 -0
  8. package/core/collections/Heap.ts +189 -0
  9. package/core/collections/LRUCache.d.ts +131 -0
  10. package/core/collections/LRUCache.js +388 -0
  11. package/core/collections/LRUCache.ts +465 -0
  12. package/core/collections/SortedMap.d.ts +92 -0
  13. package/core/collections/SortedMap.js +130 -0
  14. package/core/collections/SortedMap.ts +146 -0
  15. package/core/collections/SortedSet.d.ts +63 -0
  16. package/core/collections/SortedSet.js +93 -0
  17. package/core/collections/SortedSet.ts +104 -0
  18. package/core/collections/SortedTree.d.ts +103 -0
  19. package/core/collections/SortedTree.js +141 -0
  20. package/core/collections/SortedTree.ts +160 -0
  21. package/core/collections/SplayTree.d.ts +105 -0
  22. package/core/collections/SplayTree.js +338 -0
  23. package/core/collections/SplayTree.ts +354 -0
  24. package/core/collections/abc.d.ts +152 -0
  25. package/core/collections/abc.js +39 -0
  26. package/core/collections/abc.ts +176 -0
  27. package/core/collections/deque.d.ts +110 -0
  28. package/core/collections/deque.js +285 -0
  29. package/core/collections/deque.ts +329 -0
  30. package/core/collections/frozenset.d.ts +19 -0
  31. package/core/collections/frozenset.js +33 -0
  32. package/core/collections/frozenset.ts +37 -0
  33. package/{src → core}/collections/index.d.ts +41 -162
  34. package/{src → core}/collections/index.js +57 -165
  35. package/{src → core}/collections/index.ts +61 -225
  36. package/core/collections/test.html +10 -0
  37. package/core/collections/typedArrays.d.ts +31 -0
  38. package/core/collections/typedArrays.js +143 -0
  39. package/core/collections/typedArrays.ts +163 -0
  40. package/core/datetime/index.d.ts +45 -0
  41. package/core/datetime/index.js +71 -0
  42. package/core/datetime/index.ts +128 -0
  43. package/{src → core}/functools/index.d.ts +2 -2
  44. package/{src → core}/functools/index.js +2 -2
  45. package/{src → core}/functools/index.ts +2 -2
  46. package/{src → core}/globals/index.d.ts +2 -7
  47. package/{src → core}/globals/index.js +5 -2
  48. package/{src → core}/globals/index.ts +6 -9
  49. package/{src → core}/index.d.ts +0 -1
  50. package/{src → core}/index.js +0 -1
  51. package/{src → core}/index.ts +0 -1
  52. package/{src → core}/itertools/index.d.ts +9 -7
  53. package/{src → core}/itertools/index.js +25 -9
  54. package/{src → core}/itertools/index.ts +39 -16
  55. package/{src → core}/math/index.d.ts +0 -0
  56. package/{src → core}/math/index.js +0 -0
  57. package/{src → core}/math/index.ts +0 -0
  58. package/core/operators/index.d.ts +148 -0
  59. package/core/operators/index.js +271 -0
  60. package/core/operators/index.ts +282 -0
  61. package/{src → core}/random/index.d.ts +0 -0
  62. package/{src → core}/random/index.js +0 -0
  63. package/{src → core}/random/index.ts +0 -0
  64. package/package.json +6 -6
  65. package/tests/BTree.spec.ts +143 -0
  66. package/tests/Cache.spec.ts +161 -0
  67. package/tests/Heap.spec.ts +66 -0
  68. package/tests/SortedMap.spec.ts +144 -0
  69. package/tests/SortedSet.spec.ts +84 -0
  70. package/tests/SortedTree.spec.ts +164 -0
  71. package/tests/SplayTree.spec.ts +121 -0
  72. package/tests/collections.spec.ts +1 -1
  73. package/tests/deque.spec.ts +107 -0
  74. package/tests/globals.spec.ts +2 -2
  75. package/tests/itertools.spec.ts +2 -2
  76. package/tests/math.spec.ts +1 -1
  77. package/tests/operators.spec.ts +5 -5
  78. package/tests/random.spec.ts +10 -10
  79. package/tsconfig.json +10 -10
  80. package/tsconfig.node.json +2 -2
  81. package/tsconfig.xo.json +2 -2
  82. package/src/datetime/index.d.ts +0 -6
  83. package/src/datetime/index.js +0 -6
  84. package/src/datetime/index.ts +0 -7
  85. package/src/operators/index.d.ts +0 -31
  86. package/src/operators/index.js +0 -171
  87. package/src/operators/index.ts +0 -175
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Returns a new immutable Set object with elements from `iterable`. Its contents cannot be altered after it's created.
3
+ *
4
+ *
5
+ * @class FrozenSet
6
+ * @extends {Set<T>}
7
+ * @template T
8
+ */
9
+ export declare class FrozenSet<T = any> extends Set<T> {
10
+ constructor(iterable: Iterable<T>);
11
+ /** @private */
12
+ add(): this;
13
+ /** @private */
14
+ delete(): boolean;
15
+ /** @private */
16
+ clear(): this;
17
+ /** @private */
18
+ freeze(): Readonly<this>;
19
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Returns a new immutable Set object with elements from `iterable`. Its contents cannot be altered after it's created.
3
+ *
4
+ *
5
+ * @class FrozenSet
6
+ * @extends {Set<T>}
7
+ * @template T
8
+ */
9
+ export class FrozenSet extends Set {
10
+ constructor(iterable) {
11
+ super(iterable);
12
+ return this.freeze();
13
+ }
14
+ /** @private */
15
+ add() {
16
+ throw TypeError('FrozenSet cannot be modified.');
17
+ return this; // eslint-disable-line
18
+ }
19
+ /** @private */
20
+ delete() {
21
+ throw TypeError('FrozenSet cannot be modified.');
22
+ return false; // eslint-disable-line
23
+ }
24
+ /** @private */
25
+ clear() {
26
+ throw TypeError('FrozenSet cannot be modified.');
27
+ return this; // eslint-disable-line
28
+ }
29
+ /** @private */
30
+ freeze() {
31
+ return Object.freeze(this);
32
+ }
33
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Returns a new immutable Set object with elements from `iterable`. Its contents cannot be altered after it's created.
3
+ *
4
+ *
5
+ * @class FrozenSet
6
+ * @extends {Set<T>}
7
+ * @template T
8
+ */
9
+ export class FrozenSet<T = any> extends Set<T> {
10
+ constructor(iterable: Iterable<T>) {
11
+ super(iterable)
12
+ return this.freeze()
13
+ }
14
+
15
+ /** @private */
16
+ add() {
17
+ throw TypeError('FrozenSet cannot be modified.')
18
+ return this // eslint-disable-line
19
+ }
20
+
21
+ /** @private */
22
+ delete() {
23
+ throw TypeError('FrozenSet cannot be modified.')
24
+ return false // eslint-disable-line
25
+ }
26
+
27
+ /** @private */
28
+ clear() {
29
+ throw TypeError('FrozenSet cannot be modified.')
30
+ return this // eslint-disable-line
31
+ }
32
+
33
+ /** @private */
34
+ freeze() {
35
+ return Object.freeze(this)
36
+ }
37
+ }
@@ -4,167 +4,18 @@
4
4
  * @module Collections
5
5
  */
6
6
  import { Iteratee } from '../globals/index.js';
7
- /**
8
- * A container type that provides contains().
9
- *
10
- *
11
- * @interface Container
12
- */
13
- export interface Container {
14
- contains: (x: any) => boolean;
15
- }
16
- /**
17
- * These are the "rich comparison" methods, inspired by Python operators.
18
- *
19
- *
20
- * @interface Comparable
21
- */
22
- export interface Comparable {
23
- /** x < y: calls this.lt(other) reflection. */
24
- lt?: (other: any) => boolean;
25
- /** x ≤ y: calls this.le(other) reflection. */
26
- le?: (other: any) => boolean;
27
- /** x == y: calls this.eq(other) reflection. */
28
- eq: (other: any) => boolean;
29
- /** x != y: calls this.ne(other) reflection. */
30
- ne: (other: any) => boolean;
31
- /** x > y: calls this.gt(other) reflection. */
32
- gt?: (other: any) => boolean;
33
- /** x ≥ y: calls this.ge(other) reflection. */
34
- ge?: (other: any) => boolean;
35
- }
36
- /**
37
- * A `Collection` is an iterable {@link Container} type.
38
- * This is an abstract base class for user-defined collection types.
39
- *
40
- *
41
- * @abstract
42
- * @class Collection
43
- * @implements {Container}
44
- */
45
- export declare abstract class Collection implements Container {
46
- /**
47
- * Returns an iterator for the container items
48
- * @return {Iterator}
49
- */
50
- [Symbol.iterator](): this;
51
- /**
52
- * Implements the iterator protocol and returns the next item in the iterable.
53
- */
54
- next(): void;
55
- /**
56
- * Adds a new item to the container.
57
- * @param item - The item to add to the collection.
58
- */
59
- add(item: any): void;
60
- /**
61
- * Checks if item is present in the container.
62
- * @param item - The item to search for in the collection.
63
- * @returns `true` when the element is found, else `false`
64
- */
65
- contains(item: any): boolean;
66
- /**
67
- * Returns the total number of elements in the container.
68
- */
69
- size(): number;
70
- /**
71
- * Remove the first item from the container where `item == x`
72
- * @returns `true` when the element is found and removed, else `false`
73
- */
74
- remove(x: any): boolean;
75
- /**
76
- * Retrieve and remove the item at index `i`.
77
- * @returns the item or undefined if not found.
78
- */
79
- pop(i: any): void;
80
- /**
81
- * Remove all items from the container
82
- */
83
- clear(): void;
84
- }
85
- /**
86
- * Implements an iterable that allows backward iteration.
87
- * @interface Reversible
88
- */
89
- export interface Reversible<T = unknown> {
90
- reversed: () => Iterator<T>;
91
- }
92
- /**
93
- * A sequence is an iterable {@link Collection} type with efficient index-based access.
94
- * @abstract
95
- * @class Sequence
96
- * @extends {Collection}
97
- * @template T
98
- */
99
- export declare abstract class Sequence<T> extends Collection implements Reversible<T> {
100
- /**
101
- * Return the item at the given key or index
102
- * @param {*} key
103
- * @memberof Sequence
104
- */
105
- get(key: any): T | undefined;
106
- /**
107
- * Set a new value at the given key or index
108
- *
109
- * @param {*} key
110
- * @param {T} val
111
- * @memberof Sequence
112
- */
113
- set(key: any, val: T): void;
114
- /**
115
- * Deletes the given key or index, and its value.
116
- * Raises ValueError if not key is found.
117
- *
118
- * @param {*} key
119
- * @memberof Sequence
120
- */
121
- delete(key: any): void;
122
- /**
123
- * Adds a new item to the end of the sequence.
124
- *
125
- * @param {T} x
126
- * @memberof Sequence
127
- */
128
- append(x: T): void;
129
- /**
130
- * Append all the items to the sequence.
131
- *
132
- * @param {Iterable<T>} iter
133
- * @memberof Sequence
134
- */
135
- extend(iter: Iterable<T>): void;
136
- /**
137
- * Return the index of x in the sequence, or undefined if not found.
138
- *
139
- * @param {T} item
140
- * @return {(number | undefined)}
141
- * @memberof Sequence
142
- */
143
- indexOf(item: T): number | undefined;
144
- size(): number;
145
- reversed(): Iterator<T>;
146
- }
147
- /**
148
- * Returns a new immutable Set object with elements from `iterable`. Its contents cannot be altered after it's created.
149
- *
150
- *
151
- * @class FrozenSet
152
- * @extends {Set<T>}
153
- * @template T
154
- */
155
- export declare class FrozenSet<T = any> extends Set<T> {
156
- constructor(iterable: Iterable<T>);
157
- /** @private */
158
- add(): this;
159
- /** @private */
160
- delete(): boolean;
161
- /** @private */
162
- clear(): this;
163
- /** @private */
164
- freeze(): Readonly<this>;
165
- }
166
- /**
167
- * Creates a new array with all `falsey` and empty values removed.
7
+ export * from './abc.js';
8
+ export * from './BTree.js';
9
+ export * from './deque.js';
10
+ export * from './frozenset.js';
11
+ export * from './Heap.js';
12
+ export * from './LRUCache.js';
13
+ export * from './SortedSet.js';
14
+ export * from './SortedMap.js';
15
+ export * from './SortedTree.js';
16
+ export * from './SplayTree.js';
17
+ /**
18
+ * Creates a new array with all falsy and empty values removed.
168
19
  * @param arr The array to compact
169
20
  * @returns A new array with the filtered values.
170
21
  * @example
@@ -256,6 +107,15 @@ findRight([1, 2, 3, 4], (n) => n % 2 === 1)
256
107
  * @see {@link find}
257
108
  */
258
109
  export declare function findRight(arr: any, fn: Iteratee | PropertyKey | Object): any;
110
+ /**
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
+ *
113
+ * @param {Array<*>} arr The given array to insert into
114
+ * @param {number} index The index of the array insert operation.
115
+ * @param {*} value The value to insert in the array at the given `index`.
116
+ * @returns {Array<*>} The given array.
117
+ */
118
+ export declare function insert(arr: any[], index: number, value: any): any[];
259
119
  /**
260
120
  * Creates a function that performs a partial deep comparison between a given object and `shape`, returning `true` if the given object has equivalent property values, else `false`.
261
121
  * @example
@@ -650,7 +510,7 @@ export declare function union(...args: Array<Iterable<any>>): Generator<unknown,
650
510
  *
651
511
  * @param arr The collection to iterate over.
652
512
  * @param [func=id] The iteratee to transform keys.
653
- * @returns Returns the composed aggregate object.
513
+ * @returns Returns the aggregated object.
654
514
  *
655
515
  * @example
656
516
  ```js
@@ -663,6 +523,25 @@ groupBy(['one', 'two', 'three'], 'length')
663
523
  ```
664
524
  */
665
525
  export declare function groupBy(arr: Iterable<any> | Object, func?: Iteratee | PropertyKey): Object;
526
+ /**
527
+ * Similar to {@link groupBy} but it returns a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map Map} object with the results.
528
+ *
529
+ * @param arr The collection to iterate over.
530
+ * @param {Function | PropertyKey} [func=id] The iteratee to transform keys.
531
+ * @returns {Map} Returns the aggregated map object.
532
+ * @template K, V
533
+ *
534
+ * @example
535
+ ```js
536
+ groupByMap([6.1, 4.2, 6.3], Math.floor)
537
+ // => Map { 4: [4.2], 6: [6.1, 6.3] }
538
+
539
+ // The `property` iteratee shorthand.
540
+ groupByMap(['one', 'two', 'three'], 'length')
541
+ // => Map { 3: ['one', 'two'], 5: ['three'] }
542
+ ```
543
+ */
544
+ export declare function groupByMap<K = any, V = any>(arr: Iterable<any> | Object, func?: Iteratee | PropertyKey): Map<K, V[]>;
666
545
  /**
667
546
  * Removes all elements from array that `func` returns truthy for and returns an array of the removed elements.
668
547
  * @param arr The array to remove from.
@@ -4,166 +4,19 @@
4
4
  * @module Collections
5
5
  */
6
6
  import { partial } from '../functools/index.js';
7
- import { call, get, isArray, isArrayLike, isBool, isEmpty, isFunc, isIterable, isNull, isNumber, isObject, isString, isSymbol, keys, NotImplementedError, set } from '../globals/index.js';
7
+ import { call, get, isArray, isArrayLike, isBool, isEmpty, isFunc, isIterable, isNull, isNumber, isObject, isString, isSymbol, keys, set } from '../globals/index.js';
8
8
  import { enumerate } from '../itertools/index.js';
9
- import { comp, eq, id } from '../operators/index.js';
10
- /**
11
- * A `Collection` is an iterable {@link Container} type.
12
- * This is an abstract base class for user-defined collection types.
13
- *
14
- *
15
- * @abstract
16
- * @class Collection
17
- * @implements {Container}
18
- */
19
- export class Collection {
20
- /**
21
- * Returns an iterator for the container items
22
- * @return {Iterator}
23
- */
24
- [Symbol.iterator]() {
25
- return this;
26
- }
27
- /**
28
- * Implements the iterator protocol and returns the next item in the iterable.
29
- */
30
- next() { }
31
- /**
32
- * Adds a new item to the container.
33
- * @param item - The item to add to the collection.
34
- */
35
- add(item) { }
36
- /**
37
- * Checks if item is present in the container.
38
- * @param item - The item to search for in the collection.
39
- * @returns `true` when the element is found, else `false`
40
- */
41
- contains(item) {
42
- return false;
43
- }
44
- /**
45
- * Returns the total number of elements in the container.
46
- */
47
- size() {
48
- throw new NotImplementedError();
49
- }
50
- /**
51
- * Remove the first item from the container where `item == x`
52
- * @returns `true` when the element is found and removed, else `false`
53
- */
54
- remove(x) {
55
- return false;
56
- }
57
- /**
58
- * Retrieve and remove the item at index `i`.
59
- * @returns the item or undefined if not found.
60
- */
61
- pop(i) { }
62
- /**
63
- * Remove all items from the container
64
- */
65
- clear() { }
66
- }
67
- /**
68
- * A sequence is an iterable {@link Collection} type with efficient index-based access.
69
- * @abstract
70
- * @class Sequence
71
- * @extends {Collection}
72
- * @template T
73
- */
74
- export class Sequence extends Collection {
75
- /**
76
- * Return the item at the given key or index
77
- * @param {*} key
78
- * @memberof Sequence
79
- */
80
- get(key) {
81
- throw new NotImplementedError();
82
- }
83
- /**
84
- * Set a new value at the given key or index
85
- *
86
- * @param {*} key
87
- * @param {T} val
88
- * @memberof Sequence
89
- */
90
- set(key, val) {
91
- throw new NotImplementedError();
92
- }
93
- /**
94
- * Deletes the given key or index, and its value.
95
- * Raises ValueError if not key is found.
96
- *
97
- * @param {*} key
98
- * @memberof Sequence
99
- */
100
- delete(key) {
101
- throw new NotImplementedError();
102
- }
103
- /**
104
- * Adds a new item to the end of the sequence.
105
- *
106
- * @param {T} x
107
- * @memberof Sequence
108
- */
109
- append(x) { }
110
- /**
111
- * Append all the items to the sequence.
112
- *
113
- * @param {Iterable<T>} iter
114
- * @memberof Sequence
115
- */
116
- extend(iter) { }
117
- /**
118
- * Return the index of x in the sequence, or undefined if not found.
119
- *
120
- * @param {T} item
121
- * @return {(number | undefined)}
122
- * @memberof Sequence
123
- */
124
- indexOf(item) {
125
- throw new NotImplementedError();
126
- }
127
- size() {
128
- return 0;
129
- }
130
- reversed() {
131
- throw new NotImplementedError();
132
- }
133
- }
134
- /**
135
- * Returns a new immutable Set object with elements from `iterable`. Its contents cannot be altered after it's created.
136
- *
137
- *
138
- * @class FrozenSet
139
- * @extends {Set<T>}
140
- * @template T
141
- */
142
- export class FrozenSet extends Set {
143
- constructor(iterable) {
144
- super(iterable);
145
- return this.freeze();
146
- }
147
- /** @private */
148
- add() {
149
- throw TypeError('FrozenSet cannot be modified.');
150
- return this; // eslint-disable-line
151
- }
152
- /** @private */
153
- delete() {
154
- throw TypeError('FrozenSet cannot be modified.');
155
- return false; // eslint-disable-line
156
- }
157
- /** @private */
158
- clear() {
159
- throw TypeError('FrozenSet cannot be modified.');
160
- return this; // eslint-disable-line
161
- }
162
- /** @private */
163
- freeze() {
164
- return Object.freeze(this);
165
- }
166
- }
9
+ import { compare, eq, id } from '../operators/index.js';
10
+ export * from './abc.js';
11
+ export * from './BTree.js';
12
+ export * from './deque.js';
13
+ export * from './frozenset.js';
14
+ export * from './Heap.js';
15
+ export * from './LRUCache.js';
16
+ export * from './SortedSet.js';
17
+ export * from './SortedMap.js';
18
+ export * from './SortedTree.js';
19
+ export * from './SplayTree.js';
167
20
  export function compact(arr) {
168
21
  if (arr == null)
169
22
  return;
@@ -303,6 +156,20 @@ export function findRight(arr, fn) {
303
156
  }
304
157
  }
305
158
  }
159
+ /**
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
+ *
162
+ * @param {Array<*>} arr The given array to insert into
163
+ * @param {number} index The index of the array insert operation.
164
+ * @param {*} value The value to insert in the array at the given `index`.
165
+ * @returns {Array<*>} The given array.
166
+ */
167
+ export function insert(arr, index, value) {
168
+ if (!arr || arr.length <= index || index < 0)
169
+ return arr;
170
+ arr.splice(index, 0, value);
171
+ return arr;
172
+ }
306
173
  /**
307
174
  * Creates a function that performs a partial deep comparison between a given object and `shape`, returning `true` if the given object has equivalent property values, else `false`.
308
175
  * @example
@@ -852,7 +719,7 @@ uniq([2, 1, 2])
852
719
  */
853
720
  export function sortedUniq(arr, fn = id) {
854
721
  const values = uniq(arr, fn);
855
- values.sort(comp);
722
+ values.sort(compare);
856
723
  return values;
857
724
  }
858
725
  function baseMergeDeep(obj, source, key, stack) {
@@ -1045,7 +912,7 @@ export function* union(...args) {
1045
912
  *
1046
913
  * @param arr The collection to iterate over.
1047
914
  * @param [func=id] The iteratee to transform keys.
1048
- * @returns Returns the composed aggregate object.
915
+ * @returns Returns the aggregated object.
1049
916
  *
1050
917
  * @example
1051
918
  ```js
@@ -1068,6 +935,35 @@ export function groupBy(arr, func = id) {
1068
935
  });
1069
936
  return results;
1070
937
  }
938
+ /**
939
+ * Similar to {@link groupBy} but it returns a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map Map} object with the results.
940
+ *
941
+ * @param arr The collection to iterate over.
942
+ * @param {Function | PropertyKey} [func=id] The iteratee to transform keys.
943
+ * @returns {Map} Returns the aggregated map object.
944
+ * @template K, V
945
+ *
946
+ * @example
947
+ ```js
948
+ groupByMap([6.1, 4.2, 6.3], Math.floor)
949
+ // => Map { 4: [4.2], 6: [6.1, 6.3] }
950
+
951
+ // The `property` iteratee shorthand.
952
+ groupByMap(['one', 'two', 'three'], 'length')
953
+ // => Map { 3: ['one', 'two'], 5: ['three'] }
954
+ ```
955
+ */
956
+ export function groupByMap(arr, func = id) {
957
+ const results = new Map();
958
+ const useKey = typeof func === 'function' ? func : partial(get, func);
959
+ forEach(arr, (value) => {
960
+ const groupKey = useKey(value);
961
+ const values = results.get(groupKey) || [];
962
+ values.push(value);
963
+ results.set(groupKey, values);
964
+ });
965
+ return results;
966
+ }
1071
967
  /**
1072
968
  * Removes all elements from array that `func` returns truthy for and returns an array of the removed elements.
1073
969
  * @param arr The array to remove from.
@@ -1084,8 +980,4 @@ export function remove(arr, func) {
1084
980
  });
1085
981
  }
1086
982
  // TODO: binary search utils
1087
- // TODO: heap/heapify
1088
- // TODO: deque
1089
983
  // TODO: Counter
1090
- // TODO: Skip list
1091
- // TODO: BST (BTree)