atomic-fns 0.4.3 → 1.1.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.
- package/README.md +11 -9
- package/dist/collections/BTree.d.ts +1 -1
- package/dist/collections/SortedMap.d.ts +2 -2
- package/dist/collections/SortedMap.js +2 -2
- package/dist/collections/Trie.d.ts +42 -0
- package/dist/collections/Trie.js +104 -0
- package/dist/collections/deque.d.ts +1 -1
- package/dist/collections/index.d.ts +3 -2
- package/dist/collections/index.js +2 -1
- package/dist/decimal/index.d.ts +149 -0
- package/dist/decimal/index.js +378 -0
- package/dist/functools/index.d.ts +24 -16
- package/dist/functools/index.js +36 -50
- package/dist/globals/decimal.d.ts +4 -4
- package/dist/globals/index.d.ts +4 -20
- package/dist/globals/index.js +1 -2
- package/dist/i18n/currency.d.ts +5 -5
- package/dist/i18n/currency.js +1 -1
- package/dist/i18n/date/format.d.ts +16 -0
- package/dist/i18n/date/format.js +313 -0
- package/dist/i18n/date/test.html +72 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/itertools/index.d.ts +3 -3
- package/dist/itertools/index.js +2 -2
- package/dist/math/index.d.ts +2 -2
- package/dist/math/index.js +1 -1
- package/dist/operators/index.d.ts +3 -3
- package/dist/operators/index.js +3 -3
- package/dist/random/index.d.ts +1 -1
- package/dist/random/index.js +1 -1
- package/dist/types.d.ts +19 -0
- package/dist/types.js +1 -0
- package/package.json +2 -2
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
|
|
5
|
+
## Like Lodash, but in TypeScript and for ESNext.
|
|
6
6
|
|
|
7
|
-
Stop shipping code built for browsers from
|
|
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
|
-
* **
|
|
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.
|
|
12
13
|
* **ESNext**: Uses modern idiomatic syntax, data structures, and control flow techniques.
|
|
14
|
+
* **Modular**: Pick what you need. Supports tree-shaking, no side effects, and works with all bundlers.
|
|
13
15
|
* **Lazy**: Leverage generators, iterators, and functional composition.
|
|
14
|
-
* **
|
|
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
|
+
* **Tests**: All modules have comprehensive test suites and public apis are 100% covered.
|
|
17
17
|
|
|
18
18
|
## Docs
|
|
19
19
|
|
|
@@ -52,7 +52,7 @@ See the full list of [modules](https://atomic-fns.dev/modules.html).
|
|
|
52
52
|
## Data model
|
|
53
53
|
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.
|
|
54
54
|
|
|
55
|
-
For example in Python collections `size()` is a method while in JS
|
|
55
|
+
For example, in Python collections, `size()` is a method, while in JS it's just a property. So this library uses `size` property instead of method. Similarly, the `len` function implementation checks for both `length` and `size` since many JS objects have a `length` property.
|
|
56
56
|
|
|
57
57
|
## Special method names
|
|
58
58
|
This library adds support for operators using object methods such as:
|
|
@@ -63,11 +63,13 @@ This library adds support for operators using object methods such as:
|
|
|
63
63
|
- `obj.gte(other)` adds custom `obj >= other`.
|
|
64
64
|
- `obj.compare(other)` overloads sorting operations.
|
|
65
65
|
|
|
66
|
-
When
|
|
66
|
+
When working with objects, functions in this library use these custom operators for equality and sorting. You can also apply operators to values like `eq(x, y)`, `lte(x, y)`, `compare(x, y)`, etc.
|
|
67
67
|
|
|
68
68
|
## New Types
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
[`Decimal`](https://atomic-fns.dev/classes/decimal.Decimal.html) provides support for correct rounded floating point arithmetic, with arbitrary precision.
|
|
71
|
+
|
|
72
|
+
Some common base interfaces and typing patterns you may have seen like `Optional`, `Result`, `Comparable`, `Iterator`, and others.
|
|
71
73
|
|
|
72
74
|
### Sorted Containers
|
|
73
75
|
- [SortedSet](https://atomic-fns.dev/classes/Collections.SortedSet.html)
|
|
@@ -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;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Mapping } from './abc.js';
|
|
2
2
|
/**
|
|
3
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
|
-
* @see {@link
|
|
5
|
-
* @see {@link
|
|
4
|
+
* @see {@link operators.compare}
|
|
5
|
+
* @see {@link operators.lt}
|
|
6
6
|
* @template K, V
|
|
7
7
|
*/
|
|
8
8
|
export declare class SortedMap<K, V> extends Mapping<K, V> {
|
|
@@ -2,8 +2,8 @@ import { Mapping } from './abc.js';
|
|
|
2
2
|
import { SplayTree } from './SplayTree.js';
|
|
3
3
|
/**
|
|
4
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
|
-
* @see {@link
|
|
6
|
-
* @see {@link
|
|
5
|
+
* @see {@link operators.compare}
|
|
6
|
+
* @see {@link operators.lt}
|
|
7
7
|
* @template K, V
|
|
8
8
|
*/
|
|
9
9
|
export class SortedMap extends Mapping {
|
|
@@ -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,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This module includes classes and functions to work with Container types.
|
|
3
3
|
*
|
|
4
|
-
* @module
|
|
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';
|
|
@@ -15,6 +15,7 @@ export * from './SortedMap.js';
|
|
|
15
15
|
export * from './SortedSet.js';
|
|
16
16
|
export * from './SortedTree.js';
|
|
17
17
|
export * from './SplayTree.js';
|
|
18
|
+
export * from './Trie.js';
|
|
18
19
|
/**
|
|
19
20
|
* Creates a new array with all falsy and empty values removed.
|
|
20
21
|
* @param arr The array to compact
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This module includes classes and functions to work with Container types.
|
|
3
3
|
*
|
|
4
|
-
* @module
|
|
4
|
+
* @module collections
|
|
5
5
|
*/
|
|
6
6
|
import { call, get, isArray, isArrayLike, isBool, isEmpty, isFunction, isIterable, isNull, isNumber, isObject, isString, keys, set } from '../globals/index.js';
|
|
7
7
|
import { enumerate } from '../itertools/index.js';
|
|
@@ -16,6 +16,7 @@ export * from './SortedMap.js';
|
|
|
16
16
|
export * from './SortedSet.js';
|
|
17
17
|
export * from './SortedTree.js';
|
|
18
18
|
export * from './SplayTree.js';
|
|
19
|
+
export * from './Trie.js';
|
|
19
20
|
export function compact(arr) {
|
|
20
21
|
if (!arr)
|
|
21
22
|
return;
|
|
@@ -0,0 +1,149 @@
|
|
|
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
|
|
12
|
+
* `Number` type. It also supports user-defined precision (default is 20 decimal places), which
|
|
13
|
+
* can be as large as needed.
|
|
14
|
+
*/
|
|
15
|
+
export declare class Decimal {
|
|
16
|
+
static PRECISION: number;
|
|
17
|
+
private i;
|
|
18
|
+
private e;
|
|
19
|
+
/**
|
|
20
|
+
* Returns a new `Decimal` instance from the value or `0`.
|
|
21
|
+
* @param {*} value
|
|
22
|
+
* @returns {Decimal}
|
|
23
|
+
*/
|
|
24
|
+
constructor(value?: any);
|
|
25
|
+
/**
|
|
26
|
+
* Returns a new `Decimal` that has the opposite sign of this `Decimal`.
|
|
27
|
+
* @example
|
|
28
|
+
```js
|
|
29
|
+
let a = decimal('5')
|
|
30
|
+
a.negated() // -5
|
|
31
|
+
```
|
|
32
|
+
* @returns {Decimal}
|
|
33
|
+
*/
|
|
34
|
+
negated(): Decimal;
|
|
35
|
+
/**
|
|
36
|
+
* Returns a new `Decimal` that is the sum of this and `x`.
|
|
37
|
+
* @param x
|
|
38
|
+
* @example
|
|
39
|
+
```js
|
|
40
|
+
let a = decimal('50000000000')
|
|
41
|
+
let b = decimal('0.000000005')
|
|
42
|
+
a.add(b) // 50000000000.000000005
|
|
43
|
+
```
|
|
44
|
+
* @returns {Decimal}
|
|
45
|
+
*/
|
|
46
|
+
add(x: any): Decimal;
|
|
47
|
+
/**
|
|
48
|
+
* Returns a new `Decimal` that is the difference between this and `x`.
|
|
49
|
+
* @param x
|
|
50
|
+
* @example
|
|
51
|
+
```js
|
|
52
|
+
let a = decimal('1')
|
|
53
|
+
let b = decimal('0.0000000000000000001')
|
|
54
|
+
a.sub(b) // 0.9999999999999999999
|
|
55
|
+
```
|
|
56
|
+
* @returns {Decimal}
|
|
57
|
+
*/
|
|
58
|
+
sub(x: any): Decimal;
|
|
59
|
+
/**
|
|
60
|
+
* Returns a new `Decimal` that is the product of this and `x`.
|
|
61
|
+
* @param x
|
|
62
|
+
* @example
|
|
63
|
+
```js
|
|
64
|
+
let a = decimal('0.0000000000000000000025')
|
|
65
|
+
let b = decimal('400000000000000000000')
|
|
66
|
+
a.mul(b) // 1
|
|
67
|
+
```
|
|
68
|
+
* @returns {Decimal}
|
|
69
|
+
*/
|
|
70
|
+
mul(x: any): Decimal;
|
|
71
|
+
/**
|
|
72
|
+
* Returns a new `Decimal` that is the quotient of this and `x`
|
|
73
|
+
* @param x
|
|
74
|
+
* @example
|
|
75
|
+
```js
|
|
76
|
+
let a = decimal('1')
|
|
77
|
+
let b = decimal('3')
|
|
78
|
+
a.div(b) // 0.333333333333333333333333333333
|
|
79
|
+
```
|
|
80
|
+
* @returns {Decimal}
|
|
81
|
+
*/
|
|
82
|
+
div(x: any): Decimal;
|
|
83
|
+
/**
|
|
84
|
+
* Returns the square root of this `Decimal`.
|
|
85
|
+
* @returns {TDecimal}
|
|
86
|
+
*/
|
|
87
|
+
sqrt(): Decimal;
|
|
88
|
+
/** Returns a new copy of this Decimal value. */
|
|
89
|
+
clone(): Decimal;
|
|
90
|
+
/**
|
|
91
|
+
* Remove zeroes in the least-significant digit of this `Decimal`
|
|
92
|
+
*/
|
|
93
|
+
private _normalize;
|
|
94
|
+
/**
|
|
95
|
+
* Truncate this `Decimal` to the configured precision
|
|
96
|
+
*/
|
|
97
|
+
private _truncate;
|
|
98
|
+
/**
|
|
99
|
+
* Returns a string representation of this decimal.
|
|
100
|
+
* @returns {Decimal}
|
|
101
|
+
*/
|
|
102
|
+
toString(): string;
|
|
103
|
+
/**
|
|
104
|
+
* Converts this decimal to the `Number` value
|
|
105
|
+
* @returns {number}
|
|
106
|
+
*/
|
|
107
|
+
toNumber(): number;
|
|
108
|
+
static get precision(): number;
|
|
109
|
+
static set precision(value: number);
|
|
110
|
+
/**
|
|
111
|
+
* Returns `true` if this value is equal to the value of `x`, otherwise `false`.
|
|
112
|
+
* @param {*} x other
|
|
113
|
+
* @returns {boolean}
|
|
114
|
+
*/
|
|
115
|
+
eq(x: any): boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Returns `true` if this value is less than the value of `x`, otherwise `false`.
|
|
118
|
+
* @param {*} x
|
|
119
|
+
* @returns {boolean}
|
|
120
|
+
*/
|
|
121
|
+
lt(x: any): boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Returns a comparison value representing the ordering of this value in respect to `x`.
|
|
124
|
+
* * `-1` if this < x
|
|
125
|
+
* * `1` if this > x
|
|
126
|
+
* * `0` if this == x
|
|
127
|
+
* @param {*} x
|
|
128
|
+
* @returns {number}
|
|
129
|
+
*/
|
|
130
|
+
compare(x: any): 1 | -1 | 0;
|
|
131
|
+
/**
|
|
132
|
+
* Returns `true` if this value is greater than the value of `x`, otherwise `false`.
|
|
133
|
+
* @param {*} x
|
|
134
|
+
* @returns {boolean}
|
|
135
|
+
*/
|
|
136
|
+
gt(x: any): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Returns `true` if this value is less than or equal to the value of `x`, otherwise `false`.
|
|
139
|
+
* @param {*} x
|
|
140
|
+
* @returns {boolean}
|
|
141
|
+
*/
|
|
142
|
+
lte(x: any): boolean;
|
|
143
|
+
/**
|
|
144
|
+
* Returns `true` if this value is greater than or equal to the value of `x`, otherwise `false`.
|
|
145
|
+
* @param {*} x
|
|
146
|
+
* @returns {boolean}
|
|
147
|
+
*/
|
|
148
|
+
gte(x: any): boolean;
|
|
149
|
+
}
|