Skip to content
This repository was archived by the owner on Feb 3, 2022. It is now read-only.

Commit b48ed7c

Browse files
committed
fix: Double precision EJSON fix and Spec update
Reduce the precision outputted for Double types NODE-2431
1 parent 148193b commit b48ed7c

37 files changed

+172
-2873
lines changed

etc/update-spec-tests.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
# This script is used to fetch the latest JSON tests for the BSON spec.
4+
# It puts the tests in the direcory $spec_root It should be run from the root of the repository.
5+
6+
set -o errexit
7+
set -o nounset
8+
9+
if [ ! -d ".git" ]; then
10+
echo "$0: This script must be run from the root of the repository" >&2
11+
exit 1
12+
fi
13+
14+
spec_root="test"
15+
16+
tmpdir=$(mktemp -d -t spec_testsXXXX)
17+
curl -sL https://github.com/mongodb/specifications/archive/master.zip -o "$tmpdir/specs.zip"
18+
unzip -d "$tmpdir" "$tmpdir/specs.zip" > /dev/null
19+
20+
mkdir -p "$spec_root/bson-corpus"
21+
rsync -ah "$tmpdir/specifications-master/source/bson-corpus/tests/" "$spec_root/bson-corpus" --delete
22+
23+
rm -rf "$tmpdir"

lib/bson/double.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
'use strict';
22

33
function toExtendedJSON(obj, options) {
4-
if (options && options.relaxed && isFinite(obj.value)) return obj.value;
5-
return { $numberDouble: obj.value.toString() };
4+
if (options && (options.legacy || (options.relaxed && isFinite(obj.value)))) {
5+
return obj.value;
6+
}
7+
// NOTE: JavaScript has +0 and -0, apparently to model limit calculations. If a user
8+
// explicitly provided `-0` then we need to ensure the sign makes it into the output
9+
if (Object.is(Math.sign(obj.value), -0)) {
10+
return { $numberDouble: `-${obj.value.toFixed(1)}` };
11+
}
12+
13+
var numberDouble;
14+
if (Number.isInteger(obj.value)) {
15+
numberDouble = obj.value.toFixed(1);
16+
if (numberDouble.length >= 13) {
17+
numberDouble = obj.value.toExponential(13).toUpperCase();
18+
}
19+
} else {
20+
numberDouble = obj.value.toString();
21+
}
22+
23+
return { $numberDouble: numberDouble };
624
}
725

826
function fromExtendedJSON(BSON, doc, options) {

package-lock.json

Lines changed: 98 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"eslint-plugin-prettier": "^2.3.1",
4242
"istanbul": "^0.4.5",
4343
"mocha": "^3.4.1",
44-
"mongodb": "^2.2.27",
44+
"mongodb": "^3.5",
4545
"prettier": "~1.12.0",
4646
"rollup": "^0.56.2",
4747
"rollup-plugin-babel": "^3.0.3",

test/bson-corpus/array.json

100755100644
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@
1414
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
1515
},
1616
{
17-
"description": "Single Element Array with index set incorrectly",
17+
"description": "Single Element Array with index set incorrectly to empty string",
1818
"degenerate_bson": "130000000461000B00000010000A0000000000",
1919
"canonical_bson": "140000000461000C0000001030000A0000000000",
2020
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
2121
},
2222
{
23-
"description": "Single Element Array with index set incorrectly",
23+
"description": "Single Element Array with index set incorrectly to ab",
2424
"degenerate_bson": "150000000461000D000000106162000A0000000000",
2525
"canonical_bson": "140000000461000C0000001030000A0000000000",
2626
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
27+
},
28+
{
29+
"description": "Multi Element Array with duplicate indexes",
30+
"degenerate_bson": "1b000000046100130000001030000a000000103000140000000000",
31+
"canonical_bson": "1b000000046100130000001030000a000000103100140000000000",
32+
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}, {\"$numberInt\": \"20\"}]}"
2733
}
2834
],
2935
"decodeErrors": [

test/bson-corpus/binary.json

100755100644
File mode changed.

test/bson-corpus/boolean.json

100755100644
File mode changed.

test/bson-corpus/code.json

100755100644
File mode changed.

test/bson-corpus/code_w_scope.json

100755100644
File mode changed.

test/bson-corpus/datetime.json

100755100644
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
"description" : "Y10K",
2626
"canonical_bson" : "1000000009610000DC1FD277E6000000",
2727
"canonical_extjson" : "{\"a\":{\"$date\":{\"$numberLong\":\"253402300800000\"}}}"
28+
},
29+
{
30+
"description": "leading zero ms",
31+
"canonical_bson": "10000000096100D1D6D6CC3B01000000",
32+
"relaxed_extjson": "{\"a\" : {\"$date\" : \"2012-12-24T12:15:30.001Z\"}}",
33+
"canonical_extjson": "{\"a\" : {\"$date\" : {\"$numberLong\" : \"1356351330001\"}}}"
2834
}
2935
],
3036
"decodeErrors": [

0 commit comments

Comments
 (0)