atomic-fns 0.4.2 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -17
- package/dist/collections/BTree.d.ts +1 -1
- package/dist/collections/Trie.d.ts +42 -0
- package/dist/collections/Trie.js +104 -0
- package/dist/collections/deque.d.ts +2 -2
- package/dist/collections/deque.js +6 -8
- package/dist/collections/index.d.ts +208 -106
- package/dist/collections/index.js +179 -436
- package/dist/decimal/index.d.ts +147 -0
- package/dist/decimal/index.js +376 -0
- package/dist/functools/index.d.ts +26 -18
- package/dist/functools/index.js +36 -50
- package/dist/globals/decimal.d.ts +142 -0
- package/dist/globals/decimal.js +371 -0
- package/dist/globals/index.d.ts +19 -31
- package/dist/globals/index.js +9 -24
- package/dist/i18n/currency.d.ts +24 -0
- package/dist/i18n/currency.js +85 -0
- package/dist/i18n/date/format.d.ts +16 -0
- package/dist/i18n/date/format.js +313 -0
- package/dist/i18n/date/index.d.ts +9 -9
- package/dist/i18n/date/test.html +72 -0
- package/dist/i18n/date/utils.d.ts +3 -3
- package/dist/i18n/date/utils.js +6 -28
- package/dist/i18n/format.d.ts +2 -2
- package/dist/i18n/format.js +1 -1
- package/dist/i18n/index.d.ts +1 -0
- package/dist/i18n/index.js +1 -0
- package/dist/itertools/index.d.ts +52 -24
- package/dist/itertools/index.js +4 -32
- package/dist/math/index.d.ts +7 -8
- package/dist/math/index.js +3 -1
- package/dist/operators/index.d.ts +2 -2
- package/dist/operators/index.js +0 -6
- package/dist/random/index.d.ts +1 -1
- package/dist/random/index.js +2 -2
- package/dist/types.d.ts +19 -0
- package/dist/types.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `Decimal` provides support for correct rounded floating point arithmetic.
|
|
3
|
+
*
|
|
4
|
+
* @module decimal
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Casts the given value as a {@link Decimal}.
|
|
8
|
+
*/
|
|
9
|
+
export declare function decimal(x: any): Decimal;
|
|
10
|
+
/**
|
|
11
|
+
* `Decimal` provides support for correct rounded floating point arithmetic, unlike the standard Number type. It also supports user-defined precision (default is 20 decimal places) which can be as large as needed.
|
|
12
|
+
*/
|
|
13
|
+
export declare class Decimal {
|
|
14
|
+
static PRECISION: number;
|
|
15
|
+
private i;
|
|
16
|
+
private e;
|
|
17
|
+
/**
|
|
18
|
+
* Returns a new `Decimal` instance from the value or `0`.
|
|
19
|
+
* @param {*} value
|
|
20
|
+
* @returns {Decimal}
|
|
21
|
+
*/
|
|
22
|
+
constructor(value?: any);
|
|
23
|
+
/**
|
|
24
|
+
* Returns a new `Decimal` that has the opposite sign of this `Decimal`.
|
|
25
|
+
* @example
|
|
26
|
+
```js
|
|
27
|
+
let a = decimal('5')
|
|
28
|
+
a.minus() // -5
|
|
29
|
+
```
|
|
30
|
+
* @returns {Decimal}
|
|
31
|
+
*/
|
|
32
|
+
negated(): Decimal;
|
|
33
|
+
/**
|
|
34
|
+
* Returns a new `Decimal` that is the sum of this and `x`.
|
|
35
|
+
* @param x
|
|
36
|
+
* @example
|
|
37
|
+
```js
|
|
38
|
+
let a = decimal('50000000000')
|
|
39
|
+
let b = decimal('0.000000005')
|
|
40
|
+
a.add(b) // 50000000000.000000005
|
|
41
|
+
```
|
|
42
|
+
* @returns {Decimal}
|
|
43
|
+
*/
|
|
44
|
+
add(x: any): Decimal;
|
|
45
|
+
/**
|
|
46
|
+
* Returns a new `Decimal` that is the difference between this and `x`.
|
|
47
|
+
* @param x
|
|
48
|
+
* @example
|
|
49
|
+
```js
|
|
50
|
+
let a = decimal('1')
|
|
51
|
+
let b = decimal('0.0000000000000000001')
|
|
52
|
+
a.sub(b) // 0.9999999999999999999
|
|
53
|
+
```
|
|
54
|
+
* @returns {Decimal}
|
|
55
|
+
*/
|
|
56
|
+
sub(x: any): Decimal;
|
|
57
|
+
/**
|
|
58
|
+
* Returns a new `Decimal` that is the product of this and `x`.
|
|
59
|
+
* @param x
|
|
60
|
+
* @example
|
|
61
|
+
```js
|
|
62
|
+
let a = decimal('0.0000000000000000000025')
|
|
63
|
+
let b = decimal('400000000000000000000')
|
|
64
|
+
a.mul(b) // 1
|
|
65
|
+
```
|
|
66
|
+
* @returns {Decimal}
|
|
67
|
+
*/
|
|
68
|
+
mul(x: any): Decimal;
|
|
69
|
+
/**
|
|
70
|
+
* Returns a new `Decimal` that is the quotient of this and `x`
|
|
71
|
+
* @param x
|
|
72
|
+
* @example
|
|
73
|
+
```js
|
|
74
|
+
let a = decimal('1')
|
|
75
|
+
let b = decimal('3')
|
|
76
|
+
a.div(b) // 0.333333333333333333333333333333
|
|
77
|
+
```
|
|
78
|
+
* @returns {Decimal}
|
|
79
|
+
*/
|
|
80
|
+
div(x: any): Decimal;
|
|
81
|
+
/**
|
|
82
|
+
* Returns the square root of this `Decimal`.
|
|
83
|
+
* @returns {TDecimal}
|
|
84
|
+
*/
|
|
85
|
+
sqrt(): Decimal;
|
|
86
|
+
/** Returns a new copy of this Decimal value. */
|
|
87
|
+
clone(): Decimal;
|
|
88
|
+
/**
|
|
89
|
+
* Remove zeroes in the least-significant digit of this `Decimal`
|
|
90
|
+
*/
|
|
91
|
+
private _normalize;
|
|
92
|
+
/**
|
|
93
|
+
* Truncate this `Decimal` to the configured precision
|
|
94
|
+
*/
|
|
95
|
+
private _truncate;
|
|
96
|
+
/**
|
|
97
|
+
* Returns a string representation of this decimal.
|
|
98
|
+
* @returns {Decimal}
|
|
99
|
+
*/
|
|
100
|
+
toString(): string;
|
|
101
|
+
/**
|
|
102
|
+
* Converts this decimal to the `Number` value
|
|
103
|
+
* @returns {number}
|
|
104
|
+
*/
|
|
105
|
+
toNumber(): number;
|
|
106
|
+
static get precision(): number;
|
|
107
|
+
static set precision(value: number);
|
|
108
|
+
/**
|
|
109
|
+
* Returns `true` if this value is equal to the value of `x`, otherwise `false`.
|
|
110
|
+
* @param {*} x other
|
|
111
|
+
* @returns {boolean}
|
|
112
|
+
*/
|
|
113
|
+
eq(x: any): boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Returns `true` if this value is less than the value of `x`, otherwise `false`.
|
|
116
|
+
* @param {*} x
|
|
117
|
+
* @returns {boolean}
|
|
118
|
+
*/
|
|
119
|
+
lt(x: any): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Returns a comparison value representing the ordering of this value in respect to `x`.
|
|
122
|
+
* * `-1` if this < x
|
|
123
|
+
* * `1` if this > x
|
|
124
|
+
* * `0` if this == x
|
|
125
|
+
* @param {*} x
|
|
126
|
+
* @returns {number}
|
|
127
|
+
*/
|
|
128
|
+
compare(x: any): 1 | -1 | 0;
|
|
129
|
+
/**
|
|
130
|
+
* Returns `true` if this value is greater than the value of `x`, otherwise `false`.
|
|
131
|
+
* @param {*} x
|
|
132
|
+
* @returns {boolean}
|
|
133
|
+
*/
|
|
134
|
+
gt(x: any): boolean;
|
|
135
|
+
/**
|
|
136
|
+
* Returns `true` if this value is less than or equal to the value of `x`, otherwise `false`.
|
|
137
|
+
* @param {*} x
|
|
138
|
+
* @returns {boolean}
|
|
139
|
+
*/
|
|
140
|
+
lte(x: any): boolean;
|
|
141
|
+
/**
|
|
142
|
+
* Returns `true` if this value is greater than or equal to the value of `x`, otherwise `false`.
|
|
143
|
+
* @param {*} x
|
|
144
|
+
* @returns {boolean}
|
|
145
|
+
*/
|
|
146
|
+
gte(x: any): boolean;
|
|
147
|
+
}
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `Decimal` provides support for correct rounded floating point arithmetic.
|
|
3
|
+
*
|
|
4
|
+
* @module decimal
|
|
5
|
+
*/
|
|
6
|
+
const NUMBER_REGEX = /^(-?)([0-9]+)(\.([0-9]+))?$/;
|
|
7
|
+
/**
|
|
8
|
+
* Casts the given value as a {@link Decimal}.
|
|
9
|
+
*/
|
|
10
|
+
export function decimal(x) {
|
|
11
|
+
return new Decimal(x);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* `Decimal` provides support for correct rounded floating point arithmetic, unlike the standard Number type. It also supports user-defined precision (default is 20 decimal places) which can be as large as needed.
|
|
15
|
+
*/
|
|
16
|
+
export class Decimal {
|
|
17
|
+
static PRECISION = 20;
|
|
18
|
+
i = 0n;
|
|
19
|
+
e = 0;
|
|
20
|
+
/**
|
|
21
|
+
* Returns a new `Decimal` instance from the value or `0`.
|
|
22
|
+
* @param {*} value
|
|
23
|
+
* @returns {Decimal}
|
|
24
|
+
*/
|
|
25
|
+
constructor(value) {
|
|
26
|
+
if (value == null) {
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
// Check if value is a Decimal like
|
|
30
|
+
if (typeof value?.i === 'bigint' && typeof value.e === 'number') {
|
|
31
|
+
this.i = value.i;
|
|
32
|
+
this.e = value.e;
|
|
33
|
+
this._normalize();
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
const result = NUMBER_REGEX.exec(value.toString());
|
|
37
|
+
if (result) {
|
|
38
|
+
// console.log(result);
|
|
39
|
+
// Construct from string if possible
|
|
40
|
+
// this.i = parseInt(result[1] + result[2] + result[4]);
|
|
41
|
+
if (typeof result[2] === 'undefined')
|
|
42
|
+
result[2] = '';
|
|
43
|
+
if (typeof result[4] === 'undefined')
|
|
44
|
+
result[4] = '';
|
|
45
|
+
this.i = BigInt(result[1] + result[2] + result[4]);
|
|
46
|
+
this.e = result[4] ? -result[4].length : 0;
|
|
47
|
+
this._normalize();
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
// Conversion to string failed, so construct from number
|
|
51
|
+
let m = 1n;
|
|
52
|
+
if (value < 0) {
|
|
53
|
+
value = -value;
|
|
54
|
+
m = -1n;
|
|
55
|
+
}
|
|
56
|
+
if (value === 0) {
|
|
57
|
+
this.i = 0n;
|
|
58
|
+
this.e = 0;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// Multiply the number by 10 until it is an integer
|
|
62
|
+
let e = -Math.floor(Math.log10(value));
|
|
63
|
+
if (e > 0) {
|
|
64
|
+
value *= Math.pow(10, e);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
e = 0;
|
|
68
|
+
}
|
|
69
|
+
while (value !== Math.round(value)) {
|
|
70
|
+
e++;
|
|
71
|
+
value *= 10;
|
|
72
|
+
}
|
|
73
|
+
// Divide the number by 10 until the one's digit is non-zero
|
|
74
|
+
while (value % 10 === 0 && value > 0) {
|
|
75
|
+
e--;
|
|
76
|
+
value /= 10;
|
|
77
|
+
}
|
|
78
|
+
value = BigInt(value);
|
|
79
|
+
this.i = value * m;
|
|
80
|
+
this.e = -e;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Returns a new `Decimal` that has the opposite sign of this `Decimal`.
|
|
86
|
+
* @example
|
|
87
|
+
```js
|
|
88
|
+
let a = decimal('5')
|
|
89
|
+
a.minus() // -5
|
|
90
|
+
```
|
|
91
|
+
* @returns {Decimal}
|
|
92
|
+
*/
|
|
93
|
+
negated() {
|
|
94
|
+
const negated = new Decimal();
|
|
95
|
+
negated.i = -this.i;
|
|
96
|
+
negated.e = this.e;
|
|
97
|
+
return negated;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns a new `Decimal` that is the sum of this and `x`.
|
|
101
|
+
* @param x
|
|
102
|
+
* @example
|
|
103
|
+
```js
|
|
104
|
+
let a = decimal('50000000000')
|
|
105
|
+
let b = decimal('0.000000005')
|
|
106
|
+
a.add(b) // 50000000000.000000005
|
|
107
|
+
```
|
|
108
|
+
* @returns {Decimal}
|
|
109
|
+
*/
|
|
110
|
+
add(x) {
|
|
111
|
+
let a = new Decimal(this);
|
|
112
|
+
let b = new Decimal(x);
|
|
113
|
+
// a+b = a.i * 10^a.e + b.i * 10^b.e
|
|
114
|
+
// a+b = ( a.i * 10^(a.e-b.e) + b.i ) * 10^b.e
|
|
115
|
+
const result = new Decimal();
|
|
116
|
+
// Order arguments so that a.e >= b.e
|
|
117
|
+
if (a.e < b.e) {
|
|
118
|
+
const c = a;
|
|
119
|
+
a = b;
|
|
120
|
+
b = c;
|
|
121
|
+
}
|
|
122
|
+
const intPart = exp(a.i, a.e - b.e);
|
|
123
|
+
result.i = intPart + b.i;
|
|
124
|
+
result.e = b.e;
|
|
125
|
+
result._normalize();
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Returns a new `Decimal` that is the difference between this and `x`.
|
|
130
|
+
* @param x
|
|
131
|
+
* @example
|
|
132
|
+
```js
|
|
133
|
+
let a = decimal('1')
|
|
134
|
+
let b = decimal('0.0000000000000000001')
|
|
135
|
+
a.sub(b) // 0.9999999999999999999
|
|
136
|
+
```
|
|
137
|
+
* @returns {Decimal}
|
|
138
|
+
*/
|
|
139
|
+
sub(x) {
|
|
140
|
+
return this.add(decimal(x).negated());
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Returns a new `Decimal` that is the product of this and `x`.
|
|
144
|
+
* @param x
|
|
145
|
+
* @example
|
|
146
|
+
```js
|
|
147
|
+
let a = decimal('0.0000000000000000000025')
|
|
148
|
+
let b = decimal('400000000000000000000')
|
|
149
|
+
a.mul(b) // 1
|
|
150
|
+
```
|
|
151
|
+
* @returns {Decimal}
|
|
152
|
+
*/
|
|
153
|
+
mul(x) {
|
|
154
|
+
const a = new Decimal(this);
|
|
155
|
+
const b = new Decimal(x);
|
|
156
|
+
// a * b = (a.i * b.i) * 10^(a.e + b.e)
|
|
157
|
+
const result = new Decimal();
|
|
158
|
+
result.i = a.i * b.i;
|
|
159
|
+
result.e = a.e + b.e;
|
|
160
|
+
result._normalize();
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Returns a new `Decimal` that is the quotient of this and `x`
|
|
165
|
+
* @param x
|
|
166
|
+
* @example
|
|
167
|
+
```js
|
|
168
|
+
let a = decimal('1')
|
|
169
|
+
let b = decimal('3')
|
|
170
|
+
a.div(b) // 0.333333333333333333333333333333
|
|
171
|
+
```
|
|
172
|
+
* @returns {Decimal}
|
|
173
|
+
*/
|
|
174
|
+
div(x) {
|
|
175
|
+
const a = new Decimal(this);
|
|
176
|
+
const b = new Decimal(x);
|
|
177
|
+
// We need to increase the number of digits of a and b so that a / b will have the desired precision.
|
|
178
|
+
// In order for the integer quotient to have the correct precision, a.e - b.e must be greater than that precision.
|
|
179
|
+
// So we need only to increase a.e.
|
|
180
|
+
// TODO: Revisit the value of increaseA. Is it correct in every circumstance?
|
|
181
|
+
const increaseA = Math.max(Decimal.precision - (a.e + b.e), 0);
|
|
182
|
+
const intIncrease = new Decimal();
|
|
183
|
+
intIncrease.i = exp(a.i, increaseA);
|
|
184
|
+
const quotient = new Decimal();
|
|
185
|
+
quotient.i = intIncrease.i / b.i;
|
|
186
|
+
quotient.e = a.e - b.e - increaseA;
|
|
187
|
+
// No need to truncate since we already increased the number of digits before dividing
|
|
188
|
+
quotient._normalize();
|
|
189
|
+
return quotient;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Returns the square root of this `Decimal`.
|
|
193
|
+
* @returns {TDecimal}
|
|
194
|
+
*/
|
|
195
|
+
sqrt() {
|
|
196
|
+
Decimal.PRECISION++;
|
|
197
|
+
// Let the guess value be 10^(e/2), where e is this.e
|
|
198
|
+
let x = new Decimal();
|
|
199
|
+
x.i = 1n;
|
|
200
|
+
x.e = Math.floor(this.e / 2);
|
|
201
|
+
const onehalf = new Decimal('0.5');
|
|
202
|
+
let xt = x.clone();
|
|
203
|
+
xt._truncate();
|
|
204
|
+
xt._normalize();
|
|
205
|
+
let lastxt;
|
|
206
|
+
let i;
|
|
207
|
+
for (i = 0; i < 100; i++) {
|
|
208
|
+
lastxt = xt;
|
|
209
|
+
x = this.div(x).add(x).mul(onehalf);
|
|
210
|
+
xt = x.clone();
|
|
211
|
+
xt._truncate();
|
|
212
|
+
xt._normalize();
|
|
213
|
+
if (xt.i === lastxt.i && xt.e === lastxt.e)
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
if (i >= 100) {
|
|
217
|
+
console.log('Warning: sqrt exceeded maximum iterations. (Did it enter a cycle?)');
|
|
218
|
+
}
|
|
219
|
+
Decimal.PRECISION--;
|
|
220
|
+
xt._truncate();
|
|
221
|
+
xt._normalize();
|
|
222
|
+
return xt;
|
|
223
|
+
}
|
|
224
|
+
/** Returns a new copy of this Decimal value. */
|
|
225
|
+
clone() {
|
|
226
|
+
const a = new Decimal();
|
|
227
|
+
a.i = this.i;
|
|
228
|
+
a.e = this.e;
|
|
229
|
+
return a;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Remove zeroes in the least-significant digit of this `Decimal`
|
|
233
|
+
*/
|
|
234
|
+
_normalize() {
|
|
235
|
+
while (this.i % 10n === 0n && this.i !== 0n) {
|
|
236
|
+
this.i /= 10n;
|
|
237
|
+
this.e++;
|
|
238
|
+
}
|
|
239
|
+
return this;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Truncate this `Decimal` to the configured precision
|
|
243
|
+
*/
|
|
244
|
+
_truncate() {
|
|
245
|
+
// TODO: Make this better
|
|
246
|
+
// TODO: Rounding
|
|
247
|
+
const trunc = -this.e - Decimal.PRECISION;
|
|
248
|
+
if (trunc > 0) {
|
|
249
|
+
this.i /= 10n ** BigInt(trunc - 1);
|
|
250
|
+
this.e += trunc;
|
|
251
|
+
const nextDigit = this.i % 10n;
|
|
252
|
+
if (nextDigit >= 5) {
|
|
253
|
+
this.i += 10n;
|
|
254
|
+
}
|
|
255
|
+
this.i /= 10n;
|
|
256
|
+
}
|
|
257
|
+
// while(-this.e > Decimal.PRECISION) {
|
|
258
|
+
// this.i /= 10n;
|
|
259
|
+
// this.e++;
|
|
260
|
+
// }
|
|
261
|
+
return this;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Returns a string representation of this decimal.
|
|
265
|
+
* @returns {Decimal}
|
|
266
|
+
*/
|
|
267
|
+
toString() {
|
|
268
|
+
let s = this.i.toString();
|
|
269
|
+
let m = '';
|
|
270
|
+
if (s[0] === '-') {
|
|
271
|
+
s = s.substring(1, s.length);
|
|
272
|
+
m = '-';
|
|
273
|
+
}
|
|
274
|
+
if (this.e > 0) {
|
|
275
|
+
return m + s + '0'.repeat(this.e);
|
|
276
|
+
}
|
|
277
|
+
else if (this.e < 0) {
|
|
278
|
+
if (-this.e >= s.length) {
|
|
279
|
+
return m + '0.' + '0'.repeat(-this.e - s.length) + s;
|
|
280
|
+
}
|
|
281
|
+
return m + s.slice(0, s.length + this.e) + '.' + s.slice(s.length + this.e, s.length);
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
return m + s;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
288
|
+
return this.toString();
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Converts this decimal to the `Number` value
|
|
292
|
+
* @returns {number}
|
|
293
|
+
*/
|
|
294
|
+
toNumber() {
|
|
295
|
+
return Number(Decimal.prototype.toString.call(this));
|
|
296
|
+
}
|
|
297
|
+
static get precision() {
|
|
298
|
+
return Decimal.PRECISION;
|
|
299
|
+
}
|
|
300
|
+
static set precision(value) {
|
|
301
|
+
Decimal.PRECISION = value;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Returns `true` if this value is equal to the value of `x`, otherwise `false`.
|
|
305
|
+
* @param {*} x other
|
|
306
|
+
* @returns {boolean}
|
|
307
|
+
*/
|
|
308
|
+
eq(x) {
|
|
309
|
+
const other = decimal(x);
|
|
310
|
+
return this.i === other.i && this.e === other.e;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Returns `true` if this value is less than the value of `x`, otherwise `false`.
|
|
314
|
+
* @param {*} x
|
|
315
|
+
* @returns {boolean}
|
|
316
|
+
*/
|
|
317
|
+
lt(x) {
|
|
318
|
+
const other = decimal(x);
|
|
319
|
+
if (this.e === other.e)
|
|
320
|
+
return this.i < other.i;
|
|
321
|
+
const e = this.e - other.e;
|
|
322
|
+
if (e > 0) {
|
|
323
|
+
return exp(this.i, e) < other.i;
|
|
324
|
+
}
|
|
325
|
+
return this.i < exp(other.i, -e);
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Returns a comparison value representing the ordering of this value in respect to `x`.
|
|
329
|
+
* * `-1` if this < x
|
|
330
|
+
* * `1` if this > x
|
|
331
|
+
* * `0` if this == x
|
|
332
|
+
* @param {*} x
|
|
333
|
+
* @returns {number}
|
|
334
|
+
*/
|
|
335
|
+
compare(x) {
|
|
336
|
+
if (this.eq(x))
|
|
337
|
+
return 0;
|
|
338
|
+
if (this.lt(x))
|
|
339
|
+
return -1;
|
|
340
|
+
return 1;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Returns `true` if this value is greater than the value of `x`, otherwise `false`.
|
|
344
|
+
* @param {*} x
|
|
345
|
+
* @returns {boolean}
|
|
346
|
+
*/
|
|
347
|
+
gt(x) {
|
|
348
|
+
return this.compare(x) > 0;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Returns `true` if this value is less than or equal to the value of `x`, otherwise `false`.
|
|
352
|
+
* @param {*} x
|
|
353
|
+
* @returns {boolean}
|
|
354
|
+
*/
|
|
355
|
+
lte(x) {
|
|
356
|
+
return this.compare(x) <= 0;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Returns `true` if this value is greater than or equal to the value of `x`, otherwise `false`.
|
|
360
|
+
* @param {*} x
|
|
361
|
+
* @returns {boolean}
|
|
362
|
+
*/
|
|
363
|
+
gte(x) {
|
|
364
|
+
return this.compare(x) >= 0;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
/** Returns a BigInt that is equal to `i` times `10^e` */
|
|
368
|
+
function exp(i, e) {
|
|
369
|
+
// TODO: Make this better
|
|
370
|
+
// The implementation is faster than 10n ** BigInt(e). But could we make it faster still?
|
|
371
|
+
let ii = i;
|
|
372
|
+
for (let j = 0; j < e; j++) {
|
|
373
|
+
ii *= 10n;
|
|
374
|
+
}
|
|
375
|
+
return ii;
|
|
376
|
+
}
|
|
@@ -16,10 +16,9 @@ export declare const bind: (fn: Function, thisArg: any, ...partials: any[]) => a
|
|
|
16
16
|
/**
|
|
17
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.
|
|
18
18
|
* @param {Function} func The function to curry.
|
|
19
|
-
* @param {number} [arity=func.length]
|
|
20
19
|
* @returns Returns the new curried function.
|
|
21
20
|
*/
|
|
22
|
-
export declare const curry: (func: Function
|
|
21
|
+
export declare const curry: (func: Function) => (...args: any[]) => any;
|
|
23
22
|
/**
|
|
24
23
|
* Creates a function that invokes `fn` with partials prepended to the arguments it receives.
|
|
25
24
|
* **Note:** The given function will have `this` bound to `undefined`. If using `this` inside `fn`, consider {@link bind} instead.
|
|
@@ -39,6 +38,11 @@ export declare const flow: (...args: any[]) => (x: any) => any;
|
|
|
39
38
|
* @see {@link flow}
|
|
40
39
|
*/
|
|
41
40
|
export declare const pipe: (...args: any[]) => (x: any) => any;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a function composition from a given set of functions that will be each applied on the result of the previous one from right to left.
|
|
43
|
+
* @param {...*} [args] The set of functions to apply.
|
|
44
|
+
* @returns {Function} A new function that applies each given function on the result of the previous step.
|
|
45
|
+
*/
|
|
42
46
|
export declare const compose: (...args: any[]) => (x: any) => any;
|
|
43
47
|
/**
|
|
44
48
|
* Creates a function that memoizes the result of `func`. If `resolver` is provided, it determines the cache key for storing the result based on the arguments provided to the memoized function. By default, the first argument provided to the memoized function is used as the map cache key.
|
|
@@ -47,7 +51,7 @@ export declare const compose: (...args: any[]) => (x: any) => any;
|
|
|
47
51
|
* @param {Function} [resolver] The function to resolve the cache key.
|
|
48
52
|
* @return {Function} Returns the new memoized function.
|
|
49
53
|
*/
|
|
50
|
-
export declare function memoize<T>(func: Function<T>, resolver?: Function): Function<
|
|
54
|
+
export declare function memoize<T>(func: Function<T>, resolver?: Function): Function<T>;
|
|
51
55
|
/**
|
|
52
56
|
* Can be used as a decorator on any function or method to memoize its results.
|
|
53
57
|
* **NOTE:** By default the given function is memoized using it's first argument, regardless of others.
|
|
@@ -92,31 +96,35 @@ single(3) // 1
|
|
|
92
96
|
// `func` is invoked only once.
|
|
93
97
|
```
|
|
94
98
|
*/
|
|
95
|
-
export declare function once<T>(func: Function<T>):
|
|
99
|
+
export declare function once<T>(func: Function<T>): Function<T>;
|
|
96
100
|
export declare type Maybe<T> = T | undefined;
|
|
97
101
|
export declare type Result<T, E = any> = [Maybe<T>, Maybe<E>];
|
|
98
102
|
export declare type Optional<T> = Maybe<T>;
|
|
99
103
|
/**
|
|
100
|
-
* Wraps a function to catch any exceptions inside and return a
|
|
104
|
+
* Wraps a function to catch any exceptions inside and return a {@link Result} tuple with the value and error, if any.
|
|
101
105
|
* @template T,E
|
|
102
|
-
* @param {Function}
|
|
103
|
-
* @
|
|
104
|
-
* @returns {Function} A new function that wraps `fun`.
|
|
106
|
+
* @param {Function<T>} fn The function to wrap.
|
|
107
|
+
* @returns {Function} A new function that wraps `fn` and returns the result tuple.
|
|
105
108
|
*/
|
|
106
|
-
export declare function result<T = any, E = unknown>(
|
|
109
|
+
export declare function result<T = any, E = unknown>(fn: Function<T>): Function<Result<T, E>>;
|
|
107
110
|
/**
|
|
108
|
-
* Wraps
|
|
111
|
+
* Wraps an async function to catch any exceptions inside and return a {@link Result} tuple with the value and error, if any.
|
|
109
112
|
* @template T,E
|
|
110
|
-
* @param {Function<
|
|
111
|
-
* @
|
|
112
|
-
* @returns {Function<Promise<Result<T, E>>>} A new function that awaits the `awaitable` param and returns a {@link Result} type.
|
|
113
|
+
* @param {Function<Promise<T>>} fn The async function to await.
|
|
114
|
+
* @returns {Function<Promise<Result<T, E>>>} A new function that awaits the given function and returns the result tuple.
|
|
113
115
|
*/
|
|
114
|
-
export declare function resultAsync<T = any, E = unknown>(
|
|
116
|
+
export declare function resultAsync<T = any, E = unknown>(fn: Function<Promise<T>>): Function<Promise<Result<T, E>>>;
|
|
115
117
|
/**
|
|
116
|
-
* Converts a function that expects a node-style callback argument like `(err, result)` to return a Promise instead.
|
|
118
|
+
* Converts a function that expects a node-style callback argument like `(err, result)` to return a `Promise` instead.
|
|
117
119
|
* @template T
|
|
118
|
-
* @param {Function<T>} fun The given function to convert from callback style to Promise
|
|
120
|
+
* @param {Function<T>} fun The given function to convert from callback style to `Promise`.
|
|
119
121
|
* @param {*} thisArg
|
|
120
|
-
* @returns {Function<Promise<T>>} A function that wraps `fun` and returns a Promise
|
|
122
|
+
* @returns {Function<Promise<T>>} A function that wraps `fun` and returns a `Promise`.
|
|
123
|
+
*/
|
|
124
|
+
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.
|
|
121
129
|
*/
|
|
122
|
-
export declare function
|
|
130
|
+
export declare function callAsync<T = any>(fn: (successCallback: (data: T) => any, errorCallback: Function) => any): Promise<T>;
|