bson 6.0.0-alpha.0 → 6.0.0

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/lib/bson.rn.cjs CHANGED
@@ -627,7 +627,7 @@ class Code extends BSONValue {
627
627
  }
628
628
  inspect() {
629
629
  const codeJson = this.toJSON();
630
- return `new Code("${String(codeJson.code)}"${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
630
+ return `new Code(${JSON.stringify(String(codeJson.code))}${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
631
631
  }
632
632
  }
633
633
 
@@ -1437,6 +1437,7 @@ class Decimal128 extends BSONValue {
1437
1437
  }
1438
1438
  static fromString(representation) {
1439
1439
  let isNegative = false;
1440
+ let sawSign = false;
1440
1441
  let sawRadix = false;
1441
1442
  let foundNonZero = false;
1442
1443
  let significantDigits = 0;
@@ -1447,10 +1448,8 @@ class Decimal128 extends BSONValue {
1447
1448
  const digits = [0];
1448
1449
  let nDigitsStored = 0;
1449
1450
  let digitsInsert = 0;
1450
- let firstDigit = 0;
1451
1451
  let lastDigit = 0;
1452
1452
  let exponent = 0;
1453
- let i = 0;
1454
1453
  let significandHigh = new Long(0, 0);
1455
1454
  let significandLow = new Long(0, 0);
1456
1455
  let biasedExponent = 0;
@@ -1478,6 +1477,7 @@ class Decimal128 extends BSONValue {
1478
1477
  }
1479
1478
  }
1480
1479
  if (representation[index] === '+' || representation[index] === '-') {
1480
+ sawSign = true;
1481
1481
  isNegative = representation[index++] === '-';
1482
1482
  }
1483
1483
  if (!isDigit(representation[index]) && representation[index] !== '.') {
@@ -1496,7 +1496,7 @@ class Decimal128 extends BSONValue {
1496
1496
  index = index + 1;
1497
1497
  continue;
1498
1498
  }
1499
- if (nDigitsStored < 34) {
1499
+ if (nDigitsStored < MAX_DIGITS) {
1500
1500
  if (representation[index] !== '0' || foundNonZero) {
1501
1501
  if (!foundNonZero) {
1502
1502
  firstNonZero = nDigitsRead;
@@ -1524,10 +1524,7 @@ class Decimal128 extends BSONValue {
1524
1524
  }
1525
1525
  if (representation[index])
1526
1526
  return new Decimal128(NAN_BUFFER);
1527
- firstDigit = 0;
1528
1527
  if (!nDigitsStored) {
1529
- firstDigit = 0;
1530
- lastDigit = 0;
1531
1528
  digits[0] = 0;
1532
1529
  nDigits = 1;
1533
1530
  nDigitsStored = 1;
@@ -1537,12 +1534,12 @@ class Decimal128 extends BSONValue {
1537
1534
  lastDigit = nDigitsStored - 1;
1538
1535
  significantDigits = nDigits;
1539
1536
  if (significantDigits !== 1) {
1540
- while (digits[firstNonZero + significantDigits - 1] === 0) {
1537
+ while (representation[firstNonZero + significantDigits - 1 + Number(sawSign) + Number(sawRadix)] === '0') {
1541
1538
  significantDigits = significantDigits - 1;
1542
1539
  }
1543
1540
  }
1544
1541
  }
1545
- if (exponent <= radixPosition && radixPosition - exponent > 1 << 14) {
1542
+ if (exponent <= radixPosition && radixPosition > exponent + (1 << 14)) {
1546
1543
  exponent = EXPONENT_MIN;
1547
1544
  }
1548
1545
  else {
@@ -1550,9 +1547,8 @@ class Decimal128 extends BSONValue {
1550
1547
  }
1551
1548
  while (exponent > EXPONENT_MAX) {
1552
1549
  lastDigit = lastDigit + 1;
1553
- if (lastDigit - firstDigit > MAX_DIGITS) {
1554
- const digitsString = digits.join('');
1555
- if (digitsString.match(/^0+$/)) {
1550
+ if (lastDigit >= MAX_DIGITS) {
1551
+ if (significantDigits === 0) {
1556
1552
  exponent = EXPONENT_MAX;
1557
1553
  break;
1558
1554
  }
@@ -1561,69 +1557,43 @@ class Decimal128 extends BSONValue {
1561
1557
  exponent = exponent - 1;
1562
1558
  }
1563
1559
  while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1564
- if (lastDigit === 0 && significantDigits < nDigitsStored) {
1565
- exponent = EXPONENT_MIN;
1566
- significantDigits = 0;
1567
- break;
1560
+ if (lastDigit === 0) {
1561
+ if (significantDigits === 0) {
1562
+ exponent = EXPONENT_MIN;
1563
+ break;
1564
+ }
1565
+ invalidErr(representation, 'exponent underflow');
1568
1566
  }
1569
1567
  if (nDigitsStored < nDigits) {
1568
+ if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
1569
+ significantDigits !== 0) {
1570
+ invalidErr(representation, 'inexact rounding');
1571
+ }
1570
1572
  nDigits = nDigits - 1;
1571
1573
  }
1572
1574
  else {
1575
+ if (digits[lastDigit] !== 0) {
1576
+ invalidErr(representation, 'inexact rounding');
1577
+ }
1573
1578
  lastDigit = lastDigit - 1;
1574
1579
  }
1575
1580
  if (exponent < EXPONENT_MAX) {
1576
1581
  exponent = exponent + 1;
1577
1582
  }
1578
1583
  else {
1579
- const digitsString = digits.join('');
1580
- if (digitsString.match(/^0+$/)) {
1581
- exponent = EXPONENT_MAX;
1582
- break;
1583
- }
1584
1584
  invalidErr(representation, 'overflow');
1585
1585
  }
1586
1586
  }
1587
- if (lastDigit - firstDigit + 1 < significantDigits) {
1588
- let endOfString = nDigitsRead;
1587
+ if (lastDigit + 1 < significantDigits) {
1589
1588
  if (sawRadix) {
1590
1589
  firstNonZero = firstNonZero + 1;
1591
- endOfString = endOfString + 1;
1592
1590
  }
1593
- if (isNegative) {
1591
+ if (sawSign) {
1594
1592
  firstNonZero = firstNonZero + 1;
1595
- endOfString = endOfString + 1;
1596
1593
  }
1597
1594
  const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
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
- }
1608
- }
1609
- }
1610
- }
1611
- if (roundBit) {
1612
- let dIdx = lastDigit;
1613
- for (; dIdx >= 0; dIdx--) {
1614
- if (++digits[dIdx] > 9) {
1615
- digits[dIdx] = 0;
1616
- if (dIdx === 0) {
1617
- if (exponent < EXPONENT_MAX) {
1618
- exponent = exponent + 1;
1619
- digits[dIdx] = 1;
1620
- }
1621
- else {
1622
- return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
1623
- }
1624
- }
1625
- }
1626
- }
1595
+ if (roundDigit !== 0) {
1596
+ invalidErr(representation, 'inexact rounding');
1627
1597
  }
1628
1598
  }
1629
1599
  significandHigh = Long.fromNumber(0);
@@ -1632,8 +1602,8 @@ class Decimal128 extends BSONValue {
1632
1602
  significandHigh = Long.fromNumber(0);
1633
1603
  significandLow = Long.fromNumber(0);
1634
1604
  }
1635
- else if (lastDigit - firstDigit < 17) {
1636
- let dIdx = firstDigit;
1605
+ else if (lastDigit < 17) {
1606
+ let dIdx = 0;
1637
1607
  significandLow = Long.fromNumber(digits[dIdx++]);
1638
1608
  significandHigh = new Long(0, 0);
1639
1609
  for (; dIdx <= lastDigit; dIdx++) {
@@ -1642,7 +1612,7 @@ class Decimal128 extends BSONValue {
1642
1612
  }
1643
1613
  }
1644
1614
  else {
1645
- let dIdx = firstDigit;
1615
+ let dIdx = 0;
1646
1616
  significandHigh = Long.fromNumber(digits[dIdx++]);
1647
1617
  for (; dIdx <= lastDigit - 17; dIdx++) {
1648
1618
  significandHigh = significandHigh.multiply(Long.fromNumber(10));
@@ -2364,7 +2334,7 @@ class BSONSymbol extends BSONValue {
2364
2334
  return this.value;
2365
2335
  }
2366
2336
  inspect() {
2367
- return `new BSONSymbol("${this.value}")`;
2337
+ return `new BSONSymbol(${JSON.stringify(this.value)})`;
2368
2338
  }
2369
2339
  toJSON() {
2370
2340
  return this.value;