@@ -16,10 +16,21 @@ const Long = BSON.Long;
16
16
const MaxKey = BSON . MaxKey ;
17
17
const MinKey = BSON . MinKey ;
18
18
const ObjectID = BSON . ObjectID ;
19
+ const ObjectId = BSON . ObjectId ;
19
20
const BSONRegExp = BSON . BSONRegExp ;
20
21
const BSONSymbol = BSON . BSONSymbol ;
21
22
const Timestamp = BSON . Timestamp ;
22
23
24
+ // support old ObjectID class because MongoDB drivers still return it
25
+ const OldObjectID = ( function ( ) {
26
+ try {
27
+ return require ( 'mongodb' ) . ObjectID ;
28
+ }
29
+ catch {
30
+ return ObjectId ; // if mongo is unavailable, e.g. browsers, just re-use BSON's
31
+ }
32
+ } ) ( ) ;
33
+
23
34
describe ( 'Extended JSON' , function ( ) {
24
35
let doc = { } ;
25
36
@@ -41,7 +52,9 @@ describe('Extended JSON', function() {
41
52
long : Long . fromNumber ( 200 ) ,
42
53
maxKey : new MaxKey ( ) ,
43
54
minKey : new MinKey ( ) ,
44
- objectId : ObjectID . createFromHexString ( '111111111111111111111111' ) ,
55
+ objectId : ObjectId . createFromHexString ( '111111111111111111111111' ) ,
56
+ objectID : ObjectID . createFromHexString ( '111111111111111111111111' ) ,
57
+ oldObjectID : OldObjectID . createFromHexString ( '111111111111111111111111' ) ,
45
58
regexp : new BSONRegExp ( 'hello world' , 'i' ) ,
46
59
symbol : new BSONSymbol ( 'symbol' ) ,
47
60
timestamp : Timestamp . fromNumber ( 1000 ) ,
@@ -55,7 +68,7 @@ describe('Extended JSON', function() {
55
68
it ( 'should correctly extend an existing mongodb module' , function ( ) {
56
69
// Serialize the document
57
70
var json =
58
- '{"_id":{"$numberInt":"100"},"gh":{"$numberInt":"1"},"binary":{"$binary":{"base64":"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==","subType":"00"}},"date":{"$date":{"$numberLong":"1488372056737"}},"code":{"$code":"function() {}","$scope":{"a":{"$numberInt":"1"}}},"dbRef":{"$ref":"tests","$id":{"$numberInt":"1"},"$db":"test"},"decimal":{"$numberDecimal":"100"},"double":{"$numberDouble":"10.1"},"int32":{"$numberInt":"10"},"long":{"$numberLong":"200"},"maxKey":{"$maxKey":1},"minKey":{"$minKey":1},"objectId":{"$oid":"111111111111111111111111"},"regexp":{"$regularExpression":{"pattern":"hello world","options":"i"}},"symbol":{"$symbol":"symbol"},"timestamp":{"$timestamp":{"t":0,"i":1000}},"int32Number":{"$numberInt":"300"},"doubleNumber":{"$numberDouble":"200.2"},"longNumberIntFit":{"$numberLong":"7036874417766400"},"doubleNumberIntFit":{"$numberLong":"19007199250000000"}}' ;
71
+ '{"_id":{"$numberInt":"100"},"gh":{"$numberInt":"1"},"binary":{"$binary":{"base64":"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==","subType":"00"}},"date":{"$date":{"$numberLong":"1488372056737"}},"code":{"$code":"function() {}","$scope":{"a":{"$numberInt":"1"}}},"dbRef":{"$ref":"tests","$id":{"$numberInt":"1"},"$db":"test"},"decimal":{"$numberDecimal":"100"},"double":{"$numberDouble":"10.1"},"int32":{"$numberInt":"10"},"long":{"$numberLong":"200"},"maxKey":{"$maxKey":1},"minKey":{"$minKey":1},"objectId":{"$oid":"111111111111111111111111"},"objectID":{"$oid":"111111111111111111111111"},"oldObjectID":{"$oid":"111111111111111111111111"},"regexp":{"$regularExpression":{"pattern":"hello world","options":"i"}},"symbol":{"$symbol":"symbol"},"timestamp":{"$timestamp":{"t":0,"i":1000}},"int32Number":{"$numberInt":"300"},"doubleNumber":{"$numberDouble":"200.2"},"longNumberIntFit":{"$numberLong":"7036874417766400"},"doubleNumberIntFit":{"$numberLong":"19007199250000000"}}' ;
59
72
60
73
assert . equal ( json , EJSON . stringify ( doc , null , 0 , { relaxed : false } ) ) ;
61
74
} ) ;
@@ -98,17 +111,36 @@ describe('Extended JSON', function() {
98
111
} ) ;
99
112
100
113
it ( 'should correctly serialize bson types when they are values' , function ( ) {
101
- var serialized = EJSON . stringify ( new ObjectID ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
114
+ var serialized = EJSON . stringify ( new ObjectId ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
115
+ expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
116
+ serialized = EJSON . stringify ( new ObjectID ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
102
117
expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
118
+ serialized = EJSON . stringify ( new OldObjectID ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
119
+ expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
120
+
103
121
serialized = EJSON . stringify ( new Int32 ( 42 ) , { relaxed : false } ) ;
104
122
expect ( serialized ) . to . equal ( '{"$numberInt":"42"}' ) ;
123
+ serialized = EJSON . stringify (
124
+ {
125
+ _id : { $nin : [ new ObjectId ( '591801a468f9e7024b6235ea' ) ] }
126
+ } ,
127
+ { relaxed : false }
128
+ ) ;
129
+ expect ( serialized ) . to . equal ( '{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}' ) ;
105
130
serialized = EJSON . stringify (
106
131
{
107
132
_id : { $nin : [ new ObjectID ( '591801a468f9e7024b6235ea' ) ] }
108
133
} ,
109
134
{ relaxed : false }
110
135
) ;
111
136
expect ( serialized ) . to . equal ( '{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}' ) ;
137
+ serialized = EJSON . stringify (
138
+ {
139
+ _id : { $nin : [ new OldObjectID ( '591801a468f9e7024b6235ea' ) ] }
140
+ } ,
141
+ { relaxed : false }
142
+ ) ;
143
+ expect ( serialized ) . to . equal ( '{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}' ) ;
112
144
113
145
serialized = EJSON . stringify ( new Binary ( new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] ) ) , { relaxed : false } ) ;
114
146
expect ( serialized ) . to . equal ( '{"$binary":{"base64":"AQIDBAU=","subType":"00"}}' ) ;
@@ -122,7 +154,7 @@ describe('Extended JSON', function() {
122
154
var parsed = EJSON . parse ( input ) ;
123
155
124
156
expect ( parsed ) . to . deep . equal ( {
125
- result : [ { _id : new ObjectID ( '591801a468f9e7024b623939' ) , emptyField : null } ]
157
+ result : [ { _id : new ObjectId ( '591801a468f9e7024b623939' ) , emptyField : null } ]
126
158
} ) ;
127
159
} ) ;
128
160
@@ -170,7 +202,9 @@ describe('Extended JSON', function() {
170
202
long : new Long ( 234 ) ,
171
203
maxKey : new MaxKey ( ) ,
172
204
minKey : new MinKey ( ) ,
205
+ objectId : ObjectId . createFromHexString ( '111111111111111111111111' ) ,
173
206
objectID : ObjectID . createFromHexString ( '111111111111111111111111' ) ,
207
+ oldObjectID : OldObjectID . createFromHexString ( '111111111111111111111111' ) ,
174
208
bsonRegExp : new BSONRegExp ( 'hello world' , 'i' ) ,
175
209
symbol : new BSONSymbol ( 'symbol' ) ,
176
210
timestamp : new Timestamp ( )
@@ -187,7 +221,9 @@ describe('Extended JSON', function() {
187
221
long : { $numberLong : '234' } ,
188
222
maxKey : { $maxKey : 1 } ,
189
223
minKey : { $minKey : 1 } ,
224
+ objectId : { $oid : '111111111111111111111111' } ,
190
225
objectID : { $oid : '111111111111111111111111' } ,
226
+ oldObjectID : { $oid : '111111111111111111111111' } ,
191
227
bsonRegExp : { $regularExpression : { pattern : 'hello world' , options : 'i' } } ,
192
228
symbol : { $symbol : 'symbol' } ,
193
229
timestamp : { $timestamp : { t : 0 , i : 0 } }
@@ -205,7 +241,9 @@ describe('Extended JSON', function() {
205
241
long : { $numberLong : '234' } ,
206
242
maxKey : { $maxKey : 1 } ,
207
243
minKey : { $minKey : 1 } ,
244
+ objectId : { $oid : '111111111111111111111111' } ,
208
245
objectID : { $oid : '111111111111111111111111' } ,
246
+ oldObjectID : { $oid : '111111111111111111111111' } ,
209
247
bsonRegExp : { $regularExpression : { pattern : 'hello world' , options : 'i' } } ,
210
248
symbol : { $symbol : 'symbol' } ,
211
249
timestamp : { $timestamp : { t : 0 , i : 0 } }
@@ -237,7 +275,9 @@ describe('Extended JSON', function() {
237
275
// minKey
238
276
expect ( result . minKey ) . to . be . an . instanceOf ( BSON . MinKey ) ;
239
277
// objectID
278
+ expect ( result . objectId . toString ( ) ) . to . equal ( '111111111111111111111111' ) ;
240
279
expect ( result . objectID . toString ( ) ) . to . equal ( '111111111111111111111111' ) ;
280
+ expect ( result . oldObjectID . toString ( ) ) . to . equal ( '111111111111111111111111' ) ;
241
281
//bsonRegExp
242
282
expect ( result . bsonRegExp ) . to . be . an . instanceOf ( BSON . BSONRegExp ) ;
243
283
expect ( result . bsonRegExp . pattern ) . to . equal ( 'hello world' ) ;
@@ -253,4 +293,22 @@ describe('Extended JSON', function() {
253
293
expect ( result . test ) . to . equal ( 34.12 ) ;
254
294
expect ( result . test ) . to . be . a ( 'number' ) ;
255
295
} ) ;
296
+
297
+ it ( 'should work for function-valued and array-valued replacer parameters' , function ( ) {
298
+ const doc = { a : new Int32 ( 10 ) , b : new Int32 ( 10 ) } ;
299
+
300
+ var replacerArray = [ 'a' , '$numberInt' ] ;
301
+ var serialized = EJSON . stringify ( doc , replacerArray , 0 , { relaxed : false } ) ;
302
+ expect ( serialized ) . to . equal ( '{"a":{"$numberInt":"10"}}' ) ;
303
+
304
+ serialized = EJSON . stringify ( doc , replacerArray ) ;
305
+ expect ( serialized ) . to . equal ( '{"a":10}' ) ;
306
+
307
+ var replacerFunc = function ( key , value ) { return key === 'b' ? undefined : value ; }
308
+ serialized = EJSON . stringify ( doc , replacerFunc , 0 , { relaxed : false } ) ;
309
+ expect ( serialized ) . to . equal ( '{"a":{"$numberInt":"10"}}' ) ;
310
+
311
+ serialized = EJSON . stringify ( doc , replacerFunc ) ;
312
+ expect ( serialized ) . to . equal ( '{"a":10}' ) ;
313
+ } ) ;
256
314
} ) ;
0 commit comments