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