@formatjs/intl-durationformat 0.10.5 → 0.10.6
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/index.js +13 -477
- package/index.js.map +1 -1
- package/package.json +2 -2
- package/polyfill-force.js +13 -477
- package/polyfill-force.js.map +1 -1
- package/polyfill.js +13 -477
- package/polyfill.js.map +1 -1
package/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LookupSupportedLocales, ResolveLocale } from "@formatjs/intl-localematcher";
|
|
2
|
+
import { BigDecimal, Decimal } from "@formatjs/bigdecimal";
|
|
2
3
|
//#region packages/ecma262-abstract/OrdinaryHasInstance.js
|
|
3
4
|
function IsCallable$1(fn) {
|
|
4
5
|
return typeof fn === "function";
|
|
@@ -307,7 +308,7 @@ function PartitionDurationFormatPattern(df, duration) {
|
|
|
307
308
|
const separator = dataLocaleData.digitalFormat[numberingSystem];
|
|
308
309
|
for (let i = 0; i < TABLE_2.length && !done; i++) {
|
|
309
310
|
const row = TABLE_2[i];
|
|
310
|
-
let value = duration[row.valueField];
|
|
311
|
+
let value = new BigDecimal(duration[row.valueField]);
|
|
311
312
|
const style = internalSlots[row.styleSlot];
|
|
312
313
|
const display = internalSlots[row.displaySlot];
|
|
313
314
|
const { unit, numberFormatUnit } = row;
|
|
@@ -318,9 +319,9 @@ function PartitionDurationFormatPattern(df, duration) {
|
|
|
318
319
|
else if (unit === "milliseconds") nextStyle = internalSlots.microseconds;
|
|
319
320
|
else nextStyle = internalSlots.nanoseconds;
|
|
320
321
|
if (nextStyle === "numeric") {
|
|
321
|
-
if (unit === "seconds") value
|
|
322
|
-
else if (unit === "milliseconds") value
|
|
323
|
-
else value
|
|
322
|
+
if (unit === "seconds") value = value.plus(new BigDecimal(duration.milliseconds).div(1e3)).plus(new BigDecimal(duration.microseconds).div(1e6)).plus(new BigDecimal(duration.nanoseconds).div(1e9));
|
|
323
|
+
else if (unit === "milliseconds") value = value.plus(new BigDecimal(duration.microseconds).div(1e3)).plus(new BigDecimal(duration.nanoseconds).div(1e6));
|
|
324
|
+
else value = value.plus(new BigDecimal(duration.nanoseconds).div(1e3));
|
|
324
325
|
if (internalSlots.fractionalDigits === void 0) {
|
|
325
326
|
nfOpts.maximumFractionDigits = 9;
|
|
326
327
|
nfOpts.minimumFractionDigits = 0;
|
|
@@ -332,7 +333,7 @@ function PartitionDurationFormatPattern(df, duration) {
|
|
|
332
333
|
done = true;
|
|
333
334
|
}
|
|
334
335
|
}
|
|
335
|
-
if (value
|
|
336
|
+
if (!value.isZero() || display !== "auto") {
|
|
336
337
|
nfOpts.numberingSystem = internalSlots.numberingSystem;
|
|
337
338
|
if (style === "2-digit") nfOpts.minimumIntegerDigits = 2;
|
|
338
339
|
if (style !== "2-digit" && style !== "numeric") {
|
|
@@ -429,471 +430,6 @@ function IsValidDurationRecord(record) {
|
|
|
429
430
|
return true;
|
|
430
431
|
}
|
|
431
432
|
//#endregion
|
|
432
|
-
//#region node_modules/.aspect_rules_js/@formatjs+bigdecimal@0.0.0/node_modules/@formatjs/bigdecimal/index.js
|
|
433
|
-
const DIV_PRECISION = 40;
|
|
434
|
-
var SpecialValue = /* @__PURE__ */ function(SpecialValue) {
|
|
435
|
-
SpecialValue[SpecialValue["NONE"] = 0] = "NONE";
|
|
436
|
-
SpecialValue[SpecialValue["NAN"] = 1] = "NAN";
|
|
437
|
-
SpecialValue[SpecialValue["POSITIVE_INFINITY"] = 2] = "POSITIVE_INFINITY";
|
|
438
|
-
SpecialValue[SpecialValue["NEGATIVE_INFINITY"] = 3] = "NEGATIVE_INFINITY";
|
|
439
|
-
return SpecialValue;
|
|
440
|
-
}(SpecialValue || {});
|
|
441
|
-
function removeTrailingZeros(mantissa, exponent) {
|
|
442
|
-
if (mantissa === 0n) return [0n, 0];
|
|
443
|
-
while (mantissa % 10n === 0n) {
|
|
444
|
-
mantissa /= 10n;
|
|
445
|
-
exponent++;
|
|
446
|
-
}
|
|
447
|
-
return [mantissa, exponent];
|
|
448
|
-
}
|
|
449
|
-
function bigintAbs(n) {
|
|
450
|
-
return n < 0n ? -n : n;
|
|
451
|
-
}
|
|
452
|
-
function digitCount(n) {
|
|
453
|
-
if (n === 0n) return 1;
|
|
454
|
-
if (n < 0n) n = -n;
|
|
455
|
-
let count = 0;
|
|
456
|
-
const big15 = 1000000000000000n;
|
|
457
|
-
while (n >= big15) {
|
|
458
|
-
n /= big15;
|
|
459
|
-
count += 15;
|
|
460
|
-
}
|
|
461
|
-
let r = Number(n);
|
|
462
|
-
while (r >= 1) {
|
|
463
|
-
r /= 10;
|
|
464
|
-
count++;
|
|
465
|
-
}
|
|
466
|
-
return count;
|
|
467
|
-
}
|
|
468
|
-
const TEN_BIGINT = 10n;
|
|
469
|
-
function bigintPow10(n) {
|
|
470
|
-
if (n <= 0) return 1n;
|
|
471
|
-
let result = 1n;
|
|
472
|
-
let base = TEN_BIGINT;
|
|
473
|
-
let exp = n;
|
|
474
|
-
while (exp > 0) {
|
|
475
|
-
if (exp & 1) result *= base;
|
|
476
|
-
base *= base;
|
|
477
|
-
exp >>= 1;
|
|
478
|
-
}
|
|
479
|
-
return result;
|
|
480
|
-
}
|
|
481
|
-
function parseDecimalString(s) {
|
|
482
|
-
s = s.trim();
|
|
483
|
-
if (s === "NaN") return {
|
|
484
|
-
mantissa: 0n,
|
|
485
|
-
exponent: 0,
|
|
486
|
-
special: SpecialValue.NAN,
|
|
487
|
-
negativeZero: false
|
|
488
|
-
};
|
|
489
|
-
if (s === "Infinity" || s === "+Infinity") return {
|
|
490
|
-
mantissa: 0n,
|
|
491
|
-
exponent: 0,
|
|
492
|
-
special: SpecialValue.POSITIVE_INFINITY,
|
|
493
|
-
negativeZero: false
|
|
494
|
-
};
|
|
495
|
-
if (s === "-Infinity") return {
|
|
496
|
-
mantissa: 0n,
|
|
497
|
-
exponent: 0,
|
|
498
|
-
special: SpecialValue.NEGATIVE_INFINITY,
|
|
499
|
-
negativeZero: false
|
|
500
|
-
};
|
|
501
|
-
let negative = false;
|
|
502
|
-
let idx = 0;
|
|
503
|
-
if (s[idx] === "-") {
|
|
504
|
-
negative = true;
|
|
505
|
-
idx++;
|
|
506
|
-
} else if (s[idx] === "+") idx++;
|
|
507
|
-
let eIdx = s.indexOf("e", idx);
|
|
508
|
-
if (eIdx === -1) eIdx = s.indexOf("E", idx);
|
|
509
|
-
let sciExp = 0;
|
|
510
|
-
let numPart;
|
|
511
|
-
if (eIdx !== -1) {
|
|
512
|
-
sciExp = parseInt(s.substring(eIdx + 1), 10);
|
|
513
|
-
numPart = s.substring(idx, eIdx);
|
|
514
|
-
} else numPart = s.substring(idx);
|
|
515
|
-
const dotIdx = numPart.indexOf(".");
|
|
516
|
-
let intPart;
|
|
517
|
-
let fracPart;
|
|
518
|
-
if (dotIdx !== -1) {
|
|
519
|
-
intPart = numPart.substring(0, dotIdx);
|
|
520
|
-
fracPart = numPart.substring(dotIdx + 1);
|
|
521
|
-
} else {
|
|
522
|
-
intPart = numPart;
|
|
523
|
-
fracPart = "";
|
|
524
|
-
}
|
|
525
|
-
const combined = intPart + fracPart;
|
|
526
|
-
const exponent = sciExp - fracPart.length;
|
|
527
|
-
if (combined === "" || combined === "0" || /^0+$/.test(combined)) return {
|
|
528
|
-
mantissa: 0n,
|
|
529
|
-
exponent: 0,
|
|
530
|
-
special: SpecialValue.NONE,
|
|
531
|
-
negativeZero: negative
|
|
532
|
-
};
|
|
533
|
-
let mantissa = BigInt(combined);
|
|
534
|
-
if (negative) mantissa = -mantissa;
|
|
535
|
-
const [normMantissa, normExponent] = removeTrailingZeros(mantissa, exponent);
|
|
536
|
-
return {
|
|
537
|
-
mantissa: normMantissa,
|
|
538
|
-
exponent: normExponent,
|
|
539
|
-
special: SpecialValue.NONE,
|
|
540
|
-
negativeZero: false
|
|
541
|
-
};
|
|
542
|
-
}
|
|
543
|
-
var BigDecimal = class BigDecimal {
|
|
544
|
-
constructor(value) {
|
|
545
|
-
if (typeof value === "bigint") {
|
|
546
|
-
const [m, e] = removeTrailingZeros(value, 0);
|
|
547
|
-
this._mantissa = m;
|
|
548
|
-
this._exponent = e;
|
|
549
|
-
this._special = SpecialValue.NONE;
|
|
550
|
-
this._negativeZero = false;
|
|
551
|
-
return;
|
|
552
|
-
}
|
|
553
|
-
if (typeof value === "number") {
|
|
554
|
-
if (Number.isNaN(value)) {
|
|
555
|
-
this._mantissa = 0n;
|
|
556
|
-
this._exponent = 0;
|
|
557
|
-
this._special = SpecialValue.NAN;
|
|
558
|
-
this._negativeZero = false;
|
|
559
|
-
return;
|
|
560
|
-
}
|
|
561
|
-
if (value === Infinity) {
|
|
562
|
-
this._mantissa = 0n;
|
|
563
|
-
this._exponent = 0;
|
|
564
|
-
this._special = SpecialValue.POSITIVE_INFINITY;
|
|
565
|
-
this._negativeZero = false;
|
|
566
|
-
return;
|
|
567
|
-
}
|
|
568
|
-
if (value === -Infinity) {
|
|
569
|
-
this._mantissa = 0n;
|
|
570
|
-
this._exponent = 0;
|
|
571
|
-
this._special = SpecialValue.NEGATIVE_INFINITY;
|
|
572
|
-
this._negativeZero = false;
|
|
573
|
-
return;
|
|
574
|
-
}
|
|
575
|
-
if (value === 0) {
|
|
576
|
-
this._mantissa = 0n;
|
|
577
|
-
this._exponent = 0;
|
|
578
|
-
this._special = SpecialValue.NONE;
|
|
579
|
-
this._negativeZero = Object.is(value, -0);
|
|
580
|
-
return;
|
|
581
|
-
}
|
|
582
|
-
value = String(value);
|
|
583
|
-
}
|
|
584
|
-
const parsed = parseDecimalString(value);
|
|
585
|
-
this._mantissa = parsed.mantissa;
|
|
586
|
-
this._exponent = parsed.exponent;
|
|
587
|
-
this._special = parsed.special;
|
|
588
|
-
this._negativeZero = parsed.negativeZero;
|
|
589
|
-
}
|
|
590
|
-
static _create(mantissa, exponent, special, negativeZero) {
|
|
591
|
-
const bd = Object.create(BigDecimal.prototype);
|
|
592
|
-
bd._mantissa = mantissa;
|
|
593
|
-
bd._exponent = exponent;
|
|
594
|
-
bd._special = special;
|
|
595
|
-
bd._negativeZero = negativeZero;
|
|
596
|
-
return bd;
|
|
597
|
-
}
|
|
598
|
-
static _coerce(v) {
|
|
599
|
-
return v instanceof BigDecimal ? v : new BigDecimal(v);
|
|
600
|
-
}
|
|
601
|
-
times(y) {
|
|
602
|
-
const other = BigDecimal._coerce(y);
|
|
603
|
-
if (this._special || other._special) return this._specialArith(other, "times");
|
|
604
|
-
if (this._mantissa === 0n || other._mantissa === 0n) {
|
|
605
|
-
const negZero = this._isSignNegative() ? !other._isSignNegative() : other._isSignNegative();
|
|
606
|
-
return BigDecimal._create(0n, 0, SpecialValue.NONE, negZero);
|
|
607
|
-
}
|
|
608
|
-
const [nm, ne] = removeTrailingZeros(this._mantissa * other._mantissa, this._exponent + other._exponent);
|
|
609
|
-
return BigDecimal._create(nm, ne, SpecialValue.NONE, false);
|
|
610
|
-
}
|
|
611
|
-
div(y) {
|
|
612
|
-
const other = BigDecimal._coerce(y);
|
|
613
|
-
if (this._special || other._special) return this._specialArith(other, "div");
|
|
614
|
-
if (other._mantissa === 0n) {
|
|
615
|
-
if (this._mantissa === 0n) return BigDecimal._create(0n, 0, SpecialValue.NAN, false);
|
|
616
|
-
const neg = this._isSignNegative() !== other._isSignNegative();
|
|
617
|
-
return BigDecimal._create(0n, 0, neg ? SpecialValue.NEGATIVE_INFINITY : SpecialValue.POSITIVE_INFINITY, false);
|
|
618
|
-
}
|
|
619
|
-
if (this._mantissa === 0n) {
|
|
620
|
-
const negZero = this._isSignNegative() !== other._isSignNegative();
|
|
621
|
-
return BigDecimal._create(0n, 0, SpecialValue.NONE, negZero);
|
|
622
|
-
}
|
|
623
|
-
const [nm, ne] = removeTrailingZeros(this._mantissa * bigintPow10(DIV_PRECISION) / other._mantissa, this._exponent - other._exponent - DIV_PRECISION);
|
|
624
|
-
return BigDecimal._create(nm, ne, SpecialValue.NONE, false);
|
|
625
|
-
}
|
|
626
|
-
plus(y) {
|
|
627
|
-
const other = BigDecimal._coerce(y);
|
|
628
|
-
if (this._special || other._special) return this._specialArith(other, "plus");
|
|
629
|
-
if (this._mantissa === 0n && other._mantissa === 0n) {
|
|
630
|
-
const negZero = this._negativeZero && other._negativeZero;
|
|
631
|
-
return BigDecimal._create(0n, 0, SpecialValue.NONE, negZero);
|
|
632
|
-
}
|
|
633
|
-
if (this._mantissa === 0n) return other;
|
|
634
|
-
if (other._mantissa === 0n) return this;
|
|
635
|
-
let m1 = this._mantissa;
|
|
636
|
-
let m2 = other._mantissa;
|
|
637
|
-
const e1 = this._exponent;
|
|
638
|
-
const e2 = other._exponent;
|
|
639
|
-
const minE = Math.min(e1, e2);
|
|
640
|
-
if (e1 > minE) m1 *= bigintPow10(e1 - minE);
|
|
641
|
-
if (e2 > minE) m2 *= bigintPow10(e2 - minE);
|
|
642
|
-
const sum = m1 + m2;
|
|
643
|
-
if (sum === 0n) return BigDecimal._create(0n, 0, SpecialValue.NONE, false);
|
|
644
|
-
const [nm, ne] = removeTrailingZeros(sum, minE);
|
|
645
|
-
return BigDecimal._create(nm, ne, SpecialValue.NONE, false);
|
|
646
|
-
}
|
|
647
|
-
minus(y) {
|
|
648
|
-
return this.plus(BigDecimal._coerce(y).negated());
|
|
649
|
-
}
|
|
650
|
-
mod(y) {
|
|
651
|
-
const other = BigDecimal._coerce(y);
|
|
652
|
-
if (this._special || other._special) {
|
|
653
|
-
if (this._special === SpecialValue.NAN || other._special === SpecialValue.NAN) return BigDecimal._create(0n, 0, SpecialValue.NAN, false);
|
|
654
|
-
if (this._special === SpecialValue.POSITIVE_INFINITY || this._special === SpecialValue.NEGATIVE_INFINITY) return BigDecimal._create(0n, 0, SpecialValue.NAN, false);
|
|
655
|
-
if (other._special === SpecialValue.POSITIVE_INFINITY || other._special === SpecialValue.NEGATIVE_INFINITY) return this;
|
|
656
|
-
}
|
|
657
|
-
if (other._mantissa === 0n) return BigDecimal._create(0n, 0, SpecialValue.NAN, false);
|
|
658
|
-
if (this._mantissa === 0n) return this;
|
|
659
|
-
let m1 = this._mantissa;
|
|
660
|
-
let m2 = other._mantissa;
|
|
661
|
-
const e1 = this._exponent;
|
|
662
|
-
const e2 = other._exponent;
|
|
663
|
-
const minE = Math.min(e1, e2);
|
|
664
|
-
if (e1 > minE) m1 *= bigintPow10(e1 - minE);
|
|
665
|
-
if (e2 > minE) m2 *= bigintPow10(e2 - minE);
|
|
666
|
-
const remainder = m1 % m2;
|
|
667
|
-
if (remainder === 0n) return BigDecimal._create(0n, 0, SpecialValue.NONE, false);
|
|
668
|
-
const [nm, ne] = removeTrailingZeros(remainder, minE);
|
|
669
|
-
return BigDecimal._create(nm, ne, SpecialValue.NONE, false);
|
|
670
|
-
}
|
|
671
|
-
abs() {
|
|
672
|
-
if (this._special === SpecialValue.NAN) return this;
|
|
673
|
-
if (this._special === SpecialValue.NEGATIVE_INFINITY) return BigDecimal._create(0n, 0, SpecialValue.POSITIVE_INFINITY, false);
|
|
674
|
-
return BigDecimal._create(bigintAbs(this._mantissa), this._exponent, this._special, false);
|
|
675
|
-
}
|
|
676
|
-
negated() {
|
|
677
|
-
if (this._special === SpecialValue.NAN) return this;
|
|
678
|
-
if (this._special === SpecialValue.POSITIVE_INFINITY) return BigDecimal._create(0n, 0, SpecialValue.NEGATIVE_INFINITY, false);
|
|
679
|
-
if (this._special === SpecialValue.NEGATIVE_INFINITY) return BigDecimal._create(0n, 0, SpecialValue.POSITIVE_INFINITY, false);
|
|
680
|
-
if (this._mantissa === 0n) return BigDecimal._create(0n, 0, SpecialValue.NONE, !this._negativeZero);
|
|
681
|
-
return BigDecimal._create(-this._mantissa, this._exponent, SpecialValue.NONE, false);
|
|
682
|
-
}
|
|
683
|
-
pow(n) {
|
|
684
|
-
if (this._special === SpecialValue.NAN) return this;
|
|
685
|
-
if (n === 0) return new BigDecimal(1);
|
|
686
|
-
if (n < 0) return new BigDecimal(1).div(this.pow(-n));
|
|
687
|
-
if (this._special === SpecialValue.POSITIVE_INFINITY) return this;
|
|
688
|
-
if (this._special === SpecialValue.NEGATIVE_INFINITY) return n % 2 === 0 ? BigDecimal._create(0n, 0, SpecialValue.POSITIVE_INFINITY, false) : this;
|
|
689
|
-
if (this._mantissa === 0n) return new BigDecimal(0);
|
|
690
|
-
const [nm, ne] = removeTrailingZeros(this._mantissa ** BigInt(n), this._exponent * n);
|
|
691
|
-
return BigDecimal._create(nm, ne, SpecialValue.NONE, false);
|
|
692
|
-
}
|
|
693
|
-
floor() {
|
|
694
|
-
if (this._special !== SpecialValue.NONE) return this;
|
|
695
|
-
if (this._mantissa === 0n) return this;
|
|
696
|
-
if (this._exponent >= 0) return this;
|
|
697
|
-
const divisor = bigintPow10(-this._exponent);
|
|
698
|
-
const m = this._mantissa;
|
|
699
|
-
let q = m / divisor;
|
|
700
|
-
if (m < 0n && m % divisor !== 0n) q -= 1n;
|
|
701
|
-
if (q === 0n) {
|
|
702
|
-
const negZero = this._mantissa < 0n;
|
|
703
|
-
return BigDecimal._create(0n, 0, SpecialValue.NONE, negZero);
|
|
704
|
-
}
|
|
705
|
-
const [nm, ne] = removeTrailingZeros(q, 0);
|
|
706
|
-
return BigDecimal._create(nm, ne, SpecialValue.NONE, false);
|
|
707
|
-
}
|
|
708
|
-
ceil() {
|
|
709
|
-
if (this._special !== SpecialValue.NONE) return this;
|
|
710
|
-
if (this._mantissa === 0n) return this;
|
|
711
|
-
if (this._exponent >= 0) return this;
|
|
712
|
-
const divisor = bigintPow10(-this._exponent);
|
|
713
|
-
const m = this._mantissa;
|
|
714
|
-
let q = m / divisor;
|
|
715
|
-
if (m > 0n && m % divisor !== 0n) q += 1n;
|
|
716
|
-
if (q === 0n) return BigDecimal._create(0n, 0, SpecialValue.NONE, false);
|
|
717
|
-
const [nm, ne] = removeTrailingZeros(q, 0);
|
|
718
|
-
return BigDecimal._create(nm, ne, SpecialValue.NONE, false);
|
|
719
|
-
}
|
|
720
|
-
log(base) {
|
|
721
|
-
if (this._special === SpecialValue.NAN) return this;
|
|
722
|
-
if (this._special === SpecialValue.NEGATIVE_INFINITY) return BigDecimal._create(0n, 0, SpecialValue.NAN, false);
|
|
723
|
-
if (this._special === SpecialValue.POSITIVE_INFINITY) return BigDecimal._create(0n, 0, SpecialValue.POSITIVE_INFINITY, false);
|
|
724
|
-
if (this._mantissa < 0n) return BigDecimal._create(0n, 0, SpecialValue.NAN, false);
|
|
725
|
-
if (this._mantissa === 0n) return BigDecimal._create(0n, 0, SpecialValue.NEGATIVE_INFINITY, false);
|
|
726
|
-
if (base === 10) return this._log10();
|
|
727
|
-
const log10x = this._log10();
|
|
728
|
-
const log10b = new BigDecimal(Math.log10(base));
|
|
729
|
-
return log10x.div(log10b);
|
|
730
|
-
}
|
|
731
|
-
_log10() {
|
|
732
|
-
const absMantissa = bigintAbs(this._mantissa);
|
|
733
|
-
const digits = digitCount(absMantissa);
|
|
734
|
-
let log10Mantissa;
|
|
735
|
-
if (digits <= 15) log10Mantissa = Math.log10(Number(absMantissa));
|
|
736
|
-
else {
|
|
737
|
-
const shift = digits - 17;
|
|
738
|
-
const leading = absMantissa / bigintPow10(shift);
|
|
739
|
-
log10Mantissa = Math.log10(Number(leading)) + shift;
|
|
740
|
-
}
|
|
741
|
-
return new BigDecimal(log10Mantissa + this._exponent);
|
|
742
|
-
}
|
|
743
|
-
eq(y) {
|
|
744
|
-
const other = BigDecimal._coerce(y);
|
|
745
|
-
if (this._special === SpecialValue.NAN || other._special === SpecialValue.NAN) return false;
|
|
746
|
-
if (this._special !== other._special) return false;
|
|
747
|
-
if (this._special !== SpecialValue.NONE) return true;
|
|
748
|
-
if (this._mantissa === 0n && other._mantissa === 0n) return true;
|
|
749
|
-
return this._mantissa === other._mantissa && this._exponent === other._exponent;
|
|
750
|
-
}
|
|
751
|
-
_compareTo(other) {
|
|
752
|
-
if (this._special === SpecialValue.NAN || other._special === SpecialValue.NAN) return NaN;
|
|
753
|
-
if (this._special === SpecialValue.POSITIVE_INFINITY) return other._special === SpecialValue.POSITIVE_INFINITY ? 0 : 1;
|
|
754
|
-
if (this._special === SpecialValue.NEGATIVE_INFINITY) return other._special === SpecialValue.NEGATIVE_INFINITY ? 0 : -1;
|
|
755
|
-
if (other._special === SpecialValue.POSITIVE_INFINITY) return -1;
|
|
756
|
-
if (other._special === SpecialValue.NEGATIVE_INFINITY) return 1;
|
|
757
|
-
const thisZero = this._mantissa === 0n;
|
|
758
|
-
const otherZero = other._mantissa === 0n;
|
|
759
|
-
if (thisZero && otherZero) return 0;
|
|
760
|
-
if (thisZero) return other._mantissa > 0n ? -1 : 1;
|
|
761
|
-
if (otherZero) return this._mantissa > 0n ? 1 : -1;
|
|
762
|
-
const thisNeg = this._mantissa < 0n;
|
|
763
|
-
if (thisNeg !== other._mantissa < 0n) return thisNeg ? -1 : 1;
|
|
764
|
-
let m1 = this._mantissa;
|
|
765
|
-
let m2 = other._mantissa;
|
|
766
|
-
const e1 = this._exponent;
|
|
767
|
-
const e2 = other._exponent;
|
|
768
|
-
const minE = Math.min(e1, e2);
|
|
769
|
-
if (e1 > minE) m1 *= bigintPow10(e1 - minE);
|
|
770
|
-
if (e2 > minE) m2 *= bigintPow10(e2 - minE);
|
|
771
|
-
if (m1 < m2) return -1;
|
|
772
|
-
if (m1 > m2) return 1;
|
|
773
|
-
return 0;
|
|
774
|
-
}
|
|
775
|
-
lessThan(y) {
|
|
776
|
-
return this._compareTo(BigDecimal._coerce(y)) === -1;
|
|
777
|
-
}
|
|
778
|
-
greaterThan(y) {
|
|
779
|
-
return this._compareTo(BigDecimal._coerce(y)) === 1;
|
|
780
|
-
}
|
|
781
|
-
lessThanOrEqualTo(y) {
|
|
782
|
-
const c = this._compareTo(BigDecimal._coerce(y));
|
|
783
|
-
return c === 0 || c === -1;
|
|
784
|
-
}
|
|
785
|
-
greaterThanOrEqualTo(y) {
|
|
786
|
-
const c = this._compareTo(BigDecimal._coerce(y));
|
|
787
|
-
return c === 0 || c === 1;
|
|
788
|
-
}
|
|
789
|
-
isZero() {
|
|
790
|
-
return this._special === SpecialValue.NONE && this._mantissa === 0n;
|
|
791
|
-
}
|
|
792
|
-
isNaN() {
|
|
793
|
-
return this._special === SpecialValue.NAN;
|
|
794
|
-
}
|
|
795
|
-
isFinite() {
|
|
796
|
-
return this._special === SpecialValue.NONE;
|
|
797
|
-
}
|
|
798
|
-
isNegative() {
|
|
799
|
-
if (this._special === SpecialValue.NAN) return false;
|
|
800
|
-
if (this._special === SpecialValue.NEGATIVE_INFINITY) return true;
|
|
801
|
-
if (this._special === SpecialValue.POSITIVE_INFINITY) return false;
|
|
802
|
-
if (this._mantissa === 0n) return this._negativeZero;
|
|
803
|
-
return this._mantissa < 0n;
|
|
804
|
-
}
|
|
805
|
-
isPositive() {
|
|
806
|
-
if (this._special === SpecialValue.NAN) return false;
|
|
807
|
-
if (this._special === SpecialValue.POSITIVE_INFINITY) return true;
|
|
808
|
-
if (this._special === SpecialValue.NEGATIVE_INFINITY) return false;
|
|
809
|
-
if (this._mantissa === 0n) return !this._negativeZero;
|
|
810
|
-
return this._mantissa > 0n;
|
|
811
|
-
}
|
|
812
|
-
isInteger() {
|
|
813
|
-
if (this._special !== SpecialValue.NONE) return false;
|
|
814
|
-
if (this._mantissa === 0n) return true;
|
|
815
|
-
return this._exponent >= 0;
|
|
816
|
-
}
|
|
817
|
-
toJSON() {
|
|
818
|
-
return this.toString();
|
|
819
|
-
}
|
|
820
|
-
toNumber() {
|
|
821
|
-
if (this._special === SpecialValue.NAN) return NaN;
|
|
822
|
-
if (this._special === SpecialValue.POSITIVE_INFINITY) return Infinity;
|
|
823
|
-
if (this._special === SpecialValue.NEGATIVE_INFINITY) return -Infinity;
|
|
824
|
-
if (this._mantissa === 0n) return this._negativeZero ? -0 : 0;
|
|
825
|
-
return Number(this.toString());
|
|
826
|
-
}
|
|
827
|
-
toString() {
|
|
828
|
-
if (this._special === SpecialValue.NAN) return "NaN";
|
|
829
|
-
if (this._special === SpecialValue.POSITIVE_INFINITY) return "Infinity";
|
|
830
|
-
if (this._special === SpecialValue.NEGATIVE_INFINITY) return "-Infinity";
|
|
831
|
-
if (this._mantissa === 0n) return "0";
|
|
832
|
-
const negative = this._mantissa < 0n;
|
|
833
|
-
const absStr = bigintAbs(this._mantissa).toString();
|
|
834
|
-
const prefix = negative ? "-" : "";
|
|
835
|
-
if (this._exponent === 0) return prefix + absStr;
|
|
836
|
-
if (this._exponent > 0) return prefix + absStr + "0".repeat(this._exponent);
|
|
837
|
-
const decimalPlaces = -this._exponent;
|
|
838
|
-
if (decimalPlaces < absStr.length) {
|
|
839
|
-
const intPart = absStr.slice(0, absStr.length - decimalPlaces);
|
|
840
|
-
const fracPart = absStr.slice(absStr.length - decimalPlaces);
|
|
841
|
-
return prefix + intPart + "." + fracPart;
|
|
842
|
-
} else {
|
|
843
|
-
const leadingZeros = decimalPlaces - absStr.length;
|
|
844
|
-
return prefix + "0." + "0".repeat(leadingZeros) + absStr;
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
static pow(base, exp) {
|
|
848
|
-
const n = typeof exp === "number" ? exp : exp.toNumber();
|
|
849
|
-
if (typeof base === "number" && base === 10) return BigDecimal._create(1n, n, SpecialValue.NONE, false);
|
|
850
|
-
return (base instanceof BigDecimal ? base : new BigDecimal(base)).pow(n);
|
|
851
|
-
}
|
|
852
|
-
static set(_config) {}
|
|
853
|
-
_isSignNegative() {
|
|
854
|
-
if (this._special === SpecialValue.NEGATIVE_INFINITY) return true;
|
|
855
|
-
if (this._mantissa < 0n) return true;
|
|
856
|
-
if (this._mantissa === 0n) return this._negativeZero;
|
|
857
|
-
return false;
|
|
858
|
-
}
|
|
859
|
-
_specialArith(other, op) {
|
|
860
|
-
const a = this._special;
|
|
861
|
-
const b = other._special;
|
|
862
|
-
if (a === SpecialValue.NAN || b === SpecialValue.NAN) return BigDecimal._create(0n, 0, SpecialValue.NAN, false);
|
|
863
|
-
const aNeg = this._isSignNegative();
|
|
864
|
-
const bNeg = other._isSignNegative();
|
|
865
|
-
const aInf = a === SpecialValue.POSITIVE_INFINITY || a === SpecialValue.NEGATIVE_INFINITY;
|
|
866
|
-
const bInf = b === SpecialValue.POSITIVE_INFINITY || b === SpecialValue.NEGATIVE_INFINITY;
|
|
867
|
-
if (op === "times") {
|
|
868
|
-
if (aInf || bInf) {
|
|
869
|
-
if (aInf && other._mantissa === 0n && !bInf || bInf && this._mantissa === 0n && !aInf) return BigDecimal._create(0n, 0, SpecialValue.NAN, false);
|
|
870
|
-
const neg = aNeg !== bNeg;
|
|
871
|
-
return BigDecimal._create(0n, 0, neg ? SpecialValue.NEGATIVE_INFINITY : SpecialValue.POSITIVE_INFINITY, false);
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
if (op === "div") {
|
|
875
|
-
if (aInf && bInf) return BigDecimal._create(0n, 0, SpecialValue.NAN, false);
|
|
876
|
-
if (aInf) {
|
|
877
|
-
const neg = aNeg !== bNeg;
|
|
878
|
-
return BigDecimal._create(0n, 0, neg ? SpecialValue.NEGATIVE_INFINITY : SpecialValue.POSITIVE_INFINITY, false);
|
|
879
|
-
}
|
|
880
|
-
if (bInf) {
|
|
881
|
-
const negZero = aNeg !== bNeg;
|
|
882
|
-
return BigDecimal._create(0n, 0, SpecialValue.NONE, negZero);
|
|
883
|
-
}
|
|
884
|
-
}
|
|
885
|
-
if (op === "plus") {
|
|
886
|
-
if (aInf && bInf) {
|
|
887
|
-
if (aNeg !== bNeg) return BigDecimal._create(0n, 0, SpecialValue.NAN, false);
|
|
888
|
-
return this;
|
|
889
|
-
}
|
|
890
|
-
if (aInf) return this;
|
|
891
|
-
if (bInf) return other;
|
|
892
|
-
}
|
|
893
|
-
return BigDecimal._create(0n, 0, SpecialValue.NAN, false);
|
|
894
|
-
}
|
|
895
|
-
};
|
|
896
|
-
//#endregion
|
|
897
433
|
//#region packages/ecma262-abstract/ToPrimitive.js
|
|
898
434
|
function invariant$1(condition, message, Err = Error) {
|
|
899
435
|
if (!condition) throw new Err(message);
|
|
@@ -939,7 +475,7 @@ function ToPrimitive(input, preferredType) {
|
|
|
939
475
|
}
|
|
940
476
|
//#endregion
|
|
941
477
|
//#region packages/ecma262-abstract/ToNumber.js
|
|
942
|
-
const ZERO = new
|
|
478
|
+
const ZERO = new Decimal(0);
|
|
943
479
|
function invariant(condition, message, Err = Error) {
|
|
944
480
|
if (!condition) throw new Err(message);
|
|
945
481
|
}
|
|
@@ -947,16 +483,16 @@ function invariant(condition, message, Err = Error) {
|
|
|
947
483
|
* https://tc39.es/ecma262/#sec-tonumber
|
|
948
484
|
*/
|
|
949
485
|
function ToNumber(arg) {
|
|
950
|
-
if (typeof arg === "number") return new
|
|
951
|
-
if (typeof arg === "bigint") return new
|
|
486
|
+
if (typeof arg === "number") return new Decimal(arg);
|
|
487
|
+
if (typeof arg === "bigint") return new Decimal(arg.toString());
|
|
952
488
|
invariant(typeof arg !== "symbol", "Symbol is not supported", TypeError);
|
|
953
|
-
if (arg === void 0) return new
|
|
489
|
+
if (arg === void 0) return new Decimal(NaN);
|
|
954
490
|
if (arg === null || arg === 0) return ZERO;
|
|
955
|
-
if (arg === true) return new
|
|
491
|
+
if (arg === true) return new Decimal(1);
|
|
956
492
|
if (typeof arg === "string") try {
|
|
957
|
-
return new
|
|
493
|
+
return new Decimal(arg);
|
|
958
494
|
} catch {
|
|
959
|
-
return new
|
|
495
|
+
return new Decimal(NaN);
|
|
960
496
|
}
|
|
961
497
|
invariant(typeof arg === "object", "object expected", TypeError);
|
|
962
498
|
let primValue = ToPrimitive(arg, "number");
|