atomic-fns 0.4.2 → 1.1.1

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 (39) hide show
  1. package/README.md +6 -17
  2. package/dist/collections/BTree.d.ts +1 -1
  3. package/dist/collections/Trie.d.ts +42 -0
  4. package/dist/collections/Trie.js +104 -0
  5. package/dist/collections/deque.d.ts +2 -2
  6. package/dist/collections/deque.js +6 -8
  7. package/dist/collections/index.d.ts +208 -106
  8. package/dist/collections/index.js +179 -436
  9. package/dist/decimal/index.d.ts +147 -0
  10. package/dist/decimal/index.js +376 -0
  11. package/dist/functools/index.d.ts +26 -18
  12. package/dist/functools/index.js +36 -50
  13. package/dist/globals/decimal.d.ts +142 -0
  14. package/dist/globals/decimal.js +371 -0
  15. package/dist/globals/index.d.ts +19 -31
  16. package/dist/globals/index.js +9 -24
  17. package/dist/i18n/currency.d.ts +24 -0
  18. package/dist/i18n/currency.js +85 -0
  19. package/dist/i18n/date/format.d.ts +16 -0
  20. package/dist/i18n/date/format.js +313 -0
  21. package/dist/i18n/date/index.d.ts +9 -9
  22. package/dist/i18n/date/test.html +72 -0
  23. package/dist/i18n/date/utils.d.ts +3 -3
  24. package/dist/i18n/date/utils.js +6 -28
  25. package/dist/i18n/format.d.ts +2 -2
  26. package/dist/i18n/format.js +1 -1
  27. package/dist/i18n/index.d.ts +1 -0
  28. package/dist/i18n/index.js +1 -0
  29. package/dist/itertools/index.d.ts +52 -24
  30. package/dist/itertools/index.js +4 -32
  31. package/dist/math/index.d.ts +7 -8
  32. package/dist/math/index.js +3 -1
  33. package/dist/operators/index.d.ts +2 -2
  34. package/dist/operators/index.js +0 -6
  35. package/dist/random/index.d.ts +1 -1
  36. package/dist/random/index.js +2 -2
  37. package/dist/types.d.ts +19 -0
  38. package/dist/types.js +1 -0
  39. package/package.json +1 -1
package/README.md CHANGED
@@ -2,18 +2,18 @@
2
2
 
