bson 5.4.0 → 5.5.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/bson.d.ts +20 -0
- package/lib/bson.bundle.js +118 -69
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +118 -69
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +118 -69
- package/lib/bson.mjs.map +1 -1
- package/lib/bson.rn.cjs +118 -69
- package/lib/bson.rn.cjs.map +1 -1
- package/package.json +1 -1
- package/src/decimal128.ts +165 -84
- package/src/error.ts +1 -3
package/lib/bson.rn.cjs
CHANGED
|
@@ -108,7 +108,7 @@ class BSONVersionError extends BSONError {
|
|
|
108
108
|
return 'BSONVersionError';
|
|
109
109
|
}
|
|
110
110
|
constructor() {
|
|
111
|
-
super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.
|
|
111
|
+
super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.x.x`);
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
class BSONRuntimeError extends BSONError {
|
|
@@ -1451,7 +1451,14 @@ class Decimal128 extends BSONValue {
|
|
|
1451
1451
|
}
|
|
1452
1452
|
}
|
|
1453
1453
|
static fromString(representation) {
|
|
1454
|
+
return Decimal128._fromString(representation, { allowRounding: false });
|
|
1455
|
+
}
|
|
1456
|
+
static fromStringWithRounding(representation) {
|
|
1457
|
+
return Decimal128._fromString(representation, { allowRounding: true });
|
|
1458
|
+
}
|
|
1459
|
+
static _fromString(representation, options) {
|
|
1454
1460
|
let isNegative = false;
|
|
1461
|
+
let sawSign = false;
|
|
1455
1462
|
let sawRadix = false;
|
|
1456
1463
|
let foundNonZero = false;
|
|
1457
1464
|
let significantDigits = 0;
|
|
@@ -1462,10 +1469,8 @@ class Decimal128 extends BSONValue {
|
|
|
1462
1469
|
const digits = [0];
|
|
1463
1470
|
let nDigitsStored = 0;
|
|
1464
1471
|
let digitsInsert = 0;
|
|
1465
|
-
let firstDigit = 0;
|
|
1466
1472
|
let lastDigit = 0;
|
|
1467
1473
|
let exponent = 0;
|
|
1468
|
-
let i = 0;
|
|
1469
1474
|
let significandHigh = new Long(0, 0);
|
|
1470
1475
|
let significandLow = new Long(0, 0);
|
|
1471
1476
|
let biasedExponent = 0;
|
|
@@ -1493,6 +1498,7 @@ class Decimal128 extends BSONValue {
|
|
|
1493
1498
|
}
|
|
1494
1499
|
}
|
|
1495
1500
|
if (representation[index] === '+' || representation[index] === '-') {
|
|
1501
|
+
sawSign = true;
|
|
1496
1502
|
isNegative = representation[index++] === '-';
|
|
1497
1503
|
}
|
|
1498
1504
|
if (!isDigit(representation[index]) && representation[index] !== '.') {
|
|
@@ -1511,7 +1517,7 @@ class Decimal128 extends BSONValue {
|
|
|
1511
1517
|
index = index + 1;
|
|
1512
1518
|
continue;
|
|
1513
1519
|
}
|
|
1514
|
-
if (nDigitsStored <
|
|
1520
|
+
if (nDigitsStored < MAX_DIGITS) {
|
|
1515
1521
|
if (representation[index] !== '0' || foundNonZero) {
|
|
1516
1522
|
if (!foundNonZero) {
|
|
1517
1523
|
firstNonZero = nDigitsRead;
|
|
@@ -1539,10 +1545,7 @@ class Decimal128 extends BSONValue {
|
|
|
1539
1545
|
}
|
|
1540
1546
|
if (representation[index])
|
|
1541
1547
|
return new Decimal128(NAN_BUFFER);
|
|
1542
|
-
firstDigit = 0;
|
|
1543
1548
|
if (!nDigitsStored) {
|
|
1544
|
-
firstDigit = 0;
|
|
1545
|
-
lastDigit = 0;
|
|
1546
1549
|
digits[0] = 0;
|
|
1547
1550
|
nDigits = 1;
|
|
1548
1551
|
nDigitsStored = 1;
|
|
@@ -1552,12 +1555,12 @@ class Decimal128 extends BSONValue {
|
|
|
1552
1555
|
lastDigit = nDigitsStored - 1;
|
|
1553
1556
|
significantDigits = nDigits;
|
|
1554
1557
|
if (significantDigits !== 1) {
|
|
1555
|
-
while (
|
|
1558
|
+
while (representation[firstNonZero + significantDigits - 1 + Number(sawSign) + Number(sawRadix)] === '0') {
|
|
1556
1559
|
significantDigits = significantDigits - 1;
|
|
1557
1560
|
}
|
|
1558
1561
|
}
|
|
1559
1562
|
}
|
|
1560
|
-
if (exponent <= radixPosition && radixPosition
|
|
1563
|
+
if (exponent <= radixPosition && radixPosition > exponent + (1 << 14)) {
|
|
1561
1564
|
exponent = EXPONENT_MIN;
|
|
1562
1565
|
}
|
|
1563
1566
|
else {
|
|
@@ -1565,9 +1568,8 @@ class Decimal128 extends BSONValue {
|
|
|
1565
1568
|
}
|
|
1566
1569
|
while (exponent > EXPONENT_MAX) {
|
|
1567
1570
|
lastDigit = lastDigit + 1;
|
|
1568
|
-
if (lastDigit
|
|
1569
|
-
|
|
1570
|
-
if (digitsString.match(/^0+$/)) {
|
|
1571
|
+
if (lastDigit >= MAX_DIGITS) {
|
|
1572
|
+
if (significantDigits === 0) {
|
|
1571
1573
|
exponent = EXPONENT_MAX;
|
|
1572
1574
|
break;
|
|
1573
1575
|
}
|
|
@@ -1575,69 +1577,116 @@ class Decimal128 extends BSONValue {
|
|
|
1575
1577
|
}
|
|
1576
1578
|
exponent = exponent - 1;
|
|
1577
1579
|
}
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
}
|
|
1584
|
-
if (nDigitsStored < nDigits) {
|
|
1585
|
-
nDigits = nDigits - 1;
|
|
1586
|
-
}
|
|
1587
|
-
else {
|
|
1588
|
-
lastDigit = lastDigit - 1;
|
|
1589
|
-
}
|
|
1590
|
-
if (exponent < EXPONENT_MAX) {
|
|
1591
|
-
exponent = exponent + 1;
|
|
1592
|
-
}
|
|
1593
|
-
else {
|
|
1594
|
-
const digitsString = digits.join('');
|
|
1595
|
-
if (digitsString.match(/^0+$/)) {
|
|
1596
|
-
exponent = EXPONENT_MAX;
|
|
1580
|
+
if (options.allowRounding) {
|
|
1581
|
+
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
|
|
1582
|
+
if (lastDigit === 0 && significantDigits < nDigitsStored) {
|
|
1583
|
+
exponent = EXPONENT_MIN;
|
|
1584
|
+
significantDigits = 0;
|
|
1597
1585
|
break;
|
|
1598
1586
|
}
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
let roundBit = 0;
|
|
1614
|
-
if (roundDigit >= 5) {
|
|
1615
|
-
roundBit = 1;
|
|
1616
|
-
if (roundDigit === 5) {
|
|
1617
|
-
roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
|
|
1618
|
-
for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
|
|
1619
|
-
if (parseInt(representation[i], 10)) {
|
|
1620
|
-
roundBit = 1;
|
|
1621
|
-
break;
|
|
1622
|
-
}
|
|
1587
|
+
if (nDigitsStored < nDigits) {
|
|
1588
|
+
nDigits = nDigits - 1;
|
|
1589
|
+
}
|
|
1590
|
+
else {
|
|
1591
|
+
lastDigit = lastDigit - 1;
|
|
1592
|
+
}
|
|
1593
|
+
if (exponent < EXPONENT_MAX) {
|
|
1594
|
+
exponent = exponent + 1;
|
|
1595
|
+
}
|
|
1596
|
+
else {
|
|
1597
|
+
const digitsString = digits.join('');
|
|
1598
|
+
if (digitsString.match(/^0+$/)) {
|
|
1599
|
+
exponent = EXPONENT_MAX;
|
|
1600
|
+
break;
|
|
1623
1601
|
}
|
|
1602
|
+
invalidErr(representation, 'overflow');
|
|
1624
1603
|
}
|
|
1625
1604
|
}
|
|
1626
|
-
if (
|
|
1627
|
-
let
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1605
|
+
if (lastDigit + 1 < significantDigits) {
|
|
1606
|
+
let endOfString = nDigitsRead;
|
|
1607
|
+
if (sawRadix) {
|
|
1608
|
+
firstNonZero = firstNonZero + 1;
|
|
1609
|
+
endOfString = endOfString + 1;
|
|
1610
|
+
}
|
|
1611
|
+
if (sawSign) {
|
|
1612
|
+
firstNonZero = firstNonZero + 1;
|
|
1613
|
+
endOfString = endOfString + 1;
|
|
1614
|
+
}
|
|
1615
|
+
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
|
|
1616
|
+
let roundBit = 0;
|
|
1617
|
+
if (roundDigit >= 5) {
|
|
1618
|
+
roundBit = 1;
|
|
1619
|
+
if (roundDigit === 5) {
|
|
1620
|
+
roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
|
|
1621
|
+
for (let i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
|
|
1622
|
+
if (parseInt(representation[i], 10)) {
|
|
1623
|
+
roundBit = 1;
|
|
1624
|
+
break;
|
|
1635
1625
|
}
|
|
1636
|
-
|
|
1637
|
-
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
if (roundBit) {
|
|
1630
|
+
let dIdx = lastDigit;
|
|
1631
|
+
for (; dIdx >= 0; dIdx--) {
|
|
1632
|
+
if (++digits[dIdx] > 9) {
|
|
1633
|
+
digits[dIdx] = 0;
|
|
1634
|
+
if (dIdx === 0) {
|
|
1635
|
+
if (exponent < EXPONENT_MAX) {
|
|
1636
|
+
exponent = exponent + 1;
|
|
1637
|
+
digits[dIdx] = 1;
|
|
1638
|
+
}
|
|
1639
|
+
else {
|
|
1640
|
+
return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
|
|
1641
|
+
}
|
|
1638
1642
|
}
|
|
1639
1643
|
}
|
|
1644
|
+
else {
|
|
1645
|
+
break;
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
else {
|
|
1652
|
+
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
|
|
1653
|
+
if (lastDigit === 0) {
|
|
1654
|
+
if (significantDigits === 0) {
|
|
1655
|
+
exponent = EXPONENT_MIN;
|
|
1656
|
+
break;
|
|
1640
1657
|
}
|
|
1658
|
+
invalidErr(representation, 'exponent underflow');
|
|
1659
|
+
}
|
|
1660
|
+
if (nDigitsStored < nDigits) {
|
|
1661
|
+
if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
|
|
1662
|
+
significantDigits !== 0) {
|
|
1663
|
+
invalidErr(representation, 'inexact rounding');
|
|
1664
|
+
}
|
|
1665
|
+
nDigits = nDigits - 1;
|
|
1666
|
+
}
|
|
1667
|
+
else {
|
|
1668
|
+
if (digits[lastDigit] !== 0) {
|
|
1669
|
+
invalidErr(representation, 'inexact rounding');
|
|
1670
|
+
}
|
|
1671
|
+
lastDigit = lastDigit - 1;
|
|
1672
|
+
}
|
|
1673
|
+
if (exponent < EXPONENT_MAX) {
|
|
1674
|
+
exponent = exponent + 1;
|
|
1675
|
+
}
|
|
1676
|
+
else {
|
|
1677
|
+
invalidErr(representation, 'overflow');
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
if (lastDigit + 1 < significantDigits) {
|
|
1681
|
+
if (sawRadix) {
|
|
1682
|
+
firstNonZero = firstNonZero + 1;
|
|
1683
|
+
}
|
|
1684
|
+
if (sawSign) {
|
|
1685
|
+
firstNonZero = firstNonZero + 1;
|
|
1686
|
+
}
|
|
1687
|
+
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
|
|
1688
|
+
if (roundDigit !== 0) {
|
|
1689
|
+
invalidErr(representation, 'inexact rounding');
|
|
1641
1690
|
}
|
|
1642
1691
|
}
|
|
1643
1692
|
}
|
|
@@ -1647,8 +1696,8 @@ class Decimal128 extends BSONValue {
|
|
|
1647
1696
|
significandHigh = Long.fromNumber(0);
|
|
1648
1697
|
significandLow = Long.fromNumber(0);
|
|
1649
1698
|
}
|
|
1650
|
-
else if (lastDigit
|
|
1651
|
-
let dIdx =
|
|
1699
|
+
else if (lastDigit < 17) {
|
|
1700
|
+
let dIdx = 0;
|
|
1652
1701
|
significandLow = Long.fromNumber(digits[dIdx++]);
|
|
1653
1702
|
significandHigh = new Long(0, 0);
|
|
1654
1703
|
for (; dIdx <= lastDigit; dIdx++) {
|
|
@@ -1657,7 +1706,7 @@ class Decimal128 extends BSONValue {
|
|
|
1657
1706
|
}
|
|
1658
1707
|
}
|
|
1659
1708
|
else {
|
|
1660
|
-
let dIdx =
|
|
1709
|
+
let dIdx = 0;
|
|
1661
1710
|
significandHigh = Long.fromNumber(digits[dIdx++]);
|
|
1662
1711
|
for (; dIdx <= lastDigit - 17; dIdx++) {
|
|
1663
1712
|
significandHigh = significandHigh.multiply(Long.fromNumber(10));
|