@@ -2,7 +2,6 @@ import * as BSON from '../register-bson';
22const EJSON = BSON . EJSON ;
33import * as vm from 'node:vm' ;
44import { expect } from 'chai' ;
5- import { BSONDataView } from '../../src/utils/byte_utils' ;
65import { BSONError } from '../../src' ;
76
87// BSON types
@@ -182,92 +181,6 @@ describe('Extended JSON', function () {
182181 expect ( EJSON . parse ( serialized ) ) . to . deep . equal ( numbers ) ;
183182 } ) ;
184183
185- it ( 'truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX] in canonical mode' , function ( ) {
186- const numbers = { a : 2n ** 64n + 1n , b : - ( 2n ** 64n ) - 1n } ;
187- const serialized = EJSON . stringify ( numbers , { relaxed : false } ) ;
188- expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}' ) ;
189- } ) ;
190-
191- it ( 'truncates bigint values in the same way as BSON.serialize in canonical mode' , function ( ) {
192- const number = { a : 0x1234_5678_1234_5678_9999n } ;
193- const stringified = EJSON . stringify ( number , { relaxed : false } ) ;
194- const serialized = BSON . serialize ( number ) ;
195-
196- const VALUE_OFFSET = 7 ;
197- const dataView = BSONDataView . fromUint8Array ( serialized ) ;
198- const serializedValue = dataView . getBigInt64 ( VALUE_OFFSET , true ) ;
199- const parsed = JSON . parse ( stringified ) ;
200-
201- expect ( parsed ) . to . have . property ( 'a' ) ;
202- expect ( parsed [ 'a' ] ) . to . have . property ( '$numberLong' ) ;
203- expect ( parsed . a . $numberLong ) . to . equal ( 0x5678_1234_5678_9999n . toString ( ) ) ;
204-
205- expect ( parsed . a . $numberLong ) . to . equal ( serializedValue . toString ( ) ) ;
206- } ) ;
207-
208- it ( 'truncates bigint values in the same way as BSON.serialize in relaxed mode' , function ( ) {
209- const number = { a : 0x1234_0000_1234_5678_9999n } ; // Ensure that the truncated number can be exactly represented as a JS number
210- const stringified = EJSON . stringify ( number , { relaxed : true } ) ;
211- const serializedDoc = BSON . serialize ( number ) ;
212-
213- const VALUE_OFFSET = 7 ;
214- const dataView = BSONDataView . fromUint8Array ( serializedDoc ) ;
215- const parsed = JSON . parse ( stringified ) ;
216-
217- expect ( parsed ) . to . have . property ( 'a' ) ;
218- expect ( parsed . a ) . to . equal ( 0x0000_1234_5678_9999 ) ;
219-
220- expect ( parsed . a ) . to . equal ( Number ( dataView . getBigInt64 ( VALUE_OFFSET , true ) ) ) ;
221- } ) ;
222-
223- it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
224- const number = { a : 2n } ;
225- const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
226- expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"2"}}' ) ;
227- } ) ;
228-
229- it ( 'serializes bigint values to Number in relaxed mode' , function ( ) {
230- const number = { a : 10000n } ;
231- const serialized = EJSON . stringify ( number , { relaxed : true } ) ;
232- expect ( serialized ) . to . equal ( '{"a":10000}' ) ;
233- } ) ;
234-
235- it ( 'loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER] in relaxed mode' , function ( ) {
236- const numbers = { a : - ( 2n ** 53n ) - 1n , b : 2n ** 53n + 2n } ;
237- const serialized = EJSON . stringify ( numbers , { relaxed : true } ) ;
238- expect ( serialized ) . to . equal ( '{"a":-9007199254740992,"b":9007199254740994}' ) ;
239- } ) ;
240-
241- it ( 'produces bigint strings that pass loose equality checks with native bigint values that are are 64 bits wide or less' , function ( ) {
242- const number = { a : 12345n } ;
243- const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
244- const parsed = JSON . parse ( serialized ) ;
245- // eslint-disable-next-line eqeqeq
246- expect ( parsed . a . $numberLong == 12345n ) . true ;
247- } ) ;
248-
249- it ( 'produces bigint strings that are equal to the strings generated when using BigInt.toString when the bigint values used are 64 bits wide or less' , function ( ) {
250- const number = { a : 12345n } ;
251- const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
252- const parsed = JSON . parse ( serialized ) ;
253- expect ( parsed . a . $numberLong ) . to . equal ( 12345n . toString ( ) ) ;
254- } ) ;
255-
256- it ( 'produces bigint strings that fail loose equality checks with native bigint values that are more than 64 bits wide' , function ( ) {
257- const number = { a : 0x1234_5678_1234_5678_9999n } ;
258- const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
259- const parsed = JSON . parse ( serialized ) ;
260- // eslint-disable-next-line eqeqeq
261- expect ( parsed . a . $numberLong == 0x1234_5678_1234_5678_9999n ) . false ;
262- } ) ;
263-
264- it ( 'produces bigint strings that are not equal to the strings generated when using BigInt.toString when the bigint values used are more than 64 bits wide' , function ( ) {
265- const number = { a : 0x1234_5678_1234_5678_9999n } ;
266- const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
267- const parsed = JSON . parse ( serialized ) ;
268- expect ( parsed . a . $numberLong ) . to . not . equal ( 0x1234_5678_1234_5678_9999n . toString ( ) ) ;
269- } ) ;
270-
271184 it ( 'should correctly parse null values' , function ( ) {
272185 expect ( EJSON . parse ( 'null' ) ) . to . be . null ;
273186 expect ( EJSON . parse ( '[null]' ) [ 0 ] ) . to . be . null ;
0 commit comments