2
2
3
3
const assert = require ( 'assert' ) ;
4
4
const expect = require ( 'chai' ) . expect ;
5
- const extJSON = require ( '..' ) ;
5
+ const EJSON = require ( '..' ) ;
6
6
const BSON = require ( 'bson' ) ;
7
7
8
8
// BSON types
@@ -57,12 +57,12 @@ describe('Extended JSON', function() {
57
57
var json =
58
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"}}' ;
59
59
60
- assert . equal ( json , extJSON . stringify ( doc , null , 0 ) ) ;
60
+ assert . equal ( json , EJSON . stringify ( doc , null , 0 ) ) ;
61
61
} ) ;
62
62
63
63
it ( 'should correctly deserialize using strict, and non-strict mode' , function ( ) {
64
64
// Deserialize the document using non strict mode
65
- var doc1 = extJSON . parse ( extJSON . stringify ( doc , null , 0 ) , { strict : false } ) ;
65
+ var doc1 = EJSON . parse ( EJSON . stringify ( doc , null , 0 ) , { strict : false } ) ;
66
66
67
67
// Validate the values
68
68
assert . equal ( 300 , doc1 . int32Number ) ;
@@ -71,7 +71,7 @@ describe('Extended JSON', function() {
71
71
assert . equal ( 19007199250000000.12 , doc1 . doubleNumberIntFit ) ;
72
72
73
73
// Deserialize the document using strict mode
74
- doc1 = extJSON . parse ( extJSON . stringify ( doc , null , 0 ) , { strict : true } ) ;
74
+ doc1 = EJSON . parse ( EJSON . stringify ( doc , null , 0 ) , { strict : true } ) ;
75
75
76
76
// Validate the values
77
77
expect ( doc1 . int32Number . _bsontype ) . to . equal ( 'Int32' ) ;
@@ -82,44 +82,44 @@ describe('Extended JSON', function() {
82
82
83
83
it ( 'should correctly serialize, and deserialize using built-in BSON' , function ( ) {
84
84
// Create ExtJSON instance
85
- var Int32 = extJSON . BSON . Int32 ;
85
+ var Int32 = EJSON . BSON . Int32 ;
86
86
// Create a doc
87
87
var doc1 = {
88
88
int32 : new Int32 ( 10 )
89
89
} ;
90
90
91
91
// Serialize the document
92
- var text = extJSON . stringify ( doc1 , null , 0 ) ;
92
+ var text = EJSON . stringify ( doc1 , null , 0 ) ;
93
93
expect ( text ) . to . equal ( '{"int32":{"$numberInt":"10"}}' ) ;
94
94
95
95
// Deserialize the json in strict and non strict mode
96
- var doc2 = extJSON . parse ( text , { strict : true } ) ;
96
+ var doc2 = EJSON . parse ( text , { strict : true } ) ;
97
97
expect ( doc2 . int32 . _bsontype ) . to . equal ( 'Int32' ) ;
98
- doc2 = extJSON . parse ( text , { strict : false } ) ;
98
+ doc2 = EJSON . parse ( text , { strict : false } ) ;
99
99
expect ( doc2 . int32 ) . to . equal ( 10 ) ;
100
100
} ) ;
101
101
102
102
it ( 'should correctly serialize bson types when they are values' , function ( ) {
103
- var Int32 = extJSON . BSON . Int32 ;
104
- var serialized = extJSON . stringify ( new ObjectID ( '591801a468f9e7024b6235ea' ) ) ;
103
+ var Int32 = EJSON . BSON . Int32 ;
104
+ var serialized = EJSON . stringify ( new ObjectID ( '591801a468f9e7024b6235ea' ) ) ;
105
105
expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
106
- serialized = extJSON . stringify ( new Int32 ( 42 ) ) ;
106
+ serialized = EJSON . stringify ( new Int32 ( 42 ) ) ;
107
107
expect ( serialized ) . to . equal ( '{"$numberInt":"42"}' ) ;
108
- serialized = extJSON . stringify ( {
108
+ serialized = EJSON . stringify ( {
109
109
_id : { $nin : [ new ObjectID ( '591801a468f9e7024b6235ea' ) ] }
110
110
} ) ;
111
111
expect ( serialized ) . to . equal ( '{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}' ) ;
112
112
113
- serialized = extJSON . stringify ( new Binary ( new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] ) ) ) ;
113
+ serialized = EJSON . stringify ( new Binary ( new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] ) ) ) ;
114
114
expect ( serialized ) . to . equal ( '{"$binary":{"base64":"AQIDBAU=","subType":"00"}}' ) ;
115
115
} ) ;
116
116
117
117
it ( 'should correctly parse null values' , function ( ) {
118
- expect ( extJSON . parse ( 'null' ) ) . to . be . null ;
119
- expect ( extJSON . parse ( '[null]' ) [ 0 ] ) . to . be . null ;
118
+ expect ( EJSON . parse ( 'null' ) ) . to . be . null ;
119
+ expect ( EJSON . parse ( '[null]' ) [ 0 ] ) . to . be . null ;
120
120
121
121
var input = '{"result":[{"_id":{"$oid":"591801a468f9e7024b623939"},"emptyField":null}]}' ;
122
- var parsed = extJSON . parse ( input , { strict : false } ) ;
122
+ var parsed = EJSON . parse ( input , { strict : false } ) ;
123
123
124
124
expect ( parsed ) . to . deep . equal ( {
125
125
result : [ { _id : new ObjectID ( '591801a468f9e7024b623939' ) , emptyField : null } ]
@@ -128,7 +128,7 @@ describe('Extended JSON', function() {
128
128
129
129
it ( 'should correctly throw when passed a non-string to parse' , function ( ) {
130
130
expect ( ( ) => {
131
- extJSON . parse ( { } , { strict : true } ) ;
131
+ EJSON . parse ( { } , { strict : true } ) ;
132
132
} ) . to . throw ;
133
133
} ) ;
134
134
@@ -141,7 +141,7 @@ describe('Extended JSON', function() {
141
141
date : { $date : { $numberLong : '1452124800000' } }
142
142
} ;
143
143
144
- const parsed = extJSON . parse ( JSON . stringify ( inputObject ) , { relaxed : true } ) ;
144
+ const parsed = EJSON . parse ( JSON . stringify ( inputObject ) , { relaxed : true } ) ;
145
145
expect ( parsed ) . to . eql ( {
146
146
int : 500 ,
147
147
long : 42 ,
@@ -151,8 +151,8 @@ describe('Extended JSON', function() {
151
151
} ) ;
152
152
153
153
it ( 'should allow regexp' , function ( ) {
154
- const parsedRegExp = extJSON . stringify ( { test : / s o m e - r e g e x / i } , { relaxed : true } ) ;
155
- const parsedBSONRegExp = extJSON . stringify (
154
+ const parsedRegExp = EJSON . stringify ( { test : / s o m e - r e g e x / i } , { relaxed : true } ) ;
155
+ const parsedBSONRegExp = EJSON . stringify (
156
156
{ test : new BSONRegExp ( 'some-regex' , 'i' ) } ,
157
157
{ relaxed : true }
158
158
) ;
@@ -176,8 +176,7 @@ describe('Extended JSON', function() {
176
176
timestamp : new Timestamp ( )
177
177
} ;
178
178
179
- const result = extJSON . serialize ( doc ) ;
180
-
179
+ const result = EJSON . serialize ( doc ) ;
181
180
expect ( result ) . to . deep . equal ( {
182
181
binary : { $binary : { base64 : '' , subType : '00' } } ,
183
182
code : { $code : 'function() {}' } ,
@@ -212,7 +211,7 @@ describe('Extended JSON', function() {
212
211
timestamp : { $timestamp : { t : 0 , i : 0 } }
213
212
} ;
214
213
215
- const result = extJSON . deserialize ( doc ) ;
214
+ const result = EJSON . deserialize ( doc ) ;
216
215
217
216
// binary
218
217
expect ( result . binary ) . to . be . an . instanceOf ( BSON . Binary ) ;
0 commit comments