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.cjs CHANGED
@@ -612,7 +612,7 @@ class Code extends BSONValue {
612
612
  }
613
613
  inspect() {
614
614
  const codeJson = this.toJSON();
615
- return `new Code("${String(codeJson.code)}"${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
615
+ return `new Code(${JSON.stringify(String(codeJson.code))}${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
616
616
  }
617
617
  }
618
618
 
@@ -1422,6 +1422,7 @@ class Decimal128 extends BSONValue {
1422
1422
  }
1423
1423
  static fromString(representation) {
1424
1424
  let isNegative = false;
1425
+ let sawSign = false;
1425
1426
  let sawRadix = false;
1426
1427
  let foundNonZero = false;
1427
1428
  let significantDigits = 0;
@@ -1432,10 +1433,8 @@ class Decimal128 extends BSONValue {
1432
1433
  const digits = [0];
1433
1434
  let nDigitsStored = 0;
1434
1435
  let digitsInsert = 0;
1435
- let firstDigit = 0;
1436
1436
  let lastDigit = 0;
1437
1437
  let exponent = 0;
1438
- let i = 0;
1439
1438
  let significandHigh = new Long(0, 0);
1440
1439
  let significandLow = new Long(0, 0);
1441
1440
  let biasedExponent = 0;
@@ -1463,6 +1462,7 @@ class Decimal128 extends BSONValue {
1463
1462
  }
1464
1463
  }
1465
1464
  if (representation[index] === '+' || representation[index] === '-') {
1465
+ sawSign = true;
1466
1466
  isNegative = representation[index++] === '-';
1467
1467
  }
1468
1468
  if (!isDigit(representation[index]) && representation[index] !== '.') {
@@ -1481,7 +1481,7 @@ class Decimal128 extends BSONValue {
1481
1481
  index = index + 1;
1482
1482
  continue;
1483
1483
  }
1484
- if (nDigitsStored < 34) {
1484
+ if (nDigitsStored < MAX_DIGITS) {
1485
1485
  if (representation[index] !== '0' || foundNonZero) {
1486
1486
  if (!foundNonZero) {
1487
1487
  firstNonZero = nDigitsRead;
@@ -1509,10 +1509,7 @@ class Decimal128 extends BSONValue {
1509
1509
  }
1510
1510
  if (representation[index])
1511
1511
  return new Decimal128(NAN_BUFFER);
1512
- firstDigit = 0;
1513
1512
  if (!nDigitsStored) {
1514
- firstDigit = 0;
1515
- lastDigit = 0;
1516
1513
  digits[0] = 0;
1517
1514
  nDigits = 1;
1518
1515
  nDigitsStored = 1;
@@ -1522,12 +1519,12 @@ class Decimal128 extends BSONValue {
1522
1519
  lastDigit = nDigitsStored - 1;
1523
1520
  significantDigits = nDigits;
1524
1521
  if (significantDigits !== 1) {
1525
- while (digits[firstNonZero + significantDigits - 1] === 0) {
1522
+ while (representation[firstNonZero + significantDigits - 1 + Number(sawSign) + Number(sawRadix)] === '0') {
1526
1523
  significantDigits = significantDigits - 1;
1527
1524
  }
1528
1525
  }
1529
1526
  }
1530
- if (exponent <= radixPosition && radixPosition - exponent > 1 << 14) {
1527
+ if (exponent <= radixPosition && radixPosition > exponent + (1 << 14)) {
1531
1528
  exponent = EXPONENT_MIN;
1532
1529
  }
1533
1530
  else {
@@ -1535,9 +1532,8 @@ class Decimal128 extends BSONValue {
1535
1532
  }
1536
1533
  while (exponent > EXPONENT_MAX) {
1537
1534
  lastDigit = lastDigit + 1;
1538
- if (lastDigit - firstDigit > MAX_DIGITS) {
1539
- const digitsString = digits.join('');
1540
- if (digitsString.match(/^0+$/)) {
1535
+ if (lastDigit >= MAX_DIGITS) {
1536
+ if (significantDigits === 0) {
1541
1537
  exponent = EXPONENT_MAX;
1542
1538
  break;
1543
1539
  }
@@ -1546,69 +1542,43 @@ class Decimal128 extends BSONValue {
1546
1542
  exponent = exponent - 1;
1547
1543
  }
1548
1544
  while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1549
- if (lastDigit === 0 && significantDigits < nDigitsStored) {
1550
- exponent = EXPONENT_MIN;
1551
- significantDigits = 0;
1552
- break;
1545
+ if (lastDigit === 0) {
1546
+ if (significantDigits === 0) {
1547
+ exponent = EXPONENT_MIN;
1548
+ break;
1549
+ }
1550
+ invalidErr(representation, 'exponent underflow');
1553
1551
  }
1554
1552
  if (nDigitsStored < nDigits) {
1553
+ if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
1554
+ significantDigits !== 0) {
1555
+ invalidErr(representation, 'inexact rounding');
1556
+ }
1555
1557
  nDigits = nDigits - 1;
1556
1558
  }
1557
1559
  else {
1560
+ if (digits[lastDigit] !== 0) {
1561
+ invalidErr(representation, 'inexact rounding');
1562
+ }
1558
1563
  lastDigit = lastDigit - 1;
1559
1564
  }
1560
1565
  if (exponent < EXPONENT_MAX) {
1561
1566
  exponent = exponent + 1;
1562
1567
  }
1563
1568
  else {
1564
- const digitsString = digits.join('');
1565
- if (digitsString.match(/^0+$/)) {
1566
- exponent = EXPONENT_MAX;
1567
- break;
1568
- }
1569
1569
  invalidErr(representation, 'overflow');
1570
1570
  }
1571
1571
  }
1572
- if (lastDigit - firstDigit + 1 < significantDigits) {
1573
- let endOfString = nDigitsRead;
1572
+ if (lastDigit + 1 < significantDigits) {
1574
1573
  if (sawRadix) {
1575
1574
  firstNonZero = firstNonZero + 1;
1576
- endOfString = endOfString + 1;
1577
1575
  }
1578
- if (isNegative) {
1576
+ if (sawSign) {
1579
1577
  firstNonZero = firstNonZero + 1;
1580
- endOfString = endOfString + 1;
1581
1578
  }
1582
1579
  const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1583
- let roundBit = 0;
1584
- if (roundDigit >= 5) {
1585
- roundBit = 1;
1586
- if (roundDigit === 5) {
1587
- roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
1588
- for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
1589
- if (parseInt(representation[i], 10)) {
1590
- roundBit = 1;
1591
- break;
1592
- }
1593
- }
1594
- }
1595
- }
1596
- if (roundBit) {
1597
- let dIdx = lastDigit;
1598
- for (; dIdx >= 0; dIdx--) {
1599
- if (++digits[dIdx] > 9) {
1600
- digits[dIdx] = 0;
1601
- if (dIdx === 0) {
1602
- if (exponent < EXPONENT_MAX) {
1603
- exponent = exponent + 1;
1604
- digits[dIdx] = 1;
1605
- }
1606
- else {
1607
- return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
1608
- }
1609
- }
1610
- }
1611
- }
1580
+ if (roundDigit !== 0) {
1581
+ invalidErr(representation, 'inexact rounding');
1612
1582
  }
1613
1583
  }
1614
1584
  significandHigh = Long.fromNumber(0);
@@ -1617,8 +1587,8 @@ class Decimal128 extends BSONValue {
1617
1587
  significandHigh = Long.fromNumber(0);
1618
1588
  significandLow = Long.fromNumber(0);
1619
1589
  }
1620
- else if (lastDigit - firstDigit < 17) {
1621
- let dIdx = firstDigit;
1590
+ else if (lastDigit < 17) {
1591
+ let dIdx = 0;
1622
1592
  significandLow = Long.fromNumber(digits[dIdx++]);
1623
1593
  significandHigh = new Long(0, 0);
1624
1594
  for (; dIdx <= lastDigit; dIdx++) {
@@ -1627,7 +1597,7 @@ class Decimal128 extends BSONValue {
1627
1597
  }
1628
1598
  }
1629
1599
  else {
1630
- let dIdx = firstDigit;
1600
+ let dIdx = 0;
1631
1601
  significandHigh = Long.fromNumber(digits[dIdx++]);
1632
1602
  for (; dIdx <= lastDigit - 17; dIdx++) {
1633
1603
  significandHigh = significandHigh.multiply(Long.fromNumber(10));
@@ -2349,7 +2319,7 @@ class BSONSymbol extends BSONValue {
2349
2319
  return this.value;
2350
2320
  }
2351
2321
  inspect() {
2352
- return `new BSONSymbol("${this.value}")`;
2322
+ return `new BSONSymbol(${JSON.stringify(this.value)})`;
2353
2323
  }
2354
2324
  toJSON() {
2355
2325
  return this.value;