atomic-fns 1.1.7 → 2.0.0
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 +405 -60
- package/dist/array/index.d.ts +9 -0
- package/dist/array/index.js +9 -0
- package/dist/array/list.d.ts +99 -0
- package/dist/array/list.js +133 -0
- package/dist/array/sets.d.ts +130 -0
- package/dist/array/sets.js +213 -0
- package/dist/async/index.d.ts +6 -0
- package/dist/async/index.js +6 -0
- package/dist/async/retry.d.ts +38 -0
- package/dist/async/retry.js +56 -0
- package/dist/async/sleep.d.ts +42 -0
- package/dist/async/sleep.js +76 -0
- package/dist/collections/bisect.d.ts +103 -0
- package/dist/collections/bisect.js +157 -0
- package/dist/collections/deque.d.ts +67 -97
- package/dist/collections/deque.js +197 -249
- package/dist/collections/entries.d.ts +89 -0
- package/dist/collections/entries.js +131 -0
- package/dist/collections/frozen-set.d.ts +42 -0
- package/dist/collections/frozen-set.js +64 -0
- package/dist/collections/group.d.ts +141 -0
- package/dist/collections/group.js +241 -0
- package/dist/collections/heap.d.ts +64 -0
- package/dist/collections/heap.js +205 -0
- package/dist/collections/index.d.ts +15 -642
- package/dist/collections/index.js +14 -716
- package/dist/collections/internal/arrayKeys.d.ts +19 -0
- package/dist/collections/internal/arrayKeys.js +83 -0
- package/dist/collections/internal/invariants.d.ts +4 -0
- package/dist/collections/internal/invariants.js +12 -0
- package/dist/collections/internal/iterable.d.ts +25 -0
- package/dist/collections/internal/iterable.js +62 -0
- package/dist/collections/iterate.d.ts +162 -0
- package/dist/collections/iterate.js +188 -0
- package/dist/collections/lru-cache.d.ts +58 -0
- package/dist/collections/lru-cache.js +164 -0
- package/dist/collections/order.d.ts +19 -0
- package/dist/collections/order.js +38 -0
- package/dist/collections/shape.d.ts +32 -0
- package/dist/collections/shape.js +86 -0
- package/dist/collections/sorted-map.d.ts +171 -0
- package/dist/collections/sorted-map.js +845 -0
- package/dist/collections/sorted-set.d.ts +103 -0
- package/dist/collections/sorted-set.js +147 -0
- package/dist/collections/splay-tree.d.ts +82 -0
- package/dist/collections/splay-tree.js +354 -0
- package/dist/collections/trie.d.ts +58 -0
- package/dist/collections/trie.js +196 -0
- package/dist/decimal/currency.d.ts +211 -0
- package/dist/decimal/currency.js +321 -0
- package/dist/decimal/decimal.d.ts +299 -0
- package/dist/decimal/decimal.js +660 -0
- package/dist/decimal/index.d.ts +11 -145
- package/dist/decimal/index.js +10 -374
- package/dist/decimal/internal/bits.d.ts +2 -0
- package/dist/decimal/internal/bits.js +7 -0
- package/dist/decimal/internal/echo.d.ts +4 -0
- package/dist/decimal/internal/echo.js +6 -0
- package/dist/decimal/internal/isqrt.d.ts +9 -0
- package/dist/decimal/internal/isqrt.js +34 -0
- package/dist/decimal/internal/locale.d.ts +7 -0
- package/dist/decimal/internal/locale.js +24 -0
- package/dist/decimal/internal/minor-units.d.ts +2 -0
- package/dist/decimal/internal/minor-units.js +33 -0
- package/dist/decimal/internal/pow10.d.ts +24 -0
- package/dist/decimal/internal/pow10.js +96 -0
- package/dist/encoding/base64.d.ts +39 -0
- package/dist/encoding/base64.js +53 -0
- package/dist/encoding/hex.d.ts +23 -0
- package/dist/encoding/hex.js +34 -0
- package/dist/encoding/index.d.ts +6 -0
- package/dist/encoding/index.js +6 -0
- package/dist/function/debounce.d.ts +68 -0
- package/dist/function/debounce.js +203 -0
- package/dist/function/index.d.ts +6 -0
- package/dist/function/index.js +6 -0
- package/dist/function/shared.d.ts +2 -0
- package/dist/function/shared.js +5 -0
- package/dist/function/wrap.d.ts +130 -0
- package/dist/function/wrap.js +113 -0
- package/dist/hash/digest.d.ts +18 -0
- package/dist/hash/digest.js +26 -0
- package/dist/hash/hash.d.ts +22 -0
- package/dist/hash/hash.js +252 -0
- package/dist/hash/hashString.d.ts +13 -0
- package/dist/hash/hashString.js +53 -0
- package/dist/hash/index.d.ts +8 -0
- package/dist/hash/index.js +8 -0
- package/dist/hash/stableStringify.d.ts +19 -0
- package/dist/hash/stableStringify.js +102 -0
- package/dist/id/index.d.ts +7 -0
- package/dist/id/index.js +7 -0
- package/dist/id/nanoid.d.ts +16 -0
- package/dist/id/nanoid.js +65 -0
- package/dist/id/uniqueId.d.ts +14 -0
- package/dist/id/uniqueId.js +20 -0
- package/dist/id/uuid.d.ts +25 -0
- package/dist/id/uuid.js +64 -0
- package/dist/index.d.ts +15 -8
- package/dist/index.js +15 -8
- package/dist/internal/bytes.d.ts +11 -0
- package/dist/internal/bytes.js +33 -0
- package/dist/internal/codec.d.ts +11 -0
- package/dist/internal/codec.js +84 -0
- package/dist/internal/compare.d.ts +10 -0
- package/dist/internal/compare.js +26 -0
- package/dist/internal/crypto.d.ts +5 -0
- package/dist/internal/crypto.js +11 -0
- package/dist/internal/keys.d.ts +12 -0
- package/dist/internal/keys.js +23 -0
- package/dist/internal/kind.d.ts +44 -0
- package/dist/internal/kind.js +139 -0
- package/dist/internal/sparse.d.ts +51 -0
- package/dist/internal/sparse.js +104 -0
- package/dist/internal/text.d.ts +7 -0
- package/dist/internal/text.js +15 -0
- package/dist/internal/timer.d.ts +10 -0
- package/dist/internal/timer.js +17 -0
- package/dist/internal/truncate.d.ts +10 -0
- package/dist/internal/truncate.js +22 -0
- package/dist/internal/walk.d.ts +47 -0
- package/dist/internal/walk.js +100 -0
- package/dist/is/index.d.ts +8 -0
- package/dist/is/index.js +8 -0
- package/dist/is/isArrayLike.d.ts +18 -0
- package/dist/is/isArrayLike.js +25 -0
- package/dist/is/isError.d.ts +13 -0
- package/dist/is/isError.js +18 -0
- package/dist/is/isNil.d.ts +12 -0
- package/dist/is/isNil.js +14 -0
- package/dist/is/isPlainObject.d.ts +15 -0
- package/dist/is/isPlainObject.js +34 -0
- package/dist/number/index.d.ts +7 -0
- package/dist/number/index.js +7 -0
- package/dist/number/round.d.ts +86 -0
- package/dist/number/round.js +130 -0
- package/dist/number/stats.d.ts +52 -0
- package/dist/number/stats.js +115 -0
- package/dist/object/cloneDeep.d.ts +28 -0
- package/dist/object/cloneDeep.js +200 -0
- package/dist/object/index.d.ts +14 -0
- package/dist/object/index.js +13 -0
- package/dist/object/isEqual.d.ts +64 -0
- package/dist/object/isEqual.js +289 -0
- package/dist/object/matches.d.ts +40 -0
- package/dist/object/matches.js +284 -0
- package/dist/object/merge.d.ts +66 -0
- package/dist/object/merge.js +272 -0
- package/dist/object/parsePath.d.ts +22 -0
- package/dist/object/parsePath.js +111 -0
- package/dist/object/path.d.ts +85 -0
- package/dist/object/path.js +208 -0
- package/dist/object/pick.d.ts +91 -0
- package/dist/object/pick.js +180 -0
- package/dist/object/reshape.d.ts +73 -0
- package/dist/object/reshape.js +144 -0
- package/dist/random/index.d.ts +96 -32
- package/dist/random/index.js +215 -50
- package/dist/string/case.d.ts +71 -0
- package/dist/string/case.js +152 -0
- package/dist/string/fold.d.ts +35 -0
- package/dist/string/fold.js +100 -0
- package/dist/string/index.d.ts +6 -187
- package/dist/string/index.js +6 -226
- package/dist/string/truncate.d.ts +25 -0
- package/dist/string/truncate.js +44 -0
- package/dist/string/words.d.ts +27 -0
- package/dist/string/words.js +241 -0
- package/package.json +101 -58
- package/dist/collections/BTree.d.ts +0 -229
- package/dist/collections/BTree.js +0 -1165
- package/dist/collections/Heap.d.ts +0 -69
- package/dist/collections/Heap.js +0 -181
- package/dist/collections/LRUCache.d.ts +0 -80
- package/dist/collections/LRUCache.js +0 -126
- package/dist/collections/SortedMap.d.ts +0 -92
- package/dist/collections/SortedMap.js +0 -131
- package/dist/collections/SortedSet.d.ts +0 -69
- package/dist/collections/SortedSet.js +0 -99
- package/dist/collections/SortedTree.d.ts +0 -103
- package/dist/collections/SortedTree.js +0 -141
- package/dist/collections/SplayTree.d.ts +0 -105
- package/dist/collections/SplayTree.js +0 -338
- package/dist/collections/Trie.d.ts +0 -42
- package/dist/collections/Trie.js +0 -104
- package/dist/collections/abc.d.ts +0 -152
- package/dist/collections/abc.js +0 -39
- package/dist/collections/frozenset.d.ts +0 -19
- package/dist/collections/frozenset.js +0 -30
- package/dist/functools/index.d.ts +0 -130
- package/dist/functools/index.js +0 -235
- package/dist/globals/index.d.ts +0 -285
- package/dist/globals/index.js +0 -463
- package/dist/i18n/currency.d.ts +0 -24
- package/dist/i18n/currency.js +0 -85
- package/dist/i18n/date/index.d.ts +0 -203
- package/dist/i18n/date/index.js +0 -531
- package/dist/i18n/date/utils.d.ts +0 -65
- package/dist/i18n/date/utils.js +0 -290
- package/dist/i18n/duration.d.ts +0 -112
- package/dist/i18n/duration.js +0 -392
- package/dist/i18n/format.d.ts +0 -31
- package/dist/i18n/format.js +0 -506
- package/dist/i18n/index.d.ts +0 -8
- package/dist/i18n/index.js +0 -8
- package/dist/itertools/index.d.ts +0 -255
- package/dist/itertools/index.js +0 -437
- package/dist/math/index.d.ts +0 -96
- package/dist/math/index.js +0 -98
- package/dist/mod.d.ts +0 -1
- package/dist/mod.js +0 -1
- package/dist/operators/index.d.ts +0 -152
- package/dist/operators/index.js +0 -305
- package/dist/types.d.ts +0 -19
- package/dist/types.js +0 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prefix tree for strings, path-compressed: memory grows with the number of words, not their
|
|
3
|
+
* length. Insertions, lookups and removals in O(s) for string length s. Prefixes compare by UTF-16
|
|
4
|
+
* code unit, as `startsWith` does.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://en.wikipedia.org/wiki/Radix_tree Radix tree}
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const words = new Trie(['car', 'cart', 'cat'])
|
|
10
|
+
* ;[...words.withPrefix('car')] // ['car', 'cart']
|
|
11
|
+
* words.withPrefix('ca').take(1).toArray() // ['car']
|
|
12
|
+
* words.has('ca') // false
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare class Trie {
|
|
16
|
+
#private;
|
|
17
|
+
/**
|
|
18
|
+
* Create a trie holding `words`.
|
|
19
|
+
* @param words Initial words. `null` means none.
|
|
20
|
+
* @throws {TypeError} when `words` is not iterable or holds a non-string.
|
|
21
|
+
*/
|
|
22
|
+
constructor(words?: Iterable<string> | null);
|
|
23
|
+
/**
|
|
24
|
+
* Add `word`. Non-strings throw. A duplicate is a no-op for size.
|
|
25
|
+
* @throws {TypeError} when `word` is not a string.
|
|
26
|
+
*/
|
|
27
|
+
add(word: string): this;
|
|
28
|
+
/**
|
|
29
|
+
* Remove `word` if present (including `''`) and prune empty branches.
|
|
30
|
+
* @returns `true` iff the word was present.
|
|
31
|
+
*/
|
|
32
|
+
delete(word: string): boolean;
|
|
33
|
+
/** Whether `word` was added. A prefix of a word is not a word. */
|
|
34
|
+
has(word: string): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Yield words that start with `prefix`, in child insertion order. The walk starts on the first
|
|
37
|
+
* `next()`, so it sees words added before then. A `clear` ends the iterator, started or not. Use
|
|
38
|
+
* `withPrefix(prefix).take(n)` for at most `n` words.
|
|
39
|
+
* @throws {TypeError} when `prefix` is not a string.
|
|
40
|
+
*/
|
|
41
|
+
withPrefix(prefix: string): IteratorObject<string>;
|
|
42
|
+
/** Remove every word, and end every iterator taken before. */
|
|
43
|
+
clear(): void;
|
|
44
|
+
/** The number of words. */
|
|
45
|
+
get size(): number;
|
|
46
|
+
/** `'Trie'`, for `Object.prototype.toString`. */
|
|
47
|
+
get [Symbol.toStringTag](): string;
|
|
48
|
+
/** Every word, in child insertion order. */
|
|
49
|
+
values(): IteratorObject<string>;
|
|
50
|
+
/** Every word, as `Set.prototype.keys` gives them. */
|
|
51
|
+
keys(): IteratorObject<string>;
|
|
52
|
+
/** `[word, word]` pairs, as `Set.prototype.entries` gives them. */
|
|
53
|
+
entries(): IteratorObject<[string, string]>;
|
|
54
|
+
/** Call `callback(word, word, trie)` for each word. */
|
|
55
|
+
forEach(callback: (value: string, key: string, trie: this) => void, thisArg?: unknown): void;
|
|
56
|
+
/** Every word, in child insertion order. */
|
|
57
|
+
[Symbol.iterator](): IteratorObject<string>;
|
|
58
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { requireIterable } from './internal/iterable.js';
|
|
2
|
+
function newNode(path) {
|
|
3
|
+
return { path, isWord: false };
|
|
4
|
+
}
|
|
5
|
+
function attach(parent, unit, child) {
|
|
6
|
+
child.parent = parent;
|
|
7
|
+
parent.children ??= new Map();
|
|
8
|
+
parent.children.set(unit, child);
|
|
9
|
+
return child;
|
|
10
|
+
}
|
|
11
|
+
/** Where `a` and `b` first differ, given that they agree up to `from`. */
|
|
12
|
+
function sharedLength(a, b, from) {
|
|
13
|
+
const end = Math.min(a.length, b.length);
|
|
14
|
+
let i = from;
|
|
15
|
+
while (i < end && a.charCodeAt(i) === b.charCodeAt(i))
|
|
16
|
+
i++;
|
|
17
|
+
return i;
|
|
18
|
+
}
|
|
19
|
+
/** Drop or merge a node that no longer holds a word. Nodes keep their paths, so open walks stay valid. */
|
|
20
|
+
function prune(node) {
|
|
21
|
+
const { parent, children } = node;
|
|
22
|
+
if (parent === undefined || node.isWord)
|
|
23
|
+
return;
|
|
24
|
+
const unit = node.path.charCodeAt(parent.path.length);
|
|
25
|
+
if (children === undefined || children.size === 0) {
|
|
26
|
+
parent.children?.delete(unit);
|
|
27
|
+
prune(parent);
|
|
28
|
+
}
|
|
29
|
+
else if (children.size === 1) {
|
|
30
|
+
const [only] = children.values();
|
|
31
|
+
attach(parent, unit, only);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Prefix tree for strings, path-compressed: memory grows with the number of words, not their
|
|
36
|
+
* length. Insertions, lookups and removals in O(s) for string length s. Prefixes compare by UTF-16
|
|
37
|
+
* code unit, as `startsWith` does.
|
|
38
|
+
*
|
|
39
|
+
* @see {@link https://en.wikipedia.org/wiki/Radix_tree Radix tree}
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const words = new Trie(['car', 'cart', 'cat'])
|
|
43
|
+
* ;[...words.withPrefix('car')] // ['car', 'cart']
|
|
44
|
+
* words.withPrefix('ca').take(1).toArray() // ['car']
|
|
45
|
+
* words.has('ca') // false
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export class Trie {
|
|
49
|
+
#root = newNode('');
|
|
50
|
+
#count = 0;
|
|
51
|
+
#generation = 0;
|
|
52
|
+
/**
|
|
53
|
+
* Create a trie holding `words`.
|
|
54
|
+
* @param words Initial words. `null` means none.
|
|
55
|
+
* @throws {TypeError} when `words` is not iterable or holds a non-string.
|
|
56
|
+
*/
|
|
57
|
+
constructor(words) {
|
|
58
|
+
for (const word of requireIterable(words ?? [])) {
|
|
59
|
+
this.add(word);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Add `word`. Non-strings throw. A duplicate is a no-op for size.
|
|
64
|
+
* @throws {TypeError} when `word` is not a string.
|
|
65
|
+
*/
|
|
66
|
+
add(word) {
|
|
67
|
+
if (typeof word !== 'string')
|
|
68
|
+
throw new TypeError('Trie stores strings');
|
|
69
|
+
let node = this.#root;
|
|
70
|
+
while (node.path.length < word.length) {
|
|
71
|
+
const from = node.path.length;
|
|
72
|
+
const unit = word.charCodeAt(from);
|
|
73
|
+
const child = node.children?.get(unit);
|
|
74
|
+
if (child === undefined) {
|
|
75
|
+
node = attach(node, unit, newNode(word));
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
const shared = sharedLength(word, child.path, from);
|
|
79
|
+
if (shared === child.path.length) {
|
|
80
|
+
node = child;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
const middle = attach(node, unit, newNode(word.slice(0, shared)));
|
|
84
|
+
attach(middle, child.path.charCodeAt(shared), child);
|
|
85
|
+
node = middle;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (!node.isWord) {
|
|
89
|
+
node.isWord = true;
|
|
90
|
+
this.#count++;
|
|
91
|
+
}
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Remove `word` if present (including `''`) and prune empty branches.
|
|
96
|
+
* @returns `true` iff the word was present.
|
|
97
|
+
*/
|
|
98
|
+
delete(word) {
|
|
99
|
+
const node = this.#findWord(word);
|
|
100
|
+
if (node === undefined)
|
|
101
|
+
return false;
|
|
102
|
+
node.isWord = false;
|
|
103
|
+
this.#count--;
|
|
104
|
+
prune(node);
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
/** Whether `word` was added. A prefix of a word is not a word. */
|
|
108
|
+
has(word) {
|
|
109
|
+
return this.#findWord(word) !== undefined;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Yield words that start with `prefix`, in child insertion order. The walk starts on the first
|
|
113
|
+
* `next()`, so it sees words added before then. A `clear` ends the iterator, started or not. Use
|
|
114
|
+
* `withPrefix(prefix).take(n)` for at most `n` words.
|
|
115
|
+
* @throws {TypeError} when `prefix` is not a string.
|
|
116
|
+
*/
|
|
117
|
+
withPrefix(prefix) {
|
|
118
|
+
if (typeof prefix !== 'string')
|
|
119
|
+
throw new TypeError('prefix must be a string');
|
|
120
|
+
return this.#walk(prefix, this.#generation);
|
|
121
|
+
}
|
|
122
|
+
*#walk(prefix, generation) {
|
|
123
|
+
const start = this.#locate(prefix);
|
|
124
|
+
if (start === undefined)
|
|
125
|
+
return;
|
|
126
|
+
const stack = [[start].values()];
|
|
127
|
+
while (stack.length > 0 && generation === this.#generation) {
|
|
128
|
+
const next = stack[stack.length - 1].next();
|
|
129
|
+
if (next.done) {
|
|
130
|
+
stack.pop();
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const node = next.value;
|
|
134
|
+
if (node.isWord)
|
|
135
|
+
yield node.path;
|
|
136
|
+
if (node.children)
|
|
137
|
+
stack.push(node.children.values());
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/** The topmost node whose path starts with `prefix`. */
|
|
141
|
+
#locate(prefix) {
|
|
142
|
+
let node = this.#root;
|
|
143
|
+
while (node.path.length < prefix.length) {
|
|
144
|
+
const from = node.path.length;
|
|
145
|
+
const child = node.children?.get(prefix.charCodeAt(from));
|
|
146
|
+
if (!child)
|
|
147
|
+
return undefined;
|
|
148
|
+
const shared = sharedLength(prefix, child.path, from);
|
|
149
|
+
if (shared < prefix.length && shared < child.path.length)
|
|
150
|
+
return undefined;
|
|
151
|
+
node = child;
|
|
152
|
+
}
|
|
153
|
+
return node;
|
|
154
|
+
}
|
|
155
|
+
#findWord(word) {
|
|
156
|
+
if (typeof word !== 'string')
|
|
157
|
+
return undefined;
|
|
158
|
+
const node = this.#locate(word);
|
|
159
|
+
return node?.isWord === true && node.path.length === word.length ? node : undefined;
|
|
160
|
+
}
|
|
161
|
+
/** Remove every word, and end every iterator taken before. */
|
|
162
|
+
clear() {
|
|
163
|
+
this.#root = newNode('');
|
|
164
|
+
this.#count = 0;
|
|
165
|
+
this.#generation++;
|
|
166
|
+
}
|
|
167
|
+
/** The number of words. */
|
|
168
|
+
get size() {
|
|
169
|
+
return this.#count;
|
|
170
|
+
}
|
|
171
|
+
/** `'Trie'`, for `Object.prototype.toString`. */
|
|
172
|
+
get [Symbol.toStringTag]() {
|
|
173
|
+
return 'Trie';
|
|
174
|
+
}
|
|
175
|
+
/** Every word, in child insertion order. */
|
|
176
|
+
values() {
|
|
177
|
+
return this.withPrefix('');
|
|
178
|
+
}
|
|
179
|
+
/** Every word, as `Set.prototype.keys` gives them. */
|
|
180
|
+
keys() {
|
|
181
|
+
return this.values();
|
|
182
|
+
}
|
|
183
|
+
/** `[word, word]` pairs, as `Set.prototype.entries` gives them. */
|
|
184
|
+
entries() {
|
|
185
|
+
return this.values().map((w) => [w, w]);
|
|
186
|
+
}
|
|
187
|
+
/** Call `callback(word, word, trie)` for each word. */
|
|
188
|
+
forEach(callback, thisArg) {
|
|
189
|
+
for (const w of this.values())
|
|
190
|
+
Reflect.apply(callback, thisArg, [w, w, this]);
|
|
191
|
+
}
|
|
192
|
+
/** Every word, in child insertion order. */
|
|
193
|
+
[Symbol.iterator]() {
|
|
194
|
+
return this.values();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { Decimal, type DecimalInput, type DivideOptions, type RoundingMode } from './decimal.js';
|
|
2
|
+
/**
|
|
3
|
+
* An exact amount of money in one ISO 4217 currency, as the TC39 Amount proposal pairs a value
|
|
4
|
+
* with its unit. Arithmetic and ordering with another currency throw `RangeError`; nothing
|
|
5
|
+
* converts between currencies.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const price = new Currency('19.99', 'USD')
|
|
10
|
+
* price.multiply(3).format('en-US') // '$59.97'
|
|
11
|
+
* price.add(new Currency('0.01', 'USD')).toString() // '20 USD'
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare class Currency {
|
|
15
|
+
#private;
|
|
16
|
+
/**
|
|
17
|
+
* Orders two amounts of the same currency, for `toSorted` and sorted collections.
|
|
18
|
+
* @throws {TypeError} when `a` or `b` is not a Currency.
|
|
19
|
+
* @throws {RangeError} when the currencies differ.
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* [new Currency(5, 'EUR'), new Currency(2, 'EUR')].toSorted(Currency.compare).map(String)
|
|
23
|
+
* // ['2 EUR', '5 EUR']
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
static compare(a: Currency, b: Currency): -1 | 0 | 1;
|
|
27
|
+
/**
|
|
28
|
+
* Pairs an amount with a currency code.
|
|
29
|
+
* @param amount The amount, read as {@link Decimal} reads it.
|
|
30
|
+
* @param currency A three-letter ISO 4217 code, in any case.
|
|
31
|
+
* @throws {TypeError} when `currency` is not a string, or `amount` not a Decimal input.
|
|
32
|
+
* @throws {RangeError} when `currency` is not three letters, or `amount` is malformed.
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* new Currency('12.50', 'usd').currency // 'USD'
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
constructor(amount: DecimalInput, currency: string);
|
|
39
|
+
/** The exact amount. */
|
|
40
|
+
get amount(): Decimal;
|
|
41
|
+
/** The upper-case ISO 4217 code, such as `'USD'`. */
|
|
42
|
+
get currency(): string;
|
|
43
|
+
/**
|
|
44
|
+
* The sum of two amounts of the same currency.
|
|
45
|
+
* @throws {TypeError} when `other` is not a Currency.
|
|
46
|
+
* @throws {RangeError} when the currencies differ.
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* new Currency('0.1', 'USD').add(new Currency('0.2', 'USD')).toString() // '0.3 USD'
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
add(other: Currency): Currency;
|
|
53
|
+
/**
|
|
54
|
+
* The difference of two amounts of the same currency.
|
|
55
|
+
* @throws {TypeError} when `other` is not a Currency.
|
|
56
|
+
* @throws {RangeError} when the currencies differ.
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* new Currency(10, 'EUR').subtract(new Currency('2.5', 'EUR')).toString() // '7.5 EUR'
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
subtract(other: Currency): Currency;
|
|
63
|
+
/**
|
|
64
|
+
* This amount times a plain factor.
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* new Currency('19.99', 'USD').multiply(3).toString() // '59.97 USD'
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
multiply(factor: DecimalInput): Currency;
|
|
71
|
+
/**
|
|
72
|
+
* This amount divided by a plain divisor, rounded as {@link Decimal.divide} rounds, to 34
|
|
73
|
+
* significant digits by default; follow with {@link Currency.round} for the minor unit.
|
|
74
|
+
* @throws {RangeError} for a zero divisor.
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* new Currency(10, 'USD').divide(3).round().toString() // '3.33 USD'
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
divide(divisor: DecimalInput, options?: DivideOptions): Currency;
|
|
81
|
+
/**
|
|
82
|
+
* This amount with the opposite sign.
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* new Currency(5, 'USD').negate().toString() // '-5 USD'
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
negate(): Currency;
|
|
89
|
+
/**
|
|
90
|
+
* The absolute amount.
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* new Currency(-5, 'USD').abs().toString() // '5 USD'
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
abs(): Currency;
|
|
97
|
+
/**
|
|
98
|
+
* Rounds the amount, by default to the currency's ISO 4217 minor unit (2 digits for USD, 0 for
|
|
99
|
+
* JPY, 3 for IQD). The mode defaults to `'halfEven'`, while {@link Currency.format} rounds what
|
|
100
|
+
* it shows half away from zero, as `Intl.NumberFormat` does.
|
|
101
|
+
* @param digits Fractional digits (may be negative); defaults to the minor unit.
|
|
102
|
+
* @param roundingMode One of the `Intl.NumberFormat` rounding modes; defaults to `'halfEven'`.
|
|
103
|
+
* @throws {RangeError} when `digits` is left out and ISO 4217 gives the code no minor unit, as
|
|
104
|
+
* for XAU or a code it does not list.
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* new Currency('2.675', 'USD').round().toString() // '2.68 USD'
|
|
108
|
+
* new Currency('1234.5', 'JPY').round().toString() // '1234 JPY'
|
|
109
|
+
* new Currency('2.665', 'USD').round(undefined, 'halfExpand').toString() // '2.67 USD'
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
round(digits?: number, roundingMode?: RoundingMode): Currency;
|
|
113
|
+
/**
|
|
114
|
+
* Orders this amount against another of the same currency: -1, 0 or 1.
|
|
115
|
+
* @throws {TypeError} when `other` is not a Currency.
|
|
116
|
+
* @throws {RangeError} when the currencies differ.
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* new Currency(1, 'USD').compare(new Currency('1.00', 'USD')) // 0
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
compare(other: Currency): -1 | 0 | 1;
|
|
123
|
+
/**
|
|
124
|
+
* True for a Currency with the same code and amount; false for anything else, including the
|
|
125
|
+
* same amount in another currency.
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* new Currency(1, 'USD').equals(new Currency(1, 'EUR')) // false
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
equals(other: Currency): boolean;
|
|
132
|
+
/**
|
|
133
|
+
* True when this amount is less than `other`.
|
|
134
|
+
* @throws {TypeError} when `other` is not a Currency.
|
|
135
|
+
* @throws {RangeError} when the currencies differ.
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* new Currency(1, 'USD').lessThan(new Currency(2, 'USD')) // true
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
lessThan(other: Currency): boolean;
|
|
142
|
+
/**
|
|
143
|
+
* True when this amount is less than or equal to `other`.
|
|
144
|
+
* @throws {TypeError} when `other` is not a Currency.
|
|
145
|
+
* @throws {RangeError} when the currencies differ.
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* new Currency(2, 'USD').lessThanOrEqual(new Currency(2, 'USD')) // true
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
lessThanOrEqual(other: Currency): boolean;
|
|
152
|
+
/**
|
|
153
|
+
* True when this amount is greater than `other`.
|
|
154
|
+
* @throws {TypeError} when `other` is not a Currency.
|
|
155
|
+
* @throws {RangeError} when the currencies differ.
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* new Currency(3, 'USD').greaterThan(new Currency(2, 'USD')) // true
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
greaterThan(other: Currency): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* True when this amount is greater than or equal to `other`.
|
|
164
|
+
* @throws {TypeError} when `other` is not a Currency.
|
|
165
|
+
* @throws {RangeError} when the currencies differ.
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* new Currency(2, 'USD').greaterThanOrEqual(new Currency(3, 'USD')) // false
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
greaterThanOrEqual(other: Currency): boolean;
|
|
172
|
+
/**
|
|
173
|
+
* Formats with `Intl.NumberFormat` in currency style. Unless `options` set the digits, the
|
|
174
|
+
* amount keeps its own fraction digits, up to 20, instead of rounding to the minor unit, so
|
|
175
|
+
* `0.125` dollars show as `$0.125`; pass `{ maximumFractionDigits: 2 }` or {@link Currency.round}
|
|
176
|
+
* first for minor units. A `roundingMode` alone changes nothing, since no digits are cut; pair it
|
|
177
|
+
* with `maximumFractionDigits`. `style` and `currency` always come from this amount.
|
|
178
|
+
* @throws {RangeError} when the amount is beyond `Number.MAX_VALUE`, which `Intl` shows as `∞`,
|
|
179
|
+
* or the locale is malformed or longer than 255 characters.
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* new Currency('1234.5', 'EUR').format('de-DE') // '1.234,50 €'
|
|
183
|
+
* new Currency('0.125', 'USD').format('en-US', { maximumFractionDigits: 2 }) // '$0.13'
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
format(locale?: Intl.LocalesArgument, options?: Intl.NumberFormatOptions): string;
|
|
187
|
+
/**
|
|
188
|
+
* The same as {@link Currency.format}, under the name `Intl` and Temporal use.
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* new Currency(5, 'GBP').toLocaleString('en-GB') // '£5.00'
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.NumberFormatOptions): string;
|
|
195
|
+
/**
|
|
196
|
+
* The amount's canonical string and the code, such as `'12.5 USD'`, for logs and keys.
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* new Currency('12.50', 'usd').toString() // '12.5 USD'
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
toString(): string;
|
|
203
|
+
/** The same string as {@link Currency.toString}, so `JSON.stringify` keeps every digit. */
|
|
204
|
+
toJSON(): string;
|
|
205
|
+
/**
|
|
206
|
+
* Throws, so `<` and `+` cannot silently compare or add amounts. Use {@link Currency.compare}
|
|
207
|
+
* or {@link Currency.add}.
|
|
208
|
+
* @throws {TypeError} always.
|
|
209
|
+
*/
|
|
210
|
+
valueOf(): never;
|
|
211
|
+
}
|