atomic-fns 1.2.0 → 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 +397 -91
- 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 -256
- 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 -623
- package/dist/collections/index.js +14 -844
- 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 -148
- package/dist/decimal/index.js +10 -410
- 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 -53
- 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 -224
- 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 +99 -51
- package/dist/collections/BTree.d.ts +0 -230
- package/dist/collections/BTree.js +0 -1143
- package/dist/collections/Heap.d.ts +0 -69
- package/dist/collections/Heap.js +0 -181
- package/dist/collections/LRUCache.d.ts +0 -83
- package/dist/collections/LRUCache.js +0 -139
- 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 -142
- package/dist/collections/SplayTree.d.ts +0 -107
- package/dist/collections/SplayTree.js +0 -351
- 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 -33
- package/dist/functools/index.d.ts +0 -106
- package/dist/functools/index.js +0 -203
- package/dist/globals/index.d.ts +0 -268
- package/dist/globals/index.js +0 -447
- package/dist/i18n/currency.d.ts +0 -27
- package/dist/i18n/currency.js +0 -100
- package/dist/i18n/date/index.d.ts +0 -210
- package/dist/i18n/date/index.js +0 -552
- package/dist/i18n/date/utils.d.ts +0 -64
- package/dist/i18n/date/utils.js +0 -192
- package/dist/i18n/duration.d.ts +0 -113
- package/dist/i18n/duration.js +0 -398
- package/dist/i18n/format.d.ts +0 -34
- package/dist/i18n/format.js +0 -378
- package/dist/i18n/index.d.ts +0 -9
- package/dist/i18n/index.js +0 -8
- package/dist/itertools/index.d.ts +0 -251
- package/dist/itertools/index.js +0 -434
- package/dist/math/index.d.ts +0 -79
- package/dist/math/index.js +0 -81
- package/dist/mod.d.ts +0 -1
- package/dist/mod.js +0 -1
- package/dist/operators/index.d.ts +0 -148
- package/dist/operators/index.js +0 -325
- package/dist/types.d.ts +0 -19
- package/dist/types.js +0 -1
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Active ISO 4217 codes by minor-unit digits, from the ISO 4217 list one published 2026-09-17.
|
|
3
|
+
* Codes the list gives no minor unit (XAU, XDR, XXX and the other funds and metals) are absent.
|
|
4
|
+
* `Intl` cannot stand in: it reports CLDR display digits, which differ from ISO 4217 (0 for IQD
|
|
5
|
+
* and HUF) and change with the runtime's ICU version.
|
|
6
|
+
*/
|
|
7
|
+
const CODES_BY_MINOR_UNITS = [
|
|
8
|
+
[0, 'BIF CLP DJF GNF ISK JPY KMF KRW PYG RWF UGX UYI VND VUV XAF XOF XPF'],
|
|
9
|
+
[
|
|
10
|
+
2,
|
|
11
|
+
'AED AFN ALL AMD AOA ARS AUD AWG AZN BAM BBD BDT BMD BND BOB BOV BRL BSD BTN BWP BYN ' +
|
|
12
|
+
'BZD CAD CDF CHE CHF CHW CNY COP COU CRC CUP CVE CZK DKK DOP DZD EGP ERN ETB EUR FJD ' +
|
|
13
|
+
'FKP GBP GEL GHS GIP GMD GTQ GYD HKD HNL HTG HUF IDR ILS INR IRR JMD KES KGS KHR KPW ' +
|
|
14
|
+
'KYD KZT LAK LBP LKR LRD LSL MAD MDL MGA MKD MMK MNT MOP MRU MUR MVR MWK MXN MXV MYR ' +
|
|
15
|
+
'MZN NAD NGN NIO NOK NPR NZD PAB PEN PGK PHP PKR PLN QAR RON RSD RUB SAR SBD SCR SDG ' +
|
|
16
|
+
'SEK SGD SHP SLE SOS SRD SSP STN SVC SYP SZL THB TJS TMT TOP TRY TTD TWD TZS UAH USD ' +
|
|
17
|
+
'USN UYU UZS VED VES WST XAD XCD XCG YER ZAR ZMW ZWG'
|
|
18
|
+
],
|
|
19
|
+
[3, 'BHD IQD JOD KWD LYD OMR TND'],
|
|
20
|
+
[4, 'CLF UYW']
|
|
21
|
+
];
|
|
22
|
+
let minorUnits;
|
|
23
|
+
/** The ISO 4217 minor-unit digits of an upper-case `code`, or undefined when it has none. */
|
|
24
|
+
export function isoMinorUnits(code) {
|
|
25
|
+
if (minorUnits === undefined) {
|
|
26
|
+
minorUnits = new Map();
|
|
27
|
+
for (const [digits, codes] of CODES_BY_MINOR_UNITS) {
|
|
28
|
+
for (const listed of codes.split(' '))
|
|
29
|
+
minorUnits.set(listed, digits);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return minorUnits.get(code);
|
|
33
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lazy power-of-ten caches for Decimal arithmetic and normalization.
|
|
3
|
+
* Built on first use so importing decimal does not allocate large bigints.
|
|
4
|
+
*
|
|
5
|
+
* Bounded: linear powers below 64, blocks of 64 below 1024, and a squares table
|
|
6
|
+
* persisted through 10^16384 (15 entries) under Decimal's public bounds.
|
|
7
|
+
* Larger powers are built by binary decomposition and are never stored.
|
|
8
|
+
*/
|
|
9
|
+
/** Return `10^k` for a non-negative integer `k`. */
|
|
10
|
+
export declare function pow10(k: number): bigint;
|
|
11
|
+
/**
|
|
12
|
+
* Return the memoized `10^(2^k)` table, with at least the powers through 10^8192.
|
|
13
|
+
* Built once; subsequent calls are free.
|
|
14
|
+
*/
|
|
15
|
+
export declare function pow10Squares(): readonly bigint[];
|
|
16
|
+
/** Length of the linear `10^k` cache (1 right after import). For tests. */
|
|
17
|
+
export declare function getPow10CacheLength(): number;
|
|
18
|
+
/** Length of the square `10^(2^k)` cache (0 right after import). For tests. */
|
|
19
|
+
export declare function getPow10SquareCacheLength(): number;
|
|
20
|
+
/**
|
|
21
|
+
* Total decimal digits across every cached power-of-ten bigint.
|
|
22
|
+
* 10^e contributes e+1 digits. For tests.
|
|
23
|
+
*/
|
|
24
|
+
export declare function getPow10CachedDigits(): number;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lazy power-of-ten caches for Decimal arithmetic and normalization.
|
|
3
|
+
* Built on first use so importing decimal does not allocate large bigints.
|
|
4
|
+
*
|
|
5
|
+
* Bounded: linear powers below 64, blocks of 64 below 1024, and a squares table
|
|
6
|
+
* persisted through 10^16384 (15 entries) under Decimal's public bounds.
|
|
7
|
+
* Larger powers are built by binary decomposition and are never stored.
|
|
8
|
+
*/
|
|
9
|
+
const LINEAR_BITS = 6;
|
|
10
|
+
const LINEAR_MAX = 1 << LINEAR_BITS;
|
|
11
|
+
const BLOCK_MAX = 1024;
|
|
12
|
+
/** Cached `10^k` for k < LINEAR_MAX. Index 0 is always `1n`. Grows on demand. */
|
|
13
|
+
const POW10_TABLE = [1n];
|
|
14
|
+
const POW10_BLOCK_TABLE = [1n];
|
|
15
|
+
const POW10_SQUARE_TABLE = [];
|
|
16
|
+
/**
|
|
17
|
+
* Ensure the persisted square table has at least `minLength` entries.
|
|
18
|
+
* First entry is `10n` (10^(2^0)); each further entry squares the previous.
|
|
19
|
+
*/
|
|
20
|
+
function ensureSquares(minLength) {
|
|
21
|
+
if (POW10_SQUARE_TABLE.length === 0 && minLength > 0) {
|
|
22
|
+
POW10_SQUARE_TABLE.push(10n);
|
|
23
|
+
}
|
|
24
|
+
while (POW10_SQUARE_TABLE.length < minLength) {
|
|
25
|
+
const last = POW10_SQUARE_TABLE[POW10_SQUARE_TABLE.length - 1];
|
|
26
|
+
POW10_SQUARE_TABLE.push(last * last);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function squareAt(bit) {
|
|
30
|
+
ensureSquares(bit + 1);
|
|
31
|
+
return POW10_SQUARE_TABLE[bit];
|
|
32
|
+
}
|
|
33
|
+
/** Return `10^k` for a non-negative integer `k`. */
|
|
34
|
+
export function pow10(k) {
|
|
35
|
+
if (k < BLOCK_MAX) {
|
|
36
|
+
const block = k >> LINEAR_BITS;
|
|
37
|
+
const remainder = k & (LINEAR_MAX - 1);
|
|
38
|
+
while (POW10_TABLE.length <= remainder) {
|
|
39
|
+
const last = POW10_TABLE[POW10_TABLE.length - 1];
|
|
40
|
+
POW10_TABLE.push(last * 10n);
|
|
41
|
+
}
|
|
42
|
+
if (block === 0)
|
|
43
|
+
return POW10_TABLE[remainder];
|
|
44
|
+
while (POW10_BLOCK_TABLE.length <= block) {
|
|
45
|
+
const last = POW10_BLOCK_TABLE[POW10_BLOCK_TABLE.length - 1];
|
|
46
|
+
POW10_BLOCK_TABLE.push(last * squareAt(LINEAR_BITS));
|
|
47
|
+
}
|
|
48
|
+
return POW10_BLOCK_TABLE[block] * POW10_TABLE[remainder];
|
|
49
|
+
}
|
|
50
|
+
// Binary decomposition: 10^k = product of 10^(2^i) for each set bit i of k.
|
|
51
|
+
let result = 1n;
|
|
52
|
+
let bit = 0;
|
|
53
|
+
let rem = k;
|
|
54
|
+
while (rem > 0) {
|
|
55
|
+
if (rem & 1) {
|
|
56
|
+
result *= squareAt(bit);
|
|
57
|
+
}
|
|
58
|
+
rem >>= 1;
|
|
59
|
+
bit++;
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Return the memoized `10^(2^k)` table, with at least the powers through 10^8192.
|
|
65
|
+
* Built once; subsequent calls are free.
|
|
66
|
+
*/
|
|
67
|
+
export function pow10Squares() {
|
|
68
|
+
// Stripping up to 10000 trailing zeros (Decimal's digit bound) needs squares through 2^13 = 8192.
|
|
69
|
+
ensureSquares(14);
|
|
70
|
+
return POW10_SQUARE_TABLE;
|
|
71
|
+
}
|
|
72
|
+
/** Length of the linear `10^k` cache (1 right after import). For tests. */
|
|
73
|
+
export function getPow10CacheLength() {
|
|
74
|
+
return POW10_TABLE.length;
|
|
75
|
+
}
|
|
76
|
+
/** Length of the square `10^(2^k)` cache (0 right after import). For tests. */
|
|
77
|
+
export function getPow10SquareCacheLength() {
|
|
78
|
+
return POW10_SQUARE_TABLE.length;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Total decimal digits across every cached power-of-ten bigint.
|
|
82
|
+
* 10^e contributes e+1 digits. For tests.
|
|
83
|
+
*/
|
|
84
|
+
export function getPow10CachedDigits() {
|
|
85
|
+
let total = 0;
|
|
86
|
+
for (let i = 0; i < POW10_TABLE.length; i++) {
|
|
87
|
+
total += i + 1;
|
|
88
|
+
}
|
|
89
|
+
for (let j = 0; j < POW10_SQUARE_TABLE.length; j++) {
|
|
90
|
+
total += (1 << j) + 1;
|
|
91
|
+
}
|
|
92
|
+
for (let i = 0; i < POW10_BLOCK_TABLE.length; i++) {
|
|
93
|
+
total += i * LINEAR_MAX + 1;
|
|
94
|
+
}
|
|
95
|
+
return total;
|
|
96
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Options for {@link toBase64}. */
|
|
2
|
+
export interface ToBase64Options {
|
|
3
|
+
/** `'base64'` (`+` and `/`, the default) or `'base64url'` (`-` and `_`). */
|
|
4
|
+
alphabet?: 'base64' | 'base64url';
|
|
5
|
+
/** Leave out the trailing `=` padding (default `false`). */
|
|
6
|
+
omitPadding?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** Options for {@link fromBase64}. */
|
|
9
|
+
export interface FromBase64Options {
|
|
10
|
+
/** `'base64'` (`+` and `/`, the default) or `'base64url'` (`-` and `_`). */
|
|
11
|
+
alphabet?: 'base64' | 'base64url';
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Base64 of a byte array, a buffer, or a string's UTF-8 bytes, with the `base64url` alphabet as an
|
|
15
|
+
* option. Uses the native `Uint8Array.prototype.toBase64` where present.
|
|
16
|
+
* @param data A `Uint8Array` (or other typed array or `DataView`), an `ArrayBuffer`, or a string.
|
|
17
|
+
* @param options `alphabet` and `omitPadding`.
|
|
18
|
+
* @returns The base64 text.
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* toBase64('hi') // 'aGk='
|
|
22
|
+
* toBase64(new Uint8Array([251, 255]), { alphabet: 'base64url', omitPadding: true }) // '-_8'
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function toBase64(data: string | ArrayBuffer | ArrayBufferView, options?: ToBase64Options): string;
|
|
26
|
+
/**
|
|
27
|
+
* Bytes of base64 text, as `Uint8Array.fromBase64` reads it (native where present): ASCII
|
|
28
|
+
* whitespace is skipped and padding is optional, but a character outside the alphabet or a broken
|
|
29
|
+
* last chunk throws `SyntaxError`, as the native method does. Decode text with `TextDecoder`.
|
|
30
|
+
* @param text Base64 text.
|
|
31
|
+
* @param options `alphabet`.
|
|
32
|
+
* @returns The decoded bytes.
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* fromBase64('aGk=') // Uint8Array [104, 105]
|
|
36
|
+
* new TextDecoder().decode(fromBase64('aGk')) // 'hi'
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function fromBase64(text: string, options?: FromBase64Options): Uint8Array;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { toBytes } from '../internal/bytes.js';
|
|
2
|
+
import { decodeBase64, encodeBase64 } from '../internal/codec.js';
|
|
3
|
+
const nativeToBase64 = Uint8Array.prototype.toBase64;
|
|
4
|
+
const nativeFromBase64 = Uint8Array.fromBase64;
|
|
5
|
+
function alphabetOf(options) {
|
|
6
|
+
const alphabet = options?.alphabet ?? 'base64';
|
|
7
|
+
if (alphabet === 'base64' || alphabet === 'base64url')
|
|
8
|
+
return alphabet;
|
|
9
|
+
throw new TypeError(`unknown base64 alphabet: ${String(alphabet)}`);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Base64 of a byte array, a buffer, or a string's UTF-8 bytes, with the `base64url` alphabet as an
|
|
13
|
+
* option. Uses the native `Uint8Array.prototype.toBase64` where present.
|
|
14
|
+
* @param data A `Uint8Array` (or other typed array or `DataView`), an `ArrayBuffer`, or a string.
|
|
15
|
+
* @param options `alphabet` and `omitPadding`.
|
|
16
|
+
* @returns The base64 text.
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* toBase64('hi') // 'aGk='
|
|
20
|
+
* toBase64(new Uint8Array([251, 255]), { alphabet: 'base64url', omitPadding: true }) // '-_8'
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function toBase64(data, options) {
|
|
24
|
+
const alphabet = alphabetOf(options);
|
|
25
|
+
const bytes = toBytes(data);
|
|
26
|
+
const omitPadding = options?.omitPadding === true;
|
|
27
|
+
if (nativeToBase64 !== undefined) {
|
|
28
|
+
return nativeToBase64.call(bytes, { alphabet, omitPadding });
|
|
29
|
+
}
|
|
30
|
+
return encodeBase64(bytes, alphabet, omitPadding);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Bytes of base64 text, as `Uint8Array.fromBase64` reads it (native where present): ASCII
|
|
34
|
+
* whitespace is skipped and padding is optional, but a character outside the alphabet or a broken
|
|
35
|
+
* last chunk throws `SyntaxError`, as the native method does. Decode text with `TextDecoder`.
|
|
36
|
+
* @param text Base64 text.
|
|
37
|
+
* @param options `alphabet`.
|
|
38
|
+
* @returns The decoded bytes.
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* fromBase64('aGk=') // Uint8Array [104, 105]
|
|
42
|
+
* new TextDecoder().decode(fromBase64('aGk')) // 'hi'
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function fromBase64(text, options) {
|
|
46
|
+
if (typeof text !== 'string')
|
|
47
|
+
throw new TypeError('fromBase64 expects a string');
|
|
48
|
+
const alphabet = alphabetOf(options);
|
|
49
|
+
if (nativeFromBase64 !== undefined) {
|
|
50
|
+
return nativeFromBase64(text, { alphabet });
|
|
51
|
+
}
|
|
52
|
+
return decodeBase64(text, alphabet);
|
|
53
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lowercase hex of a byte array, a buffer, or a string's UTF-8 bytes. Uses the native
|
|
3
|
+
* `Uint8Array.prototype.toHex` where present.
|
|
4
|
+
* @param data A `Uint8Array` (or other typed array or `DataView`), an `ArrayBuffer`, or a string.
|
|
5
|
+
* @returns Two hex digits per byte.
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* toHex(new Uint8Array([0, 255])) // '00ff'
|
|
9
|
+
* toHex('hi') // '6869'
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function toHex(data: string | ArrayBuffer | ArrayBufferView): string;
|
|
13
|
+
/**
|
|
14
|
+
* Bytes of hex text, as `Uint8Array.fromHex` reads it (native where present): digits in either case,
|
|
15
|
+
* an even count, nothing else; anything else throws `SyntaxError`, as the native method does.
|
|
16
|
+
* @param text Hex text.
|
|
17
|
+
* @returns The decoded bytes.
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* fromHex('00ff') // Uint8Array [0, 255]
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function fromHex(text: string): Uint8Array;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { bytesToHex, toBytes } from '../internal/bytes.js';
|
|
2
|
+
import { decodeHex } from '../internal/codec.js';
|
|
3
|
+
const nativeToHex = Uint8Array.prototype.toHex;
|
|
4
|
+
const nativeFromHex = Uint8Array.fromHex;
|
|
5
|
+
/**
|
|
6
|
+
* Lowercase hex of a byte array, a buffer, or a string's UTF-8 bytes. Uses the native
|
|
7
|
+
* `Uint8Array.prototype.toHex` where present.
|
|
8
|
+
* @param data A `Uint8Array` (or other typed array or `DataView`), an `ArrayBuffer`, or a string.
|
|
9
|
+
* @returns Two hex digits per byte.
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* toHex(new Uint8Array([0, 255])) // '00ff'
|
|
13
|
+
* toHex('hi') // '6869'
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function toHex(data) {
|
|
17
|
+
const bytes = toBytes(data);
|
|
18
|
+
return nativeToHex !== undefined ? nativeToHex.call(bytes) : bytesToHex(bytes);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Bytes of hex text, as `Uint8Array.fromHex` reads it (native where present): digits in either case,
|
|
22
|
+
* an even count, nothing else; anything else throws `SyntaxError`, as the native method does.
|
|
23
|
+
* @param text Hex text.
|
|
24
|
+
* @returns The decoded bytes.
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* fromHex('00ff') // Uint8Array [0, 255]
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function fromHex(text) {
|
|
31
|
+
if (typeof text !== 'string')
|
|
32
|
+
throw new TypeError('fromHex expects a string');
|
|
33
|
+
return nativeFromHex !== undefined ? nativeFromHex(text) : decodeHex(text);
|
|
34
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** A debounced or throttled function with `cancel`, `flush` and `pending`. */
|
|
2
|
+
export interface Debounced<F extends (...args: never[]) => unknown> {
|
|
3
|
+
/** Schedules a call and returns the result of the last invocation, if any. */
|
|
4
|
+
(this: ThisParameterType<F>, ...args: Parameters<F>): ReturnType<F> | undefined;
|
|
5
|
+
/** Drops the pending call and ends the current wait, so the next call starts fresh. */
|
|
6
|
+
cancel(): void;
|
|
7
|
+
/** Runs the pending call now and returns its result, or the last result when none is pending. */
|
|
8
|
+
flush(): ReturnType<F> | undefined;
|
|
9
|
+
/** Whether a call is waiting to run. */
|
|
10
|
+
pending(): boolean;
|
|
11
|
+
}
|
|
12
|
+
/** Options for {@link debounce}. */
|
|
13
|
+
export interface DebounceOptions {
|
|
14
|
+
/** Invoke on the first call of a burst (default `false`). */
|
|
15
|
+
leading?: boolean;
|
|
16
|
+
/** Invoke after the last call of a burst (default `true`). When `false`, held calls never run. */
|
|
17
|
+
trailing?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Longest time the trailing call may be held, counted from the first call of the burst or the
|
|
20
|
+
* first call after `maxWait` last ran it. Has no effect when `trailing` is `false`.
|
|
21
|
+
*/
|
|
22
|
+
maxWait?: number | null;
|
|
23
|
+
}
|
|
24
|
+
/** Options for {@link throttle}. */
|
|
25
|
+
export interface ThrottleOptions {
|
|
26
|
+
/** Invoke when a window opens (default `true`). */
|
|
27
|
+
leading?: boolean;
|
|
28
|
+
/** Invoke at the end of a window with the latest arguments (default `true`). */
|
|
29
|
+
trailing?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Creates a function that delays invoking `fn` until `wait` milliseconds have passed since the
|
|
33
|
+
* last call, with the latest arguments and `this`. With `maxWait`, a burst of calls also runs `fn`
|
|
34
|
+
* at least once per `maxWait`. `wait` defaults to 0; negative waits clamp to 0, and NaN, infinite
|
|
35
|
+
* or past-2147483647 waits throw `RangeError`, and a non-number wait throws `TypeError`. A
|
|
36
|
+
* `maxWait` below `wait` is raised to `wait`. Idle windows use `performance.now()` (faked by
|
|
37
|
+
* default in Vitest 5 and Jest) and hold no timer when `wait > 0`. Held trailing calls use timers;
|
|
38
|
+
* `wait === 0` keeps a timer until the next macrotask.
|
|
39
|
+
* @param fn The function to debounce.
|
|
40
|
+
* @param wait Milliseconds to wait after the last call.
|
|
41
|
+
* @param options `leading`, `trailing` and `maxWait`.
|
|
42
|
+
* @returns The debounced function with `cancel`, `flush` and `pending`.
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* const save = debounce((text: string) => console.log(text), 100)
|
|
46
|
+
* save('h')
|
|
47
|
+
* save('hi') // logs 'hi' once, 100 ms later
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function debounce<F extends (...args: never[]) => unknown>(fn: F, wait?: number | null, options?: DebounceOptions): Debounced<F>;
|
|
51
|
+
/**
|
|
52
|
+
* Creates a function that invokes `fn` at most once per `wait` milliseconds: on the leading edge,
|
|
53
|
+
* and once more at the end of the window with the latest arguments when calls arrived during it
|
|
54
|
+
* (Lodash's leading and trailing defaults). The window opens before `fn` runs, so a reentrant
|
|
55
|
+
* call or a throw from `fn` stays throttled. Idle windows use `performance.now()` (faked by
|
|
56
|
+
* default in Vitest 5 and Jest) and hold no timer when `wait > 0`. Held trailing calls use timers;
|
|
57
|
+
* `wait === 0` keeps one trailing invocation per macrotask. Waits are checked as in {@link debounce}.
|
|
58
|
+
* @param fn The function to throttle.
|
|
59
|
+
* @param wait Milliseconds per window.
|
|
60
|
+
* @param options `leading` and `trailing`.
|
|
61
|
+
* @returns The throttled function with `cancel`, `flush` and `pending`.
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const onScroll = throttle(() => console.log(window.scrollY), 100)
|
|
65
|
+
* addEventListener('scroll', onScroll)
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function throttle<F extends (...args: never[]) => unknown>(fn: F, wait?: number | null, options?: ThrottleOptions): Debounced<F>;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { timerWait } from '../internal/timer.js';
|
|
2
|
+
import { assertFunction } from './shared.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a function that delays invoking `fn` until `wait` milliseconds have passed since the
|
|
5
|
+
* last call, with the latest arguments and `this`. With `maxWait`, a burst of calls also runs `fn`
|
|
6
|
+
* at least once per `maxWait`. `wait` defaults to 0; negative waits clamp to 0, and NaN, infinite
|
|
7
|
+
* or past-2147483647 waits throw `RangeError`, and a non-number wait throws `TypeError`. A
|
|
8
|
+
* `maxWait` below `wait` is raised to `wait`. Idle windows use `performance.now()` (faked by
|
|
9
|
+
* default in Vitest 5 and Jest) and hold no timer when `wait > 0`. Held trailing calls use timers;
|
|
10
|
+
* `wait === 0` keeps a timer until the next macrotask.
|
|
11
|
+
* @param fn The function to debounce.
|
|
12
|
+
* @param wait Milliseconds to wait after the last call.
|
|
13
|
+
* @param options `leading`, `trailing` and `maxWait`.
|
|
14
|
+
* @returns The debounced function with `cancel`, `flush` and `pending`.
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const save = debounce((text: string) => console.log(text), 100)
|
|
18
|
+
* save('h')
|
|
19
|
+
* save('hi') // logs 'hi' once, 100 ms later
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function debounce(fn, wait, options) {
|
|
23
|
+
assertFunction(fn, 'fn');
|
|
24
|
+
const delay = timerWait(wait ?? 0, 'wait');
|
|
25
|
+
const leading = options?.leading ?? false;
|
|
26
|
+
const trailing = options?.trailing ?? true;
|
|
27
|
+
const maxWait = options?.maxWait == null ? undefined : Math.max(timerWait(options.maxWait, 'maxWait'), delay);
|
|
28
|
+
// Only timers run the held call, and only with `trailing`: without it maxWait bounds nothing.
|
|
29
|
+
const maxDelay = trailing ? maxWait : undefined;
|
|
30
|
+
let timer;
|
|
31
|
+
let maxTimer;
|
|
32
|
+
let deadline = -Infinity;
|
|
33
|
+
let maxDeadline;
|
|
34
|
+
let activeCalls = 0;
|
|
35
|
+
let lastCall;
|
|
36
|
+
let lastResult;
|
|
37
|
+
function invokeLastCall() {
|
|
38
|
+
if (lastCall === undefined)
|
|
39
|
+
return lastResult;
|
|
40
|
+
const call = lastCall;
|
|
41
|
+
lastCall = undefined;
|
|
42
|
+
activeCalls++;
|
|
43
|
+
try {
|
|
44
|
+
lastResult = fn.apply(call.thisArg, call.args);
|
|
45
|
+
return lastResult;
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
activeCalls--;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function clearTimers() {
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
clearTimeout(maxTimer);
|
|
54
|
+
timer = maxTimer = undefined;
|
|
55
|
+
}
|
|
56
|
+
/** Whether the held call will run: both timers that run it need `trailing`. */
|
|
57
|
+
function willRun() {
|
|
58
|
+
return trailing && lastCall !== undefined;
|
|
59
|
+
}
|
|
60
|
+
function debounced(...args) {
|
|
61
|
+
const now = performance.now();
|
|
62
|
+
const startsBurst = timer === undefined && (deadline === -Infinity || (activeCalls === 0 && now >= deadline));
|
|
63
|
+
const invokeLeading = leading && startsBurst;
|
|
64
|
+
if (startsBurst)
|
|
65
|
+
maxDeadline = undefined;
|
|
66
|
+
deadline = now + delay;
|
|
67
|
+
lastCall = trailing || invokeLeading ? { thisArg: this, args } : undefined;
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
timer = undefined;
|
|
70
|
+
const needsTimer = delay === 0 || (trailing && !invokeLeading);
|
|
71
|
+
if (needsTimer) {
|
|
72
|
+
timer = setTimeout(() => {
|
|
73
|
+
clearTimers();
|
|
74
|
+
deadline = -Infinity;
|
|
75
|
+
maxDeadline = undefined;
|
|
76
|
+
if (trailing)
|
|
77
|
+
invokeLastCall();
|
|
78
|
+
else
|
|
79
|
+
lastCall = undefined;
|
|
80
|
+
}, delay);
|
|
81
|
+
}
|
|
82
|
+
if (maxDelay !== undefined) {
|
|
83
|
+
maxDeadline ??= now + maxDelay;
|
|
84
|
+
if (needsTimer && maxTimer === undefined) {
|
|
85
|
+
maxTimer = setTimeout(() => {
|
|
86
|
+
maxTimer = undefined;
|
|
87
|
+
maxDeadline = undefined;
|
|
88
|
+
if (delay > 0)
|
|
89
|
+
clearTimers();
|
|
90
|
+
invokeLastCall();
|
|
91
|
+
}, Math.max(0, Math.min(maxDelay, Math.ceil(maxDeadline - now))));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return invokeLeading ? invokeLastCall() : lastResult;
|
|
95
|
+
}
|
|
96
|
+
debounced.cancel = () => {
|
|
97
|
+
clearTimers();
|
|
98
|
+
deadline = -Infinity;
|
|
99
|
+
maxDeadline = undefined;
|
|
100
|
+
lastCall = undefined;
|
|
101
|
+
};
|
|
102
|
+
debounced.flush = () => {
|
|
103
|
+
if (!willRun())
|
|
104
|
+
return lastResult;
|
|
105
|
+
clearTimers();
|
|
106
|
+
deadline = -Infinity;
|
|
107
|
+
maxDeadline = undefined;
|
|
108
|
+
return invokeLastCall();
|
|
109
|
+
};
|
|
110
|
+
debounced.pending = willRun;
|
|
111
|
+
return debounced;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Creates a function that invokes `fn` at most once per `wait` milliseconds: on the leading edge,
|
|
115
|
+
* and once more at the end of the window with the latest arguments when calls arrived during it
|
|
116
|
+
* (Lodash's leading and trailing defaults). The window opens before `fn` runs, so a reentrant
|
|
117
|
+
* call or a throw from `fn` stays throttled. Idle windows use `performance.now()` (faked by
|
|
118
|
+
* default in Vitest 5 and Jest) and hold no timer when `wait > 0`. Held trailing calls use timers;
|
|
119
|
+
* `wait === 0` keeps one trailing invocation per macrotask. Waits are checked as in {@link debounce}.
|
|
120
|
+
* @param fn The function to throttle.
|
|
121
|
+
* @param wait Milliseconds per window.
|
|
122
|
+
* @param options `leading` and `trailing`.
|
|
123
|
+
* @returns The throttled function with `cancel`, `flush` and `pending`.
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* const onScroll = throttle(() => console.log(window.scrollY), 100)
|
|
127
|
+
* addEventListener('scroll', onScroll)
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export function throttle(fn, wait, options) {
|
|
131
|
+
assertFunction(fn, 'fn');
|
|
132
|
+
const delay = timerWait(wait ?? 0, 'wait');
|
|
133
|
+
const leading = options?.leading ?? true;
|
|
134
|
+
const trailing = options?.trailing ?? true;
|
|
135
|
+
let lastResult;
|
|
136
|
+
let trailingCall;
|
|
137
|
+
let timer;
|
|
138
|
+
let deadline = -Infinity;
|
|
139
|
+
let activeCalls = 0;
|
|
140
|
+
function scheduleWindowEnd(ms) {
|
|
141
|
+
timer = setTimeout(() => {
|
|
142
|
+
timer = undefined;
|
|
143
|
+
deadline = -Infinity;
|
|
144
|
+
const call = trailingCall;
|
|
145
|
+
trailingCall = undefined;
|
|
146
|
+
if (call)
|
|
147
|
+
invoke(call);
|
|
148
|
+
}, ms);
|
|
149
|
+
}
|
|
150
|
+
function startWindow() {
|
|
151
|
+
deadline = performance.now() + delay;
|
|
152
|
+
if (delay === 0)
|
|
153
|
+
scheduleWindowEnd(delay);
|
|
154
|
+
}
|
|
155
|
+
function invoke(call) {
|
|
156
|
+
startWindow();
|
|
157
|
+
activeCalls++;
|
|
158
|
+
try {
|
|
159
|
+
lastResult = fn.apply(call.thisArg, call.args);
|
|
160
|
+
return lastResult;
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
activeCalls--;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function clearWindow() {
|
|
167
|
+
deadline = -Infinity;
|
|
168
|
+
if (timer !== undefined) {
|
|
169
|
+
clearTimeout(timer);
|
|
170
|
+
timer = undefined;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function throttled(...args) {
|
|
174
|
+
const call = { thisArg: this, args };
|
|
175
|
+
if (timer === undefined &&
|
|
176
|
+
(deadline === -Infinity || (activeCalls === 0 && performance.now() >= deadline))) {
|
|
177
|
+
if (leading)
|
|
178
|
+
return invoke(call);
|
|
179
|
+
startWindow();
|
|
180
|
+
}
|
|
181
|
+
if (trailing) {
|
|
182
|
+
trailingCall = call;
|
|
183
|
+
if (timer === undefined) {
|
|
184
|
+
scheduleWindowEnd(Math.max(0, Math.min(delay, Math.ceil(deadline - performance.now()))));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return lastResult;
|
|
188
|
+
}
|
|
189
|
+
throttled.cancel = () => {
|
|
190
|
+
clearWindow();
|
|
191
|
+
trailingCall = undefined;
|
|
192
|
+
};
|
|
193
|
+
throttled.flush = () => {
|
|
194
|
+
if (trailingCall === undefined)
|
|
195
|
+
return lastResult;
|
|
196
|
+
const call = trailingCall;
|
|
197
|
+
trailingCall = undefined;
|
|
198
|
+
clearWindow();
|
|
199
|
+
return invoke(call);
|
|
200
|
+
};
|
|
201
|
+
throttled.pending = () => trailingCall !== undefined;
|
|
202
|
+
return throttled;
|
|
203
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function wrappers: `debounce`, `throttle`, `memoize`, `once`, `curry`, `flow` and `compose`.
|
|
3
|
+
* @module function
|
|
4
|
+
*/
|
|
5
|
+
export { type DebounceOptions, type Debounced, debounce, type ThrottleOptions, throttle } from './debounce.js';
|
|
6
|
+
export { compose, type Curried, curry, flow, type LooseFunction, type MemoizeCache, type Memoized, memoize, once } from './wrap.js';
|