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