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.cjs
CHANGED
|
@@ -95,7 +95,7 @@ class BSONVersionError extends BSONError {
|
|
|
95
95
|
return 'BSONVersionError';
|
|
96
96
|
}
|
|
97
97
|
constructor() {
|
|
98
|
-
super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.
|
|
98
|
+
super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.x.x`);
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
class BSONRuntimeError extends BSONError {
|
|
@@ -1436,7 +1436,14 @@ class Decimal128 extends BSONValue {
|
|
|
1436
1436
|
}
|
|
1437
1437
|
}
|
|
1438
1438
|
static fromString(representation) {
|
|
1439
|
+
return Decimal128._fromString(representation, { allowRounding: false });
|
|
1440
|
+
}
|
|
1441
|
+
static fromStringWithRounding(representation) {
|
|
1442
|
+
return Decimal128._fromString(representation, { allowRounding: true });
|
|
1443
|
+
}
|
|
1444
|
+
static _fromString(representation, options) {
|
|
1439
1445
|
let isNegative = false;
|
|
1446
|
+
let sawSign = false;
|
|
1440
1447
|
let sawRadix = false;
|
|
1441
1448
|
let foundNonZero = false;
|
|
1442
1449
|
let significantDigits = 0;
|
|
@@ -1447,10 +1454,8 @@ class Decimal128 extends BSONValue {
|
|
|
1447
1454
|
const digits = [0];
|
|
1448
1455
|
let nDigitsStored = 0;
|
|
1449
1456
|
let digitsInsert = 0;
|
|
1450
|
-
let firstDigit = 0;
|
|
1451
1457
|
let lastDigit = 0;
|
|
1452
1458
|
let exponent = 0;
|
|
1453
|
-
let i = 0;
|
|
1454
1459
|
let significandHigh = new Long(0, 0);
|
|
1455
1460
|
let significandLow = new Long(0, 0);
|
|
1456
1461
|
let biasedExponent = 0;
|
|
@@ -1478,6 +1483,7 @@ class Decimal128 extends BSONValue {
|
|
|
1478
1483
|
}
|
|
1479
1484
|
}
|
|
1480
1485
|
if (representation[index] === '+' || representation[index] === '-') {
|
|
1486
|
+
sawSign = true;
|
|
1481
1487
|
isNegative = representation[index++] === '-';
|
|
1482
1488
|
}
|
|
1483
1489
|
if (!isDigit(representation[index]) && representation[index] !== '.') {
|
|
@@ -1496,7 +1502,7 @@ class Decimal128 extends BSONValue {
|
|
|
1496
1502
|
index = index + 1;
|
|
1497
1503
|
continue;
|
|
1498
1504
|
}
|
|
1499
|
-
if (nDigitsStored <
|
|
1505
|
+
if (nDigitsStored < MAX_DIGITS) {
|
|
1500
1506
|
if (representation[index] !== '0' || foundNonZero) {
|
|
1501
1507
|
if (!foundNonZero) {
|
|
1502
1508
|
firstNonZero = nDigitsRead;
|
|
@@ -1524,10 +1530,7 @@ class Decimal128 extends BSONValue {
|
|
|
1524
1530
|
}
|
|
1525
1531
|
if (representation[index])
|
|
1526
1532
|
return new Decimal128(NAN_BUFFER);
|
|
1527
|
-
firstDigit = 0;
|
|
1528
1533
|
if (!nDigitsStored) {
|
|
1529
|
-
firstDigit = 0;
|
|
1530
|
-
lastDigit = 0;
|
|
1531
1534
|
digits[0] = 0;
|
|
1532
1535
|
nDigits = 1;
|
|
1533
1536
|
nDigitsStored = 1;
|
|
@@ -1537,12 +1540,12 @@ class Decimal128 extends BSONValue {
|
|
|
1537
1540
|
lastDigit = nDigitsStored - 1;
|
|
1538
1541
|
significantDigits = nDigits;
|
|
1539
1542
|
if (significantDigits !== 1) {
|
|
1540
|
-
while (
|
|
1543
|
+
while (representation[firstNonZero + significantDigits - 1 + Number(sawSign) + Number(sawRadix)] === '0') {
|
|
1541
1544
|
significantDigits = significantDigits - 1;
|
|
1542
1545
|
}
|
|
1543
1546
|
}
|
|
1544
1547
|
}
|
|
1545
|
-
if (exponent <= radixPosition && radixPosition
|
|
1548
|
+
if (exponent <= radixPosition && radixPosition > exponent + (1 << 14)) {
|
|
1546
1549
|
exponent = EXPONENT_MIN;
|
|
1547
1550
|
}
|
|
1548
1551
|
else {
|
|
@@ -1550,9 +1553,8 @@ class Decimal128 extends BSONValue {
|
|
|
1550
1553
|
}
|
|
1551
1554
|
while (exponent > EXPONENT_MAX) {
|
|
1552
1555
|
lastDigit = lastDigit + 1;
|
|
1553
|
-
if (lastDigit
|
|
1554
|
-
|
|
1555
|
-
if (digitsString.match(/^0+$/)) {
|
|
1556
|
+
if (lastDigit >= MAX_DIGITS) {
|
|
1557
|
+
if (significantDigits === 0) {
|
|
1556
1558
|
exponent = EXPONENT_MAX;
|
|
1557
1559
|
break;
|
|
1558
1560
|
}
|
|
@@ -1560,69 +1562,116 @@ class Decimal128 extends BSONValue {
|
|
|
1560
1562
|
}
|
|
1561
1563
|
exponent = exponent - 1;
|
|
1562
1564
|
}
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
}
|
|
1569
|
-
if (nDigitsStored < nDigits) {
|
|
1570
|
-
nDigits = nDigits - 1;
|
|
1571
|
-
}
|
|
1572
|
-
else {
|
|
1573
|
-
lastDigit = lastDigit - 1;
|
|
1574
|
-
}
|
|
1575
|
-
if (exponent < EXPONENT_MAX) {
|
|
1576
|
-
exponent = exponent + 1;
|
|
1577
|
-
}
|
|
1578
|
-
else {
|
|
1579
|
-
const digitsString = digits.join('');
|
|
1580
|
-
if (digitsString.match(/^0+$/)) {
|
|
1581
|
-
exponent = EXPONENT_MAX;
|
|
1565
|
+
if (options.allowRounding) {
|
|
1566
|
+
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
|
|
1567
|
+
if (lastDigit === 0 && significantDigits < nDigitsStored) {
|
|
1568
|
+
exponent = EXPONENT_MIN;
|
|
1569
|
+
significantDigits = 0;
|
|
1582
1570
|
break;
|
|
1583
1571
|
}
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
let roundBit = 0;
|
|
1599
|
-
if (roundDigit >= 5) {
|
|
1600
|
-
roundBit = 1;
|
|
1601
|
-
if (roundDigit === 5) {
|
|
1602
|
-
roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
|
|
1603
|
-
for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
|
|
1604
|
-
if (parseInt(representation[i], 10)) {
|
|
1605
|
-
roundBit = 1;
|
|
1606
|
-
break;
|
|
1607
|
-
}
|
|
1572
|
+
if (nDigitsStored < nDigits) {
|
|
1573
|
+
nDigits = nDigits - 1;
|
|
1574
|
+
}
|
|
1575
|
+
else {
|
|
1576
|
+
lastDigit = lastDigit - 1;
|
|
1577
|
+
}
|
|
1578
|
+
if (exponent < EXPONENT_MAX) {
|
|
1579
|
+
exponent = exponent + 1;
|
|
1580
|
+
}
|
|
1581
|
+
else {
|
|
1582
|
+
const digitsString = digits.join('');
|
|
1583
|
+
if (digitsString.match(/^0+$/)) {
|
|
1584
|
+
exponent = EXPONENT_MAX;
|
|
1585
|
+
break;
|
|
1608
1586
|
}
|
|
1587
|
+
invalidErr(representation, 'overflow');
|
|
1609
1588
|
}
|
|
1610
1589
|
}
|
|
1611
|
-
if (
|
|
1612
|
-
let
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1590
|
+
if (lastDigit + 1 < significantDigits) {
|
|
1591
|
+
let endOfString = nDigitsRead;
|
|
1592
|
+
if (sawRadix) {
|
|
1593
|
+
firstNonZero = firstNonZero + 1;
|
|
1594
|
+
endOfString = endOfString + 1;
|
|
1595
|
+
}
|
|
1596
|
+
if (sawSign) {
|
|
1597
|
+
firstNonZero = firstNonZero + 1;
|
|
1598
|
+
endOfString = endOfString + 1;
|
|
1599
|
+
}
|
|
1600
|
+
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
|
|
1601
|
+
let roundBit = 0;
|
|
1602
|
+
if (roundDigit >= 5) {
|
|
1603
|
+
roundBit = 1;
|
|
1604
|
+
if (roundDigit === 5) {
|
|
1605
|
+
roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
|
|
1606
|
+
for (let i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
|
|
1607
|
+
if (parseInt(representation[i], 10)) {
|
|
1608
|
+
roundBit = 1;
|
|
1609
|
+
break;
|
|
1620
1610
|
}
|
|
1621
|
-
|
|
1622
|
-
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
if (roundBit) {
|
|
1615
|
+
let dIdx = lastDigit;
|
|
1616
|
+
for (; dIdx >= 0; dIdx--) {
|
|
1617
|
+
if (++digits[dIdx] > 9) {
|
|
1618
|
+
digits[dIdx] = 0;
|
|
1619
|
+
if (dIdx === 0) {
|
|
1620
|
+
if (exponent < EXPONENT_MAX) {
|
|
1621
|
+
exponent = exponent + 1;
|
|
1622
|
+
digits[dIdx] = 1;
|
|
1623
|
+
}
|
|
1624
|
+
else {
|
|
1625
|
+
return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
|
|
1626
|
+
}
|
|
1623
1627
|
}
|
|
1624
1628
|
}
|
|
1629
|
+
else {
|
|
1630
|
+
break;
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
else {
|
|
1637
|
+
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
|
|
1638
|
+
if (lastDigit === 0) {
|
|
1639
|
+
if (significantDigits === 0) {
|
|
1640
|
+
exponent = EXPONENT_MIN;
|
|
1641
|
+
break;
|
|
1625
1642
|
}
|
|
1643
|
+
invalidErr(representation, 'exponent underflow');
|
|
1644
|
+
}
|
|
1645
|
+
if (nDigitsStored < nDigits) {
|
|
1646
|
+
if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
|
|
1647
|
+
significantDigits !== 0) {
|
|
1648
|
+
invalidErr(representation, 'inexact rounding');
|
|
1649
|
+
}
|
|
1650
|
+
nDigits = nDigits - 1;
|
|
1651
|
+
}
|
|
1652
|
+
else {
|
|
1653
|
+
if (digits[lastDigit] !== 0) {
|
|
1654
|
+
invalidErr(representation, 'inexact rounding');
|
|
1655
|
+
}
|
|
1656
|
+
lastDigit = lastDigit - 1;
|
|
1657
|
+
}
|
|
1658
|
+
if (exponent < EXPONENT_MAX) {
|
|
1659
|
+
exponent = exponent + 1;
|
|
1660
|
+
}
|
|
1661
|
+
else {
|
|
1662
|
+
invalidErr(representation, 'overflow');
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
if (lastDigit + 1 < significantDigits) {
|
|
1666
|
+
if (sawRadix) {
|
|
1667
|
+
firstNonZero = firstNonZero + 1;
|
|
1668
|
+
}
|
|
1669
|
+
if (sawSign) {
|
|
1670
|
+
firstNonZero = firstNonZero + 1;
|
|
1671
|
+
}
|
|
1672
|
+
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
|
|
1673
|
+
if (roundDigit !== 0) {
|
|
1674
|
+
invalidErr(representation, 'inexact rounding');
|
|
1626
1675
|
}
|
|
1627
1676
|
}
|
|
1628
1677
|
}
|
|
@@ -1632,8 +1681,8 @@ class Decimal128 extends BSONValue {
|
|
|
1632
1681
|
significandHigh = Long.fromNumber(0);
|
|
1633
1682
|
significandLow = Long.fromNumber(0);
|
|
1634
1683
|
}
|
|
1635
|
-
else if (lastDigit
|
|
1636
|
-
let dIdx =
|
|
1684
|
+
else if (lastDigit < 17) {
|
|
1685
|
+
let dIdx = 0;
|
|
1637
1686
|
significandLow = Long.fromNumber(digits[dIdx++]);
|
|
1638
1687
|
significandHigh = new Long(0, 0);
|
|
1639
1688
|
for (; dIdx <= lastDigit; dIdx++) {
|
|
@@ -1642,7 +1691,7 @@ class Decimal128 extends BSONValue {
|
|
|
1642
1691
|
}
|
|
1643
1692
|
}
|
|
1644
1693
|
else {
|
|
1645
|
-
let dIdx =
|
|
1694
|
+
let dIdx = 0;
|
|
1646
1695
|
significandHigh = Long.fromNumber(digits[dIdx++]);
|
|
1647
1696
|
for (; dIdx <= lastDigit - 17; dIdx++) {
|
|
1648
1697
|
significandHigh = significandHigh.multiply(Long.fromNumber(10));
|