3
3
  [Read the Docs](https://atomic-fns.dev)
4
4
 
5
- ## Like Lodash, but for ESNext and with types.
5
+ ## Like Lodash, but in TypeScript and for ESnext.
6
6
 
7
- Stop shipping code built for browsers from *2015*. Really though, look at all 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
+ * **TypeScript & docs**: Includes type declaration files and js with JSDocs for best in class experience.
12
+ * **Zero deps**: Built from scratch with no runtime dependencies or polyfills.
13
+ * **ESnext**: Uses modern idiomatic syntax, data structures, and control flow techniques.
11
14
  * **Modular**: Pick what you need. Supports tree-shaking, no side effects and works with all bundlers.
12
- * **ESNext**: Uses modern idiomatic syntax, data structures, and control flow techniques.
13
15
  * **Lazy**: Leverage generators, iterators, and functional composition.
14
- * **Zero deps**: Built from scratch with no runtime dependencies or polyfills.
15
- * **Well tested**: All modules have comprehensive test suites aiming for 100% coverage.
16
- * **TypeScript & docs**: Includes type declaration files and js with JSDocs for best in class experience.
16
+ * **Well tested**: All modules have comprehensive test suites and public apis are 100% coverage.
17
17
 
18
18
  ## Docs
19
19
 
@@ -47,17 +47,6 @@ uniqueId('user_')
47
47
  ...
48
48
  ```
49
49
 
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, FrozenSet, SortedSet, SortedMap
56
- } from 'https://unpkg.com/atomic-fns/collections';
57
- // your code goes here...
58
- </script>
59
- ```
60
-
61
50
  See the full list of [modules](https://atomic-fns.dev/modules.html).
62
51
 
63
52
  ## Data model
@@ -1,6 +1,6 @@
1
- import { Iteratee } from '../globals/index.js';
2
1
  import { Comparer } from '../operators/index.js';
3
2
  import { Mapping } from './abc.js';
3
+ import { Iteratee } from '../types';
4
4
  export declare class BTree<K = any, V = any> extends Mapping<K, V> {
5
5
  private root;
6
6
  count: number;
@@ -0,0 +1,42 @@
1
+ import { Collection } from './abc.js';
2
+ /**
3
+ * A Trie is a tree for efficient storing and retrieval of strings such as words in a dictionary. They are useful for checking all the strings that have a common prefix.
4
+ * Insertions, lookups and removals in `O(s)` where `s` is the search term.
5
+ * @see {@link https://en.wikipedia.org/wiki/Trie Trie}
6
+ */
7
+ export declare class Trie extends Collection {
8
+ private root;
9
+ private count;
10
+ constructor(words?: string[]);
11
+ /**
12
+ * Adds the given string to the tree.
13
+ * @param word A string
14
+ */
15
+ add(word: string): this;
16
+ /**
17
+ * Removes the given string from the tree.
18
+ * @param word The string to remove
19
+ * @returns {boolean} Returns `true` if the string was found and removed.
20
+ */
21
+ remove(word: string): boolean;
22
+ private findNode;
23
+ /**
24
+ * Generates all results that start with the given string.
25
+ * @param word A prefix string
26
+ * @param {number} [limit=20] The number of results to return.
27
+ */
28
+ matches: (word: string, limit?: number) => Generator<any, void, any>;
29
+ /**
30
+ * Returns `true` if the given string is found in the tree.
31
+ * @param {string} word
32
+ */
33
+ contains(word: string): boolean;
34
+ /**
35
+ * Removes all strings from the tree.
36
+ */
37
+ clear(): void;
38
+ /**
39
+ * Returns the total number of strings in the tree.
40
+ */
41
+ get size(): number;
42
+ }
@@ -0,0 +1,104 @@
1
+ import { Collection } from './abc.js';
2
+ function* traverse(node, path, total = 20, count = 0) {
3
+ if (node.isWord) {
4
+ count++;
5
+ yield path;
6
+ }
7
+ for (const key of Object.keys(node)) {
8
+ yield* traverse(node[key], path + key, total, count);
9
+ }
10
+ }
11
+ /**
12
+ * A Trie is a tree for efficient storing and retrieval of strings such as words in a dictionary. They are useful for checking all the strings that have a common prefix.
13
+ * Insertions, lookups and removals in `O(s)` where `s` is the search term.
14
+ * @see {@link https://en.wikipedia.org/wiki/Trie Trie}
15
+ */
16
+ export class Trie extends Collection {
17
+ root = {};
18
+ count = 0;
19
+ constructor(words) {
20
+ super();
21
+ if (words) {
22
+ for (const word of words) {
23
+ this.add(word);
24
+ }
25
+ }
26
+ }
27
+ /**
28
+ * Adds the given string to the tree.
29
+ * @param word A string
30
+ */
31
+ add(word) {
32
+ let current = this.root;
33
+ for (let i = 0; i < word.length; i++) {
34
+ const c = word[i];
35
+ if (!current[c])
36
+ current[c] = {};
37
+ current = current[c];
38
+ }
39
+ // Set as end of the word
40
+ Object.defineProperty(current, 'isWord', {
41
+ value: true,
42
+ enumerable: false,
43
+ writable: true
44
+ });
45
+ this.count++;
46
+ return this;
47
+ }
48
+ /**
49
+ * Removes the given string from the tree.
50
+ * @param word The string to remove
51
+ * @returns {boolean} Returns `true` if the string was found and removed.
52
+ */
53
+ remove(word) {
54
+ for (let i = 0, node = this.root; i < word.length && node; i++) {
55
+ node = node[word[i]];
56
+ if (i === word.length - 1 && node.isWord) {
57
+ node.isWord = false;
58
+ this.count--;
59
+ return true;
60
+ }
61
+ }
62
+ return false;
63
+ }
64
+ findNode(word) {
65
+ let node = this.root;
66
+ for (let i = 0; i < word.length && node; i++) {
67
+ const letter = word[i];
68
+ node = node[letter];
69
+ }
70
+ return node;
71
+ }
72
+ /**
73
+ * Generates all results that start with the given string.
74
+ * @param word A prefix string
75
+ * @param {number} [limit=20] The number of results to return.
76
+ */
77
+ matches = function* (word, limit = 20) {
78
+ const node = this.findNode(word);
79
+ if (node) {
80
+ yield* traverse(node, word, limit);
81
+ }
82
+ };
83
+ /**
84
+ * Returns `true` if the given string is found in the tree.
85
+ * @param {string} word
86
+ */
87
+ contains(word) {
88
+ const node = this.findNode(word);
89
+ return node?.isWord;
90
+ }
91
+ /**
92
+ * Removes all strings from the tree.
93
+ */
94
+ clear() {
95
+ this.root = {};
96
+ this.count = 0;
97
+ }
98
+ /**
99
+ * Returns the total number of strings in the tree.
100
+ */
101
+ get size() {
102
+ return this.count;
103
+ }
104
+ }
@@ -1,4 +1,4 @@
1
- import { Iteratee } from '../globals/index.js';
1
+ import { Iteratee } from '../types.js';
2
2
  import { Sequence } from './abc.js';
3
3
  /**
4
4
  * Fixed capacity double-ended queue implemented as ring deque.
@@ -111,7 +111,7 @@ export declare class Deque<T = any> extends Sequence<T> {
111
111
  * Creates an iterator of `[index, value]` pairs for all elements in the deque.
112
112
  * @returns {Iterator}
113
113
  */
114
- entries(): Iterator<[number, T], any, undefined> & Iterable<[number, T]>;
114
+ entries(): Iterable<[number, T]> & Iterator<[number, T], any, undefined>;
115
115
  [Symbol.iterator](): Iterable<T>;
116
116
  inspect(): any;
117
117
  }
@@ -228,17 +228,15 @@ export class Deque extends Sequence {
228
228
  return IterableIterator(() => {
229
229
  if (j >= l)
230
230
  return {
231
- done: true
231
+ done: true,
232
+ value: undefined
232
233
  };
233
234
  const value = items[i];
234
235
  i++;
235
236
  j++;
236
237
  if (i === c)
237
238
  i = 0;
238
- return {
239
- value,
240
- done: false
241
- };
239
+ return { value };
242
240
  });
243
241
  }
244
242
  /**
@@ -260,15 +258,15 @@ export class Deque extends Sequence {
260
258
  return IterableIterator(() => {
261
259
  if (j >= l)
262
260
  return {
263
- done: true
261
+ done: true,
262
+ value: undefined
264
263
  };
265
264
  const value = items[i];
266
265
  i++;
267
266
  if (i === c)
268
267
  i = 0;
269
268
  return {
270
- value: [j++, value],
271
- done: false
269
+ value: [j++, value]
272
270
  };
273
271
  });
274
272
  }