atomic-fns 0.4.3 → 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.
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
 
@@ -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.
@@ -3,8 +3,8 @@
3
3
  *
4
4
  * @module Collections
5
5
  */
6
- import { Iteratee } from '../globals/index.js';
7
6
  import { compare } from '../operators/index.js';
7
+ import { Iteratee } from '../types';
8
8
  export * from './abc.js';
9
9
  export * from './BTree.js';
10
10
  export * from './deque.js';
@@ -0,0 +1,147 @@
1
+ /**
2
+ * `Decimal` provides support for correct rounded floating point arithmetic.
3
+ *
4
+ * @module decimal
5
+ */
6
+ /**
7
+ * Casts the given value as a {@link Decimal}.
8
+ */
9
+ export declare function decimal(x: any): Decimal;
10
+ /**
11
+ * `Decimal` provides support for correct rounded floating point arithmetic, unlike the standard Number type. It also supports user-defined precision (default is 20 decimal places) which can be as large as needed.
12
+ */
13
+ export declare class Decimal {
14
+ static PRECISION: number;
15
+ private i;
16
+ private e;
17
+ /**
18
+ * Returns a new `Decimal` instance from the value or `0`.
19
+ * @param {*} value
20
+ * @returns {Decimal}
21
+ */
22
+ constructor(value?: any);
23
+ /**
24
+ * Returns a new `Decimal` that has the opposite sign of this `Decimal`.
25
+ * @example
26
+ ```js
27
+ let a = decimal('5')
28
+ a.minus() // -5
29
+ ```
30
+ * @returns {Decimal}
31
+ */
32
+ negated(): Decimal;
33
+ /**
34
+ * Returns a new `Decimal` that is the sum of this and `x`.
35
+ * @param x
36
+ * @example
37
+ ```js
38
+ let a = decimal('50000000000')
39
+ let b = decimal('0.000000005')
40
+ a.add(b) // 50000000000.000000005
41
+ ```
42
+ * @returns {Decimal}
43
+ */
44
+ add(x: any): Decimal;
45
+ /**
46
+ * Returns a new `Decimal` that is the difference between this and `x`.
47
+ * @param x
48
+ * @example
49
+ ```js
50
+ let a = decimal('1')
51
+ let b = decimal('0.0000000000000000001')
52
+ a.sub(b) // 0.9999999999999999999
53
+ ```
54
+ * @returns {Decimal}
55
+ */
56
+ sub(x: any): Decimal;
57
+ /**
58
+ * Returns a new `Decimal` that is the product of this and `x`.
59
+ * @param x
60
+ * @example
61
+ ```js
62
+ let a = decimal('0.0000000000000000000025')
63
+ let b = decimal('400000000000000000000')
64
+ a.mul(b) // 1
65
+ ```
66
+ * @returns {Decimal}
67
+ */
68
+ mul(x: any): Decimal;
69
+ /**
70
+ * Returns a new `Decimal` that is the quotient of this and `x`
71
+ * @param x
72
+ * @example
73
+ ```js
74
+ let a = decimal('1')
75
+ let b = decimal('3')
76
+ a.div(b) // 0.333333333333333333333333333333
77
+ ```
78
+ * @returns {Decimal}
79
+ */
80
+ div(x: any): Decimal;
81
+ /**
82
+ * Returns the square root of this `Decimal`.
83
+ * @returns {TDecimal}
84
+ */
85
+ sqrt(): Decimal;
86
+ /** Returns a new copy of this Decimal value. */
87
+ clone(): Decimal;
88
+ /**
89
+ * Remove zeroes in the least-significant digit of this `Decimal`
90
+ */
91
+ private _normalize;
92
+ /**
93
+ * Truncate this `Decimal` to the configured precision
94
+ */
95
+ private _truncate;
96
+ /**
97
+ * Returns a string representation of this decimal.
98
+ * @returns {Decimal}
99
+ */
100
+ toString(): string;
101
+ /**
102
+ * Converts this decimal to the `Number` value
103
+ * @returns {number}
104
+ */
105
+ toNumber(): number;
106
+ static get precision(): number;
107
+ static set precision(value: number);
108
+ /**
109
+ * Returns `true` if this value is equal to the value of `x`, otherwise `false`.
110
+ * @param {*} x other
111
+ * @returns {boolean}
112
+ */
113
+ eq(x: any): boolean;
114
+ /**
115
+ * Returns `true` if this value is less than the value of `x`, otherwise `false`.
116
+ * @param {*} x
117
+ * @returns {boolean}
118
+ */
119
+ lt(x: any): boolean;
120
+ /**
121
+ * Returns a comparison value representing the ordering of this value in respect to `x`.
122
+ * * `-1` if this < x
123
+ * * `1` if this > x
124
+ * * `0` if this == x
125
+ * @param {*} x
126
+ * @returns {number}
127
+ */
128
+ compare(x: any): 1 | -1 | 0;
129
+ /**
130
+ * Returns `true` if this value is greater than the value of `x`, otherwise `false`.
131
+ * @param {*} x
132
+ * @returns {boolean}
133
+ */
134
+ gt(x: any): boolean;
135
+ /**
136
+ * Returns `true` if this value is less than or equal to the value of `x`, otherwise `false`.
137
+ * @param {*} x
138
+ * @returns {boolean}
139
+ */
140
+ lte(x: any): boolean;
141
+ /**
142
+ * Returns `true` if this value is greater than or equal to the value of `x`, otherwise `false`.
143
+ * @param {*} x
144
+ * @returns {boolean}
145
+ */
146
+ gte(x: any): boolean;
147
+ }