atomic-fns 1.1.7 → 1.2.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 +51 -12
- package/dist/collections/BTree.d.ts +11 -10
- package/dist/collections/BTree.js +58 -80
- package/dist/collections/Heap.js +1 -1
- package/dist/collections/LRUCache.d.ts +6 -3
- package/dist/collections/LRUCache.js +24 -11
- package/dist/collections/SortedMap.js +2 -2
- package/dist/collections/SortedTree.d.ts +2 -2
- package/dist/collections/SortedTree.js +3 -2
- package/dist/collections/SplayTree.d.ts +7 -5
- package/dist/collections/SplayTree.js +39 -26
- package/dist/collections/Trie.d.ts +1 -1
- package/dist/collections/Trie.js +26 -26
- package/dist/collections/deque.d.ts +1 -1
- package/dist/collections/deque.js +17 -10
- package/dist/collections/frozenset.js +4 -1
- package/dist/collections/index.d.ts +13 -32
- package/dist/collections/index.js +210 -82
- package/dist/decimal/index.d.ts +5 -2
- package/dist/decimal/index.js +63 -27
- package/dist/functools/index.d.ts +6 -30
- package/dist/functools/index.js +24 -56
- package/dist/globals/index.d.ts +3 -20
- package/dist/globals/index.js +23 -39
- package/dist/i18n/currency.d.ts +3 -0
- package/dist/i18n/currency.js +22 -7
- package/dist/i18n/date/index.d.ts +17 -10
- package/dist/i18n/date/index.js +79 -58
- package/dist/i18n/date/utils.d.ts +4 -5
- package/dist/i18n/date/utils.js +15 -113
- package/dist/i18n/duration.d.ts +11 -10
- package/dist/i18n/duration.js +38 -32
- package/dist/i18n/format.d.ts +5 -2
- package/dist/i18n/format.js +81 -209
- package/dist/i18n/index.d.ts +2 -1
- package/dist/itertools/index.d.ts +9 -13
- package/dist/itertools/index.js +38 -41
- package/dist/math/index.d.ts +10 -27
- package/dist/math/index.js +16 -33
- package/dist/operators/index.d.ts +1 -5
- package/dist/operators/index.js +60 -40
- package/dist/random/index.js +9 -6
- package/dist/string/index.d.ts +1 -1
- package/dist/string/index.js +2 -4
- package/dist/types.d.ts +3 -3
- package/package.json +16 -21
package/dist/decimal/index.js
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module decimal
|
|
5
5
|
*/
|
|
6
|
-
|
|
6
|
+
import { ValueError } from '../globals/index.js';
|
|
7
|
+
const NUMBER_REGEX = /^\s*([+-]?)(?=\.?[0-9])([0-9]*)(\.([0-9]*))?(?:[eE]([+-]?[0-9]+))?\s*$/;
|
|
8
|
+
/** Largest power of ten an exponent may reach, so a short string cannot freeze the page. */
|
|
9
|
+
const MAX_EXPONENT = 10_000;
|
|
7
10
|
/**
|
|
8
11
|
* Casts the given value as a {@link Decimal}.
|
|
9
12
|
*/
|
|
@@ -23,6 +26,8 @@ export class Decimal {
|
|
|
23
26
|
* Returns a new `Decimal` instance from the value or `0`.
|
|
24
27
|
* @param {*} value
|
|
25
28
|
* @returns {Decimal}
|
|
29
|
+
* @throws {ValueError} if the value is not numeric.
|
|
30
|
+
* @throws {RangeError} if the value is infinite or its exponent is past 10,000.
|
|
26
31
|
*/
|
|
27
32
|
constructor(value) {
|
|
28
33
|
if (value == null) {
|
|
@@ -35,7 +40,8 @@ export class Decimal {
|
|
|
35
40
|
this._normalize();
|
|
36
41
|
return this;
|
|
37
42
|
}
|
|
38
|
-
const
|
|
43
|
+
const text = value.toString();
|
|
44
|
+
const result = NUMBER_REGEX.exec(text);
|
|
39
45
|
if (result) {
|
|
40
46
|
// console.log(result);
|
|
41
47
|
// Construct from string if possible
|
|
@@ -45,11 +51,22 @@ export class Decimal {
|
|
|
45
51
|
if (typeof result[4] === 'undefined')
|
|
46
52
|
result[4] = '';
|
|
47
53
|
this.i = BigInt(result[1] + result[2] + result[4]);
|
|
48
|
-
this.e = result[
|
|
54
|
+
this.e = Number(result[5] ?? 0) - result[4].length;
|
|
49
55
|
this._normalize();
|
|
56
|
+
if (result[5]) {
|
|
57
|
+
const magnitude = this.e + (this.i < 0n ? -this.i : this.i).toString().length - 1;
|
|
58
|
+
if (this.i === 0n)
|
|
59
|
+
this.e = 0;
|
|
60
|
+
else if (Math.abs(magnitude) > MAX_EXPONENT)
|
|
61
|
+
throw new RangeError('exponent out of range');
|
|
62
|
+
}
|
|
50
63
|
}
|
|
51
64
|
else {
|
|
52
65
|
// Conversion to string failed, so construct from number
|
|
66
|
+
value = Number(value);
|
|
67
|
+
if (Number.isNaN(value) || (value === 0 && text.trim() === '')) {
|
|
68
|
+
throw new ValueError('Decimal value must be numeric');
|
|
69
|
+
}
|
|
53
70
|
let m = 1n;
|
|
54
71
|
if (value < 0) {
|
|
55
72
|
value = -value;
|
|
@@ -63,7 +80,7 @@ export class Decimal {
|
|
|
63
80
|
// Multiply the number by 10 until it is an integer
|
|
64
81
|
let e = -Math.floor(Math.log10(value));
|
|
65
82
|
if (e > 0) {
|
|
66
|
-
value *=
|
|
83
|
+
value *= 10 ** e;
|
|
67
84
|
}
|
|
68
85
|
else {
|
|
69
86
|
e = 0;
|
|
@@ -169,7 +186,7 @@ export class Decimal {
|
|
|
169
186
|
```js
|
|
170
187
|
let a = decimal('1')
|
|
171
188
|
let b = decimal('3')
|
|
172
|
-
a.div(b) // 0.
|
|
189
|
+
a.div(b) // 0.33333333333333333333
|
|
173
190
|
```
|
|
174
191
|
* @returns {Decimal}
|
|
175
192
|
*/
|
|
@@ -179,8 +196,8 @@ export class Decimal {
|
|
|
179
196
|
// We need to increase the number of digits of a and b so that a / b will have the desired precision.
|
|
180
197
|
// In order for the integer quotient to have the correct precision, a.e - b.e must be greater than that precision.
|
|
181
198
|
// So we need only to increase a.e.
|
|
182
|
-
//
|
|
183
|
-
const increaseA = Math.max(Decimal.precision
|
|
199
|
+
// The quotient needs precision + a.e - b.e, and a negative a.e keeps the digits it always had
|
|
200
|
+
const increaseA = Math.max(Decimal.precision + Math.abs(a.e) - b.e, 0);
|
|
184
201
|
const intIncrease = new Decimal();
|
|
185
202
|
intIncrease.i = exp(a.i, increaseA);
|
|
186
203
|
const quotient = new Decimal();
|
|
@@ -192,14 +209,17 @@ export class Decimal {
|
|
|
192
209
|
}
|
|
193
210
|
/**
|
|
194
211
|
* Returns the square root of this `Decimal`.
|
|
195
|
-
* @returns {
|
|
212
|
+
* @returns {Decimal}
|
|
213
|
+
* @throws {ValueError} if this value is negative.
|
|
196
214
|
*/
|
|
197
215
|
sqrt() {
|
|
216
|
+
if (this.i < 0n)
|
|
217
|
+
throw new ValueError('square root of a negative number');
|
|
198
218
|
Decimal.PRECISION++;
|
|
199
|
-
// Let the guess value be 10^(
|
|
219
|
+
// Let the guess value be 10^(m/2), where m is the magnitude of this value
|
|
200
220
|
let x = new Decimal();
|
|
201
221
|
x.i = 1n;
|
|
202
|
-
x.e = Math.floor(this.e / 2);
|
|
222
|
+
x.e = Math.floor((this.e + this.i.toString().length) / 2);
|
|
203
223
|
const onehalf = new Decimal('0.5');
|
|
204
224
|
let xt = x.clone();
|
|
205
225
|
xt._truncate();
|
|
@@ -221,6 +241,13 @@ export class Decimal {
|
|
|
221
241
|
Decimal.PRECISION--;
|
|
222
242
|
xt._truncate();
|
|
223
243
|
xt._normalize();
|
|
244
|
+
// Rounding twice can leave the last digit one too high, so check it against the exact square
|
|
245
|
+
const half = new Decimal();
|
|
246
|
+
half.i = 5n;
|
|
247
|
+
half.e = -Decimal.PRECISION - 1;
|
|
248
|
+
const low = xt.sub(half);
|
|
249
|
+
if (low.gt(0) && low.mul(low).gt(this))
|
|
250
|
+
return low.sub(half);
|
|
224
251
|
return xt;
|
|
225
252
|
}
|
|
226
253
|
/** Returns a new copy of this Decimal value. */
|
|
@@ -234,10 +261,16 @@ export class Decimal {
|
|
|
234
261
|
* Remove zeroes in the least-significant digit of this `Decimal`
|
|
235
262
|
*/
|
|
236
263
|
_normalize() {
|
|
237
|
-
|
|
238
|
-
this.
|
|
239
|
-
|
|
240
|
-
|
|
264
|
+
if (this.i === 0n)
|
|
265
|
+
this.e = Math.min(this.e, 0);
|
|
266
|
+
if (this.i === 0n || this.i % 10n !== 0n)
|
|
267
|
+
return this;
|
|
268
|
+
const digits = this.i.toString();
|
|
269
|
+
let end = digits.length;
|
|
270
|
+
while (digits[end - 1] === '0')
|
|
271
|
+
end--;
|
|
272
|
+
this.i = BigInt(digits.slice(0, end));
|
|
273
|
+
this.e += digits.length - end;
|
|
241
274
|
return this;
|
|
242
275
|
}
|
|
243
276
|
/**
|
|
@@ -277,9 +310,9 @@ export class Decimal {
|
|
|
277
310
|
}
|
|
278
311
|
else if (this.e < 0) {
|
|
279
312
|
if (-this.e >= s.length) {
|
|
280
|
-
return m
|
|
313
|
+
return `${m}0.${'0'.repeat(-this.e - s.length)}${s}`;
|
|
281
314
|
}
|
|
282
|
-
return m
|
|
315
|
+
return `${m}${s.slice(0, s.length + this.e)}.${s.slice(s.length + this.e, s.length)}`;
|
|
283
316
|
}
|
|
284
317
|
else {
|
|
285
318
|
return m + s;
|
|
@@ -307,8 +340,16 @@ export class Decimal {
|
|
|
307
340
|
* @returns {boolean}
|
|
308
341
|
*/
|
|
309
342
|
eq(x) {
|
|
310
|
-
|
|
311
|
-
|
|
343
|
+
if (x == null || typeof x === 'boolean')
|
|
344
|
+
return false;
|
|
345
|
+
let other;
|
|
346
|
+
try {
|
|
347
|
+
other = decimal(x);
|
|
348
|
+
}
|
|
349
|
+
catch {
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
return this.i === other.i && (this.e === other.e || this.i === 0n);
|
|
312
353
|
}
|
|
313
354
|
/**
|
|
314
355
|
* Returns `true` if this value is less than the value of `x`, otherwise `false`.
|
|
@@ -334,9 +375,10 @@ export class Decimal {
|
|
|
334
375
|
* @returns {number}
|
|
335
376
|
*/
|
|
336
377
|
compare(x) {
|
|
337
|
-
|
|
378
|
+
const other = x == null || typeof x === 'boolean' ? decimal(x) : x;
|
|
379
|
+
if (this.eq(other))
|
|
338
380
|
return 0;
|
|
339
|
-
if (this.lt(
|
|
381
|
+
if (this.lt(other))
|
|
340
382
|
return -1;
|
|
341
383
|
return 1;
|
|
342
384
|
}
|
|
@@ -367,11 +409,5 @@ export class Decimal {
|
|
|
367
409
|
}
|
|
368
410
|
/** Returns a BigInt that is equal to `i` times `10^e` */
|
|
369
411
|
function exp(i, e) {
|
|
370
|
-
|
|
371
|
-
// The implementation is faster than 10n ** BigInt(e). But could we make it faster still?
|
|
372
|
-
let ii = i;
|
|
373
|
-
for (let j = 0; j < e; j++) {
|
|
374
|
-
ii *= 10n;
|
|
375
|
-
}
|
|
376
|
-
return ii;
|
|
412
|
+
return i * 10n ** BigInt(e);
|
|
377
413
|
}
|
|
@@ -3,25 +3,16 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module functools
|
|
5
5
|
*/
|
|
6
|
-
import { Function } from '../globals/index.js';
|
|
6
|
+
import type { Function } from '../globals/index.js';
|
|
7
7
|
/**
|
|
8
|
-
* Creates a function that invokes `
|
|
9
|
-
* @param {Function} fn The function to bind.
|
|
10
|
-
* @param {*} thisArg The `this` value of `fn`.
|
|
11
|
-
* @param {...*} [partials] The arguments to prepend to `fn`.
|
|
12
|
-
* @returns {Function} Returns the new bound function.
|
|
13
|
-
* @see {@link partial}
|
|
14
|
-
*/
|
|
15
|
-
export declare function bind(fn: Function, thisArg: any, ...partials: any[]): Function;
|
|
16
|
-
/**
|
|
17
|
-
* Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least arity number of arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on. The arity of `func` may be specified if `func.length` is not sufficient.
|
|
8
|
+
* Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least `func.length` arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on.
|
|
18
9
|
* @param {Function} func The function to curry.
|
|
19
10
|
* @returns Returns the new curried function.
|
|
20
11
|
*/
|
|
21
12
|
export declare function curry(func: Function): Function<ReturnType<typeof func>>;
|
|
22
13
|
/**
|
|
23
14
|
* Creates a function that invokes `fn` with partials prepended to the arguments it receives.
|
|
24
|
-
* **Note:** The given function will have `this` bound to `undefined`. If using `this` inside `fn`, consider
|
|
15
|
+
* **Note:** The given function will have `this` bound to `undefined`. If using `this` inside `fn`, consider `Function.prototype.bind` instead.
|
|
25
16
|
* @param {Function} fn The function to bind.
|
|
26
17
|
* @param {...*} [args] The arguments to apply to `fn`.
|
|
27
18
|
* @returns {Function} Returns the new partially applied function
|
|
@@ -52,15 +43,6 @@ export declare function compose(...args: Function[]): Function;
|
|
|
52
43
|
* @return {Function} Returns the new memoized function.
|
|
53
44
|
*/
|
|
54
45
|
export declare function memoize<T>(func: Function<T>, resolver?: Function): Function<T>;
|
|
55
|
-
/**
|
|
56
|
-
* Can be used as a decorator on any function or method to memoize its results.
|
|
57
|
-
* **NOTE:** By default the given function is memoized using it's first argument, regardless of others.
|
|
58
|
-
* @param target
|
|
59
|
-
* @param name
|
|
60
|
-
* @param descriptor
|
|
61
|
-
* @returns A memoized version of the given function
|
|
62
|
-
*/
|
|
63
|
-
export declare function cache(target: any, name: any, descriptor: any): any;
|
|
64
46
|
/**
|
|
65
47
|
* Creates a debounced function that delays invoking `func` until after `wait` milliseconds have elapsed since the last time the debounced function was invoked. Subsequent calls to the debounced function return the result of the last successful invocation. Provide options to set the `maxWait` milliseconds, regardless of when the function was last invoked.
|
|
66
48
|
*
|
|
@@ -97,9 +79,9 @@ single(3) // 1
|
|
|
97
79
|
```
|
|
98
80
|
*/
|
|
99
81
|
export declare function once<T>(func: Function<T>): Function<T>;
|
|
100
|
-
export
|
|
101
|
-
export
|
|
102
|
-
export
|
|
82
|
+
export type Maybe<T> = T | undefined;
|
|
83
|
+
export type Result<T, E = any> = [Maybe<T>, Maybe<E>];
|
|
84
|
+
export type Optional<T> = Maybe<T>;
|
|
103
85
|
/**
|
|
104
86
|
* Wraps a function to catch any exceptions inside and return a {@link Result} tuple with the value and error, if any.
|
|
105
87
|
* @template T,E
|
|
@@ -122,9 +104,3 @@ export declare function resultAsync<T = any, E = unknown>(fn: Function<Promise<T
|
|
|
122
104
|
* @returns {Function<Promise<T>>} A function that wraps `fun` and returns a `Promise`.
|
|
123
105
|
*/
|
|
124
106
|
export declare function promisify<T>(fun: Function, thisArg?: any): Function<Promise<T>>;
|
|
125
|
-
/**
|
|
126
|
-
* Invokes a given function that accepts callback arguments for success, error and returns a `Promise` of the results.
|
|
127
|
-
* @param {Function} fn A function that accepts callback arguments for success, error.
|
|
128
|
-
* @returns {Promise<T>} A `Promise` that will be resolved when `successCallback` is called or rejected when `errorCallback` is called.
|
|
129
|
-
*/
|
|
130
|
-
export declare function callAsync<T = any>(fn: (successCallback: (data: T) => any, errorCallback: Function) => any): Promise<T>;
|
package/dist/functools/index.js
CHANGED
|
@@ -4,18 +4,7 @@
|
|
|
4
4
|
* @module functools
|
|
5
5
|
*/
|
|
6
6
|
/**
|
|
7
|
-
* Creates a function that invokes `
|
|
8
|
-
* @param {Function} fn The function to bind.
|
|
9
|
-
* @param {*} thisArg The `this` value of `fn`.
|
|
10
|
-
* @param {...*} [partials] The arguments to prepend to `fn`.
|
|
11
|
-
* @returns {Function} Returns the new bound function.
|
|
12
|
-
* @see {@link partial}
|
|
13
|
-
*/
|
|
14
|
-
export function bind(fn, thisArg, ...partials) {
|
|
15
|
-
return fn.bind(thisArg, ...partials);
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least arity number of arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on. The arity of `func` may be specified if `func.length` is not sufficient.
|
|
7
|
+
* Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least `func.length` arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on.
|
|
19
8
|
* @param {Function} func The function to curry.
|
|
20
9
|
* @returns Returns the new curried function.
|
|
21
10
|
*/
|
|
@@ -31,13 +20,13 @@ export function curry(func) {
|
|
|
31
20
|
}
|
|
32
21
|
/**
|
|
33
22
|
* Creates a function that invokes `fn` with partials prepended to the arguments it receives.
|
|
34
|
-
* **Note:** The given function will have `this` bound to `undefined`. If using `this` inside `fn`, consider
|
|
23
|
+
* **Note:** The given function will have `this` bound to `undefined`. If using `this` inside `fn`, consider `Function.prototype.bind` instead.
|
|
35
24
|
* @param {Function} fn The function to bind.
|
|
36
25
|
* @param {...*} [args] The arguments to apply to `fn`.
|
|
37
26
|
* @returns {Function} Returns the new partially applied function
|
|
38
27
|
*/
|
|
39
28
|
export function partial(fn, ...args) {
|
|
40
|
-
return bind(
|
|
29
|
+
return fn.bind(undefined, ...args);
|
|
41
30
|
}
|
|
42
31
|
/**
|
|
43
32
|
* Creates a function composition from a given set of functions that will be each applied on the result of the previous one from left to right.
|
|
@@ -74,31 +63,15 @@ export function compose(...args) {
|
|
|
74
63
|
*/
|
|
75
64
|
export function memoize(func, resolver) {
|
|
76
65
|
const cache = new Map();
|
|
77
|
-
return (...args)
|
|
78
|
-
const key = resolver ? resolver(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const value = func(...args);
|
|
66
|
+
return function (...args) {
|
|
67
|
+
const key = resolver ? resolver.apply(this, args) : args[0];
|
|
68
|
+
if (cache.has(key))
|
|
69
|
+
return cache.get(key);
|
|
70
|
+
const value = func.apply(this, args);
|
|
83
71
|
cache.set(key, value);
|
|
84
72
|
return value;
|
|
85
73
|
};
|
|
86
74
|
}
|
|
87
|
-
/**
|
|
88
|
-
* Can be used as a decorator on any function or method to memoize its results.
|
|
89
|
-
* **NOTE:** By default the given function is memoized using it's first argument, regardless of others.
|
|
90
|
-
* @param target
|
|
91
|
-
* @param name
|
|
92
|
-
* @param descriptor
|
|
93
|
-
* @returns A memoized version of the given function
|
|
94
|
-
*/
|
|
95
|
-
export function cache(target, name, descriptor) {
|
|
96
|
-
const original = descriptor.value;
|
|
97
|
-
if (typeof original === 'function') {
|
|
98
|
-
descriptor.value = memoize(original);
|
|
99
|
-
}
|
|
100
|
-
return descriptor;
|
|
101
|
-
}
|
|
102
75
|
/**
|
|
103
76
|
* Creates a debounced function that delays invoking `func` until after `wait` milliseconds have elapsed since the last time the debounced function was invoked. Subsequent calls to the debounced function return the result of the last successful invocation. Provide options to set the `maxWait` milliseconds, regardless of when the function was last invoked.
|
|
104
77
|
*
|
|
@@ -108,20 +81,21 @@ export function cache(target, name, descriptor) {
|
|
|
108
81
|
* @returns Returns the new debounced function.
|
|
109
82
|
*/
|
|
110
83
|
export function debounce(func, wait, { maxWait } = {}) {
|
|
111
|
-
let timer, maxTimer, lastResult;
|
|
112
|
-
return (...args)
|
|
84
|
+
let timer, maxTimer, lastResult, latestCall;
|
|
85
|
+
return function (...args) {
|
|
86
|
+
latestCall = () => func.apply(this, args);
|
|
113
87
|
if (timer)
|
|
114
88
|
clearTimeout(timer);
|
|
115
89
|
timer = setTimeout(() => {
|
|
116
90
|
clearTimeout(maxTimer);
|
|
117
91
|
maxTimer = null;
|
|
118
|
-
lastResult = func(
|
|
92
|
+
lastResult = func.apply(this, args);
|
|
119
93
|
}, wait);
|
|
120
94
|
if (maxWait && !maxTimer) {
|
|
121
95
|
maxTimer = setTimeout(() => {
|
|
122
96
|
clearTimeout(timer);
|
|
123
97
|
maxTimer = null;
|
|
124
|
-
lastResult =
|
|
98
|
+
lastResult = latestCall();
|
|
125
99
|
}, maxWait);
|
|
126
100
|
}
|
|
127
101
|
return lastResult;
|
|
@@ -136,14 +110,16 @@ export function debounce(func, wait, { maxWait } = {}) {
|
|
|
136
110
|
*/
|
|
137
111
|
export function throttle(func, wait) {
|
|
138
112
|
let pending = false;
|
|
139
|
-
|
|
113
|
+
let lastResult;
|
|
114
|
+
return function (...args) {
|
|
140
115
|
if (pending)
|
|
141
|
-
return;
|
|
142
|
-
func(
|
|
116
|
+
return lastResult;
|
|
117
|
+
lastResult = func.apply(this, args);
|
|
143
118
|
pending = true;
|
|
144
119
|
setTimeout(() => {
|
|
145
120
|
pending = false;
|
|
146
121
|
}, wait);
|
|
122
|
+
return lastResult;
|
|
147
123
|
};
|
|
148
124
|
}
|
|
149
125
|
/**
|
|
@@ -164,9 +140,9 @@ single(3) // 1
|
|
|
164
140
|
*/
|
|
165
141
|
export function once(func) {
|
|
166
142
|
let result, isCalled;
|
|
167
|
-
return (...args)
|
|
143
|
+
return function (...args) {
|
|
168
144
|
if (!isCalled) {
|
|
169
|
-
result = func(
|
|
145
|
+
result = func.apply(this, args);
|
|
170
146
|
isCalled = true;
|
|
171
147
|
}
|
|
172
148
|
return result;
|
|
@@ -179,10 +155,10 @@ export function once(func) {
|
|
|
179
155
|
* @returns {Function} A new function that wraps `fn` and returns the result tuple.
|
|
180
156
|
*/
|
|
181
157
|
export function result(fn) {
|
|
182
|
-
return (...args)
|
|
158
|
+
return function (...args) {
|
|
183
159
|
let value, err;
|
|
184
160
|
try {
|
|
185
|
-
value = fn(
|
|
161
|
+
value = fn.apply(this, args);
|
|
186
162
|
}
|
|
187
163
|
catch (error) {
|
|
188
164
|
err = error;
|
|
@@ -197,10 +173,10 @@ export function result(fn) {
|
|
|
197
173
|
* @returns {Function<Promise<Result<T, E>>>} A new function that awaits the given function and returns the result tuple.
|
|
198
174
|
*/
|
|
199
175
|
export function resultAsync(fn) {
|
|
200
|
-
return async (...args)
|
|
176
|
+
return async function (...args) {
|
|
201
177
|
let value, err;
|
|
202
178
|
try {
|
|
203
|
-
value = await fn(
|
|
179
|
+
value = await fn.apply(this, args);
|
|
204
180
|
}
|
|
205
181
|
catch (error) {
|
|
206
182
|
err = error;
|
|
@@ -225,11 +201,3 @@ export function promisify(fun, thisArg) {
|
|
|
225
201
|
}));
|
|
226
202
|
};
|
|
227
203
|
}
|
|
228
|
-
/**
|
|
229
|
-
* Invokes a given function that accepts callback arguments for success, error and returns a `Promise` of the results.
|
|
230
|
-
* @param {Function} fn A function that accepts callback arguments for success, error.
|
|
231
|
-
* @returns {Promise<T>} A `Promise` that will be resolved when `successCallback` is called or rejected when `errorCallback` is called.
|
|
232
|
-
*/
|
|
233
|
-
export async function callAsync(fn) {
|
|
234
|
-
return await new Promise(fn);
|
|
235
|
-
}
|
package/dist/globals/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
/**
|
|
7
7
|
* A generic function type with arbitrary arguments and return.
|
|
8
8
|
*/
|
|
9
|
-
export
|
|
9
|
+
export type Function<TReturn = any> = (...args: any) => TReturn;
|
|
10
10
|
/** This is just a noop function. */
|
|
11
11
|
export declare function noop(): void;
|
|
12
12
|
/** A function that always returns `true`. */
|
|
@@ -22,9 +22,10 @@ export declare class CustomError extends Error {
|
|
|
22
22
|
/**
|
|
23
23
|
* Returns a new CustomError with the specified message.
|
|
24
24
|
* @param {string} message
|
|
25
|
+
* @param {ErrorOptions} [options] Standard error options such as `cause`.
|
|
25
26
|
* @constructor
|
|
26
27
|
*/
|
|
27
|
-
constructor(message?: string);
|
|
28
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
28
29
|
}
|
|
29
30
|
/**
|
|
30
31
|
* Custom error to signal an invalid index access.
|
|
@@ -89,8 +90,6 @@ export declare function isDate(x: any): x is Date;
|
|
|
89
90
|
export declare function isObject(x: any): x is Object;
|
|
90
91
|
/** Check if value is a string type. */
|
|
91
92
|
export declare function isString(x: any): x is string;
|
|
92
|
-
/** Check if value is an Array type. */
|
|
93
|
-
export declare function isArray(x: any): x is any[];
|
|
94
93
|
/**
|
|
95
94
|
* Check if value is Array-like type.
|
|
96
95
|
* A value is considered array-like if it's not a function and has a `.length` number property.
|
|
@@ -98,8 +97,6 @@ export declare function isArray(x: any): x is any[];
|
|
|
98
97
|
export declare function isArrayLike(x: any): boolean;
|
|
99
98
|
/** Check if value is a number type. */
|
|
100
99
|
export declare function isNumber(x: any): x is number;
|
|
101
|
-
/** Check if value is an integer number type. */
|
|
102
|
-
export declare function isInteger(x: any): x is number;
|
|
103
100
|
/** Check if value is a bigint type. */
|
|
104
101
|
export declare function isBigint(x: any): x is BigInt;
|
|
105
102
|
/** Check if value is a Promise type. */
|
|
@@ -175,20 +172,6 @@ export declare function call<T = any>(obj: any, key: PropertyKey, ...args: any[]
|
|
|
175
172
|
export declare function chr(x: number): string;
|
|
176
173
|
/** Convert character to Unicode code point */
|
|
177
174
|
export declare function ord(x: string): number;
|
|
178
|
-
/**
|
|
179
|
-
* Returns an array of the own enumerable property names of `object`.
|
|
180
|
-
* @param object The given object.
|
|
181
|
-
* @returns The array of object keys.
|
|
182
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys Object.keys()}
|
|
183
|
-
*/
|
|
184
|
-
export declare function keys<T>(object: T): Array<keyof T>;
|
|
185
|
-
/**
|
|
186
|
-
* Returns an array of the own enumerable property values of `object`.
|
|
187
|
-
* @param object The given object.
|
|
188
|
-
* @returns The array of object keys.
|
|
189
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values Object.values()}
|
|
190
|
-
*/
|
|
191
|
-
export declare function values<T = any>(object: any): T[];
|
|
192
175
|
/**
|
|
193
176
|
* Check if a given property is present in a given object.
|
|
194
177
|
* @param obj The object to check.
|
package/dist/globals/index.js
CHANGED
|
@@ -18,13 +18,16 @@ export class CustomError extends Error {
|
|
|
18
18
|
/**
|
|
19
19
|
* Returns a new CustomError with the specified message.
|
|
20
20
|
* @param {string} message
|
|
21
|
+
* @param {ErrorOptions} [options] Standard error options such as `cause`.
|
|
21
22
|
* @constructor
|
|
22
23
|
*/
|
|
23
|
-
constructor(message) {
|
|
24
|
-
super(message);
|
|
24
|
+
constructor(message, options) {
|
|
25
|
+
super(message, options);
|
|
25
26
|
this.name = this.constructor.name;
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
// V8 and Bun have it, and only Node's types declare it, which JSR's type check does not load
|
|
28
|
+
const errorType = Error;
|
|
29
|
+
if (typeof errorType.captureStackTrace === 'function') {
|
|
30
|
+
errorType.captureStackTrace(this, this.constructor);
|
|
28
31
|
}
|
|
29
32
|
else {
|
|
30
33
|
this.stack = new Error(message).stack;
|
|
@@ -90,7 +93,7 @@ export function type(value) {
|
|
|
90
93
|
return 'error';
|
|
91
94
|
if (value[Symbol.toStringTag])
|
|
92
95
|
return value[Symbol.toStringTag];
|
|
93
|
-
const className =
|
|
96
|
+
const className = Object.prototype.toString.call(value);
|
|
94
97
|
if (className === '[object Window]')
|
|
95
98
|
return 'object';
|
|
96
99
|
if (className.endsWith('Array]') || Array.isArray(value)) {
|
|
@@ -150,10 +153,6 @@ export function isObject(x) {
|
|
|
150
153
|
export function isString(x) {
|
|
151
154
|
return type(x) === 'string';
|
|
152
155
|
}
|
|
153
|
-
/** Check if value is an Array type. */
|
|
154
|
-
export function isArray(x) {
|
|
155
|
-
return Array.isArray(x);
|
|
156
|
-
}
|
|
157
156
|
/**
|
|
158
157
|
* Check if value is Array-like type.
|
|
159
158
|
* A value is considered array-like if it's not a function and has a `.length` number property.
|
|
@@ -166,10 +165,6 @@ export function isArrayLike(x) {
|
|
|
166
165
|
export function isNumber(x) {
|
|
167
166
|
return type(x) === 'number';
|
|
168
167
|
}
|
|
169
|
-
/** Check if value is an integer number type. */
|
|
170
|
-
export function isInteger(x) {
|
|
171
|
-
return type(x) === 'number' && Math.trunc(x) === x;
|
|
172
|
-
}
|
|
173
168
|
/** Check if value is a bigint type. */
|
|
174
169
|
export function isBigint(x) {
|
|
175
170
|
return type(x) === 'bigint';
|
|
@@ -180,15 +175,15 @@ export function isPromise(x) {
|
|
|
180
175
|
}
|
|
181
176
|
/** Check if value is a function type. */
|
|
182
177
|
export function isFunction(x) {
|
|
183
|
-
return x
|
|
178
|
+
return typeof x === 'function';
|
|
184
179
|
}
|
|
185
180
|
/** Check if value is an async function type. */
|
|
186
181
|
export function isAsyncFunction(x) {
|
|
187
|
-
return x?.constructor
|
|
182
|
+
return x?.constructor?.name === 'AsyncFunction';
|
|
188
183
|
}
|
|
189
184
|
/** Check if value is a generator function type. */
|
|
190
185
|
export function isGenerator(x) {
|
|
191
|
-
return x?.constructor
|
|
186
|
+
return x?.constructor?.constructor?.name === 'GeneratorFunction';
|
|
192
187
|
}
|
|
193
188
|
/** Check if value is `null` or `undefined`. */
|
|
194
189
|
export function isNull(x) {
|
|
@@ -282,7 +277,7 @@ uuid()
|
|
|
282
277
|
*/
|
|
283
278
|
export function uuid() {
|
|
284
279
|
const u = Date.now().toString(16) + Math.random().toString(16) + '0'.repeat(16);
|
|
285
|
-
return [u.
|
|
280
|
+
return [u.slice(0, 8), u.slice(8, 12), `4000-8${u.slice(13, 16)}`, u.slice(16, 28)].join('-');
|
|
286
281
|
}
|
|
287
282
|
/**
|
|
288
283
|
* Checks if `obj.key` is a function, and calls it with any given `args`.
|
|
@@ -292,7 +287,7 @@ export function uuid() {
|
|
|
292
287
|
* @return `obj.key(...args)` or `undefined`.
|
|
293
288
|
*/
|
|
294
289
|
export function call(obj, key, ...args) {
|
|
295
|
-
if (isFunction(get(key, obj))) {
|
|
290
|
+
if (isFunction(get([key], obj))) {
|
|
296
291
|
return obj[key](...args);
|
|
297
292
|
}
|
|
298
293
|
}
|
|
@@ -302,25 +297,7 @@ export function chr(x) {
|
|
|
302
297
|
}
|
|
303
298
|
/** Convert character to Unicode code point */
|
|
304
299
|
export function ord(x) {
|
|
305
|
-
return x.
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Returns an array of the own enumerable property names of `object`.
|
|
309
|
-
* @param object The given object.
|
|
310
|
-
* @returns The array of object keys.
|
|
311
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys Object.keys()}
|
|
312
|
-
*/
|
|
313
|
-
export function keys(object) {
|
|
314
|
-
return Object.keys(object);
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Returns an array of the own enumerable property values of `object`.
|
|
318
|
-
* @param object The given object.
|
|
319
|
-
* @returns The array of object keys.
|
|
320
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values Object.values()}
|
|
321
|
-
*/
|
|
322
|
-
export function values(object) {
|
|
323
|
-
return Object.values(object);
|
|
300
|
+
return x.codePointAt(0) ?? Number.NaN;
|
|
324
301
|
}
|
|
325
302
|
/**
|
|
326
303
|
* Check if a given property is present in a given object.
|
|
@@ -329,7 +306,7 @@ export function values(object) {
|
|
|
329
306
|
* @returns `true` if the object has the property name.
|
|
330
307
|
*/
|
|
331
308
|
export function has(obj, attr) {
|
|
332
|
-
return obj && attr in obj;
|
|
309
|
+
return obj != null && attr in Object(obj);
|
|
333
310
|
}
|
|
334
311
|
export function get(key, obj, value) {
|
|
335
312
|
if (obj == null)
|
|
@@ -372,19 +349,26 @@ export function del(attr, x) {
|
|
|
372
349
|
export function set(attr, obj, value, writable = false, enumerable = false) {
|
|
373
350
|
return Object.defineProperty(obj, attr, {
|
|
374
351
|
value,
|
|
352
|
+
configurable: true,
|
|
375
353
|
enumerable,
|
|
376
354
|
writable
|
|
377
355
|
});
|
|
378
356
|
}
|
|
379
357
|
export const HASH_KEY = Symbol.for('HASH_KEY');
|
|
358
|
+
const sealedHashes = new WeakMap();
|
|
380
359
|
export function hash(obj) {
|
|
381
360
|
if (typeof obj === 'string') {
|
|
382
361
|
return hashCode(obj);
|
|
383
362
|
}
|
|
384
|
-
if (typeof obj === 'object') {
|
|
363
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
385
364
|
if (get(HASH_KEY, obj) != null) {
|
|
386
365
|
return obj[HASH_KEY];
|
|
387
366
|
}
|
|
367
|
+
if (!Object.isExtensible(obj)) {
|
|
368
|
+
if (!sealedHashes.has(obj))
|
|
369
|
+
sealedHashes.set(obj, uniqueId());
|
|
370
|
+
return sealedHashes.get(obj);
|
|
371
|
+
}
|
|
388
372
|
obj[HASH_KEY] = uniqueId();
|
|
389
373
|
return obj[HASH_KEY];
|
|
390
374
|
}
|
package/dist/i18n/currency.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export declare class Currency extends Decimal {
|
|
|
6
6
|
private readonly locale;
|
|
7
7
|
private readonly currency;
|
|
8
8
|
private readonly numberFormat;
|
|
9
|
+
private readonly overrides;
|
|
9
10
|
private readonly options;
|
|
10
11
|
constructor(amount: any, currency: string, locale?: string, opts?: Intl.NumberFormatOptions);
|
|
11
12
|
clone(n: Currency): Currency;
|
|
@@ -15,6 +16,8 @@ export declare class Currency extends Decimal {
|
|
|
15
16
|
div(x: Currency | Decimal | string | number): Currency;
|
|
16
17
|
mul(x: Currency | Decimal): Currency;
|
|
17
18
|
toDecimal(): Decimal;
|
|
19
|
+
/** `Intl` reads a decimal string exactly, while a `Number` past 2^53 has lost digits. */
|
|
20
|
+
private formattable;
|
|
18
21
|
toString(): string;
|
|
19
22
|
format(opts?: Intl.NumberFormatOptions, locale?: string): string;
|
|
20
23
|
displayName(): string;
|