atomic-fns 0.3.1 → 0.3.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.
Files changed (85) 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 +12 -161
  34. package/{src → core}/collections/index.js +13 -164
  35. package/{src → core}/collections/index.ts +13 -224
  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/{src → core}/datetime/index.d.ts +0 -0
  41. package/{src → core}/datetime/index.js +0 -0
  42. package/{src → core}/datetime/index.ts +0 -0
  43. package/core/datetime/test.html +0 -0
  44. package/{src → core}/functools/index.d.ts +2 -2
  45. package/{src → core}/functools/index.js +2 -2
  46. package/{src → core}/functools/index.ts +2 -2
  47. package/{src → core}/globals/index.d.ts +2 -7
  48. package/{src → core}/globals/index.js +5 -2
  49. package/{src → core}/globals/index.ts +6 -9
  50. package/{src → core}/index.d.ts +0 -0
  51. package/{src → core}/index.js +0 -0
  52. package/{src → core}/index.ts +0 -0
  53. package/{src → core}/itertools/index.d.ts +9 -7
  54. package/{src → core}/itertools/index.js +25 -9
  55. package/{src → core}/itertools/index.ts +39 -16
  56. package/{src → core}/math/index.d.ts +0 -0
  57. package/{src → core}/math/index.js +0 -0
  58. package/{src → core}/math/index.ts +0 -0
  59. package/core/operators/index.d.ts +148 -0
  60. package/core/operators/index.js +271 -0
  61. package/core/operators/index.ts +282 -0
  62. package/{src → core}/random/index.d.ts +0 -0
  63. package/{src → core}/random/index.js +0 -0
  64. package/{src → core}/random/index.ts +0 -0
  65. package/package.json +6 -6
  66. package/tests/BTree.spec.ts +143 -0
  67. package/tests/Cache.spec.ts +161 -0
  68. package/tests/Heap.spec.ts +66 -0
  69. package/tests/SortedMap.spec.ts +144 -0
  70. package/tests/SortedSet.spec.ts +84 -0
  71. package/tests/SortedTree.spec.ts +164 -0
  72. package/tests/SplayTree.spec.ts +121 -0
  73. package/tests/collections.spec.ts +1 -1
  74. package/tests/deque.spec.ts +107 -0
  75. package/tests/globals.spec.ts +2 -2
  76. package/tests/itertools.spec.ts +2 -2
  77. package/tests/math.spec.ts +1 -1
  78. package/tests/operators.spec.ts +5 -5
  79. package/tests/random.spec.ts +10 -10
  80. package/tsconfig.json +10 -10
  81. package/tsconfig.node.json +2 -2
  82. package/tsconfig.xo.json +2 -2
  83. package/src/operators/index.d.ts +0 -31
  84. package/src/operators/index.js +0 -171
  85. package/src/operators/index.ts +0 -175
package/.esdoc.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "source": "./src",
2
+ "source": "./lib",
3
3
  "destination": "./docs",
4
4
  "plugins": [
5
5
  {
@@ -24,4 +24,4 @@
24
24
  }
25
25
  }
26
26
  ]
27
- }
27
+ }
package/README.md CHANGED
@@ -4,47 +4,82 @@
4
4
 
5
5
  ## Like Lodash, but for ESNext and with types.
6
6
 
