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