7
- Stop shipping code built for browsers from 2015. Really though, look at the [issues](https://github.com/lodash/lodash/issues/2930).
7
+ Stop shipping code built for browsers from *2015*. Really though, look at all the [issues](https://github.com/lodash/lodash/issues/2930).
8
8
 
9
9
  You may prefer this because it's:
10
10
 
11
- * **Modular**: Pick what you need. Works with all bundlers and also supports tree-shaking.
11
+ * **Modular**: Pick what you need. Supports tree-shaking, no side effects and works with all bundlers.
12
12
  * **ESNext**: Uses modern idiomatic syntax, data structures, and control flow techniques.
13
13
  * **Lazy**: Leverage generators, iterators, and functional composition.
14
- * **Zero deps**: Built from scratch with no dependencies whatsoever.
14
+ * **Zero deps**: Built from scratch with no runtime dependencies or polyfills.
15
15
  * **Well tested**: All modules have comprehensive test suites aiming for 100% coverage.
16
16
  * **TypeScript & docs**: Includes type declaration files and js with JSDocs for best in class experience.
17
17
 
18
+ ## Docs
19
+
20
+ 📖 Available at [atomic-fns.dev](https://atomic-fns.dev)
21
+
18
22
  ## Get Started
19
23
 
20
- This library is available as an npm package. To install the package just run:
24
+ This library is available as an npm package.
25
+
26
+ To install the package you need to have [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) (or [yarn](https://yarnpkg.com/getting-started/install)) and then run:
21
27
 
22
28
  ```bash
23
29
  npm install atomic-fns
24
30
  ```
25
31
 
26
32
  ## How to use
27
- ```typescript
28
- import { reversed, sorted, times, uniqueId } from 'atomic-fns'
29
33
 
30
- reversed([1, 2, 3, 4])
31
- // -> [4, 3, 2, 1]
34
+ Just import any function or class you need.
32
35
 
33
- sorted([4, 3, 2, 1])
34
- // -> [1, 2, 3, 4]
36
+ ```js
37
+ import { sorted, times, uniqueId } from 'atomic-fns'
35
38
 
36
39
  times(5, (i) => i)
37
40
  // -> [0, 1, 2, 3, 4]
38
41
 
42
+ sorted([4, 3, 2, 1])
43
+ // -> [1, 2, 3, 4]
44
+
39
45
  uniqueId('user_')
40
46
  // -> 'user_101225005'
47
+ ...
41
48
  ```
42
49
 
43
- ## Docs
50
+ You could also import any individual module directly from an html page like this:
51
+
52
+ ```html
53
+ <script type="module">
54
+ import {
55
+ namedtuple, Deque, Heap, SortedSet, SortedMap
56
+ } from 'https://unpkg.com/atomic-fns/core/collections';
57
+ // your code goes here...
58
+ </script>
59
+ ```
60
+
61
+ See the full list of [modules](https://atomic-fns.dev/modules.html).
62
+
63
+ ## Data model
64
+ This library features a data model similar to [Python](https://docs.python.org/3/reference/datamodel.html#special-method-names) but not always. Only the missing parts have been implemented, while respecting the JS standards.
65
+
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
+
68
+ ## Special method names
69
+ This library adds support for operators via object methods like:
70
+ - `obj.eq(other)` adds custom `obj == other`.
71
+ - `obj.lt(other)` adds custom `obj < other`.
72
+ - `obj.lte(other)` adds custom `obj <= other`.
73
+ - `obj.gt(other)` adds custom `obj > other`.
74
+ - `obj.gte(other)` adds custom `obj >= other`.
75
+ - `obj.compare(other)` overloads sorting operations.
76
+
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)`.
44
78
 
45
- 📖 Available on [atomic-fns.dev](https://atomic-fns.dev)
79
+ Some new base types introduced: `Collection, Sequence, Mapping, FrozenSet, IterableIterator`.
46
80
 
47
- 💡 See the `src` directory for comments.
81
+ ## Contributing
82
+ 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.
48
83
 
49
84
  ## License
50
85
 
@@ -0,0 +1,229 @@
1
+ import { Iteratee } from '../globals/index.js';
2
+ import { Comparer } from '../operators/index.js';
3
+ import { Mapping } from './abc.js';
4
+ export declare class BTree<K = any, V = any> extends Mapping<K, V> {
5
+ private root;
6
+ count: number;
7
+ _maxNodeSize: number;
8
+ /**
9
+ * provides a total order over keys (and a strict partial order over the type K)
10
+ * @returns a negative value if a < b, 0 if a === b and a positive value if a > b
11
+ */
12
+ _compare: Comparer;
13
+ /**
14
+ * Initializes an empty B+ tree.
15
+ * @param {?Iterable<[K, V]>} [entries] The initial key value pairs.
16
+ * @param compareFn Custom function to compare pairs of elements in the tree.
17
+ * If not specified, defaultComparator will be used which is valid as long as K extends DefaultComparable.
18
+ * @param entries A set of key-value pairs to initialize the tree
19
+ * @param {number} [maxNodeSize=64] Branching factor (maximum items or children per node)
20
+ * Must be in range 4..256. If undefined or <4 then default is used; if >256 then 256.
21
+ */
22
+ constructor(entries?: Iterable<[K, V]>, compareFn?: Comparer, maxNodeSize?: number);
23
+ /** Gets the number of key-value pairs in the tree. */
24
+ get size(): number;
25
+ /** Releases the tree so that its size is 0. */
26
+ clear(): void;
27
+ /**
28
+ * Finds a pair in the tree and returns the associated value.
29
+ * @param defaultValue a value to return if the key was not found.
30
+ * @returns the value, or defaultValue if the key was not found.
31
+ * @description Computational complexity: O(log size)
32
+ */
33
+ get(key: K, defaultValue?: V): V | undefined;
34
+ /**
35
+ * Adds or overwrites a key-value pair in the B+ tree.
36
+ * @param key the key is used to determine the sort order of
37
+ * data in the tree.
38
+ * @param value data to associate with the key (optional)
39
+ * @returns true if a new key-value pair was added.
40
+ * @description Computational complexity: O(log size)
41
+ * Note: when overwriting a previous entry, the key is updated
42
+ * as well as the value. This has no effect unless the new key
43
+ * has data that does not affect its sort order.
44
+ */
45
+ set(key: K, value: V): boolean;
46
+ add(key: K): boolean;
47
+ /**
48
+ * Returns true if the key exists in the B+ tree, false if not.
49
+ * @param key Key to detect
50
+ * @description Computational complexity: O(log size)
51
+ */
52
+ contains(key: K): boolean;
53
+ /**
54
+ * Removes a single key-value pair from the B+ tree.
55
+ * @param key Key to find
56
+ * @returns true if a pair was found and removed, false otherwise.
57
+ * @description Computational complexity: O(log size)
58
+ */
59
+ remove(key: K): boolean;
60
+ /** Returns an iterator that provides items in order (ascending order if
61
+ * the collection's comparator uses ascending order, as is the default.)
62
+ * @param lowestKey First key to be iterated, or undefined to start at
63
+ * minKey(). If the specified key doesn't exist then iteration
64
+ * starts at the next higher key (according to the comparator).
65
+ * @param reusedArray Optional array used repeatedly to store key-value
66
+ * pairs, to avoid creating a new array on every iteration.
67
+ */
68
+ entries(lowestKey?: K, reusedArray?: Array<K | V>): IterableIterator<[K, V]>;
69
+ /** Returns an iterator that provides items in reversed order.
70
+ * @param highestKey Key at which to start iterating, or undefined to
71
+ * start at maxKey(). If the specified key doesn't exist then iteration
72
+ * starts at the next lower key (according to the comparator).
73
+ * @param reusedArray Optional array used repeatedly to store key-value
74
+ * pairs, to avoid creating a new array on every iteration.
75
+ * @param skipHighest Iff this flag is true and the highestKey exists in the
76
+ * collection, the pair matching highestKey is skipped, not iterated.
77
+ */
78
+ reversed(highestKey?: K, reusedArray?: Array<K | V>, skipHighest?: boolean): IterableIterator<[K, V]>;
79
+ private findPath;
80
+ /** Returns a new iterator for iterating the keys of each pair in ascending order.
81
+ * @param firstKey: Minimum key to include in the output. */
82
+ keys(firstKey?: K): IterableIterator<K>;
83
+ /** Gets the lowest key in the tree. Complexity: O(log size) */
84
+ minKey(): K | undefined;
85
+ /** Gets the highest key in the tree. Complexity: O(1) */
86
+ maxKey(): K;
87
+ /** Returns a new iterator for iterating the values of each pair in order by key.
88
+ * @param firstKey: Minimum key whose associated value is included in the output. */
89
+ values(firstKey?: K): IterableIterator<V>;
90
+ /** Returns the maximum number of children/values before nodes will split. */
91
+ get maxNodeSize(): number;
92
+ /** Performs a deep cloning of the tree, immediately duplicating any nodes that are
93
+ * not currently marked as shared, in order to avoid marking any
94
+ * additional nodes as shared.
95
+ * @param force Clone all nodes, even shared ones.
96
+ */
97
+ clone(force?: boolean): BTree<K, V>;
98
+ /** Gets an array filled with the contents of the tree, sorted by key */
99
+ toArray(): Array<[K, V]>;
100
+ /** Gets a string representing the tree's data based on toArray(). */
101
+ toString(): string;
102
+ /** Returns the next pair whose key is larger than the specified key (or undefined if there is none).
103
+ * If key === undefined, this function returns the lowest pair.
104
+ * @param key The key to search for.
105
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
106
+ * avoid creating a new array on every iteration.
107
+ */
108
+ lowerBound(key: K | undefined, reusedArray?: [K, V]): [K, V] | undefined;
109
+ /** Returns the next key larger than the specified key, or undefined if there is none.
110
+ * Also, nextHigherKey(undefined) returns the lowest key.
111
+ */
112
+ /** Returns the next pair whose key is smaller than the specified key (or undefined if there is none).
113
+ * If key === undefined, this function returns the highest pair.
114
+ * @param key The key to search for.
115
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
116
+ * avoid creating a new array each time you call this method.
117
+ */
118
+ upperBound(key: K | undefined, reusedArray?: [K, V]): [K, V] | undefined;
119
+ /** Returns the next key smaller than the specified key, or undefined if there is none.
120
+ * Also, nextLowerKey(undefined) returns the highest key.
121
+ */
122
+ /** Returns the key-value pair associated with the supplied key if it exists
123
+ * or the pair associated with the next lower pair otherwise. If there is no
124
+ * next lower pair, undefined is returned.
125
+ * @param key The key to search for.
126
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
127
+ * avoid creating a new array each time you call this method.
128
+ * */
129
+ floor(key: K, reusedArray?: [K, V]): [K, V] | undefined;
130
+ /** Returns the key-value pair associated with the supplied key if it exists
131
+ * or the pair associated with the next lower pair otherwise. If there is no
132
+ * next lower pair, undefined is returned.
133
+ * @param key The key to search for.
134
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
135
+ * avoid creating a new array each time you call this method.
136
+ * */
137
+ ceiling(key: K, reusedArray?: [K, V]): [K, V] | undefined;
138
+ /**
139
+ * Builds an array of pairs from the specified range of keys, sorted by key.
140
+ * Each returned pair is also an array: pair[0] is the key, pair[1] is the value.
141
+ * @param low The first key in the array will be greater than or equal to `low`.
142
+ * @param high This method returns when a key larger than this is reached.
143
+ * @param includeHigh If the `high` key is present, its pair will be included
144
+ * in the output if and only if this parameter is true. Note: if the
145
+ * `low` key is present, it is always included in the output.
146
+ * @param maxLength Length limit. getRange will stop scanning the tree when
147
+ * the array reaches this size.
148
+ * @description Computational complexity: O(result.length + log size)
149
+ */
150
+ keysRange(low: K, high: K, includeHigh?: boolean, maxLength?: number): Array<[K, V]>;
151
+ /** Adds all pairs from a list of key-value pairs.
152
+ * @param entries Pairs to add to this tree. If there are duplicate keys,
153
+ * later pairs currently overwrite earlier ones (e.g. [[0,1],[0,7]]
154
+ * associates 0 with 7.)
155
+ * @returns The number of pairs added to the collection.
156
+ * @description Computational complexity: O(pairs.length * log(size + pairs.length))
157
+ */
158
+ extend(entries: Iterable<[K, V]>): number;
159
+ /**
160
+ * Scans the specified range of keys, in ascending order by key.
161
+ * Note: the callback `onFound` must not insert or remove items in the
162
+ * collection. Doing so may cause incorrect data to be sent to the
163
+ * callback afterward.
164
+ * @param low The first key scanned will be greater than or equal to `low`.
165
+ * @param high Scanning stops when a key larger than this is reached.
166
+ * @param includeHigh If the `high` key is present, `onFound` is called for
167
+ * that final pair if and only if this parameter is true.
168
+ * @param onFound A function that is called for each key-value pair. This
169
+ * function can return {break:R} to stop early with result R.
170
+ * @param initialCounter Initial third argument of onFound. This value
171
+ * increases by one each time `onFound` is called. Default: 0
172
+ * @returns The number of values found, or R if the callback returned
173
+ * `{break:R}` to stop early.
174
+ * @description Computational complexity: O(number of items scanned + log size)
175
+ */
176
+ rangeForEach<R = number>(low: K, high: K, includeHigh: boolean, onFound?: Iteratee<K, V>, initialCounter?: number): R | number;
177
+ /**
178
+ * Scans and potentially modifies values for a subsequence of keys.
179
+ * Note: the callback `onFound` should ideally be a pure function.
180
+ * Specifically, it must not insert items, call clone(), or change
181
+ * the collection except via return value; out-of-band editing may
182
+ * cause an exception or may cause incorrect data to be sent to
183
+ * the callback (duplicate or missed items). It must not cause a
184
+ * clone() of the collection, otherwise the clone could be modified
185
+ * by changes requested by the callback.
186
+ * @param low The first key scanned will be greater than or equal to `low`.
187
+ * @param high Scanning stops when a key larger than this is reached.
188
+ * @param includeHigh If the `high` key is present, `onFound` is called for
189
+ * that final pair if and only if this parameter is true.
190
+ * @param onFound A function that is called for each key-value pair. This
191
+ * function can return `{value:v}` to change the value associated
192
+ * with the current key, `{delete:true}` to delete the current pair,
193
+ * `{break:R}` to stop early with result R, or it can return nothing
194
+ * (undefined or {}) to cause no effect and continue iterating.
195
+ * `{break:R}` can be combined with one of the other two commands.
196
+ * The third argument `counter` is the number of items iterated
197
+ * previously; it equals 0 when `onFound` is called the first time.
198
+ * @returns The number of values scanned, or R if the callback returned
199
+ * `{break:R}` to stop early.
200
+ * @description
201
+ * Computational complexity: O(number of items scanned + log size)
202
+ * Note: if the tree has been cloned with clone(), any shared
203
+ * nodes are copied before `onFound` is called. This takes O(n) time
204
+ * where n is proportional to the amount of shared data scanned.
205
+ */
206
+ rangeUpdate<R = V>(low: K, high: K, includeHigh: boolean, onFound: (k: K, v: V | undefined, counter: number) => any, initialCounter?: number): R | number;
207
+ /**
208
+ * Removes a range of key-value pairs from the B+ tree.
209
+ * @param low The first key scanned will be greater than or equal to `low`.
210
+ * @param high Scanning stops when a key larger than this is reached.
211
+ * @param includeHigh Specifies whether the `high` key, if present, is deleted.
212
+ * @returns The number of key-value pairs that were deleted.
213
+ * @description Computational complexity: O(log size + number of items deleted)
214
+ */
215
+ removeRange(low: K, high: K, includeHigh: boolean): number;
216
+ /** Removes a series of keys from the collection. */
217
+ removeKeys(keys: K[]): number;
218
+ /** Gets the height of the tree: the number of internal nodes between the
219
+ * BTree object and its leaf nodes (zero if there are no internal nodes). */
220
+ get height(): number;
221
+ /** Makes the object read-only to ensure it is not accidentally modified.
222
+ * Freezing does not have to be permanent; unfreeze() reverses the effect.
223
+ * This is accomplished by replacing mutator functions with a function
224
+ * that throws an Error. Compared to using a property (e.g. this.isFrozen)
225
+ * this implementation gives better performance in non-frozen BTrees.
226
+ */
227
+ freeze(): void;
228
+ [Symbol.iterator](): IterableIterator<[K, V]>;
229
+ }