Skip to content

Commit bfd36ea

Browse files
committed
Update tests
1 parent 89a69fa commit bfd36ea

File tree

1 file changed

+69
-55
lines changed

1 file changed

+69
-55
lines changed

test/index.js

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
var Flatted = require('../cjs');
1+
var TJS = require('../cjs');
22

33
var a = [];
44
var o = {};
55

6-
console.assert(Flatted.stringify(a) === '[[]]', 'empty Array');
7-
console.assert(Flatted.stringify(o) === '[{}]', 'empty Object');
6+
console.assert(TJS.stringify(a) === '[[]]', 'empty Array');
7+
console.assert(TJS.stringify(o) === '[{}]', 'empty Object');
88

99
a.push(a);
1010
o.o = o;
1111

12-
console.assert(Flatted.stringify(a) === '[["0"]]', 'recursive Array');
13-
console.assert(Flatted.stringify(o) === '[{"o":"0"}]', 'recursive Object');
12+
console.assert(TJS.stringify(a) === '[["0"]]', 'recursive Array');
13+
console.assert(TJS.stringify(o) === '[{"o":"0"}]', 'recursive Object');
1414

15-
var b = Flatted.parse(Flatted.stringify(a));
15+
var b = TJS.parse(TJS.stringify(a));
1616
console.assert(Array.isArray(b) && b[0] === b, 'restoring recursive Array');
1717

1818
a.push(1, 'two', true);
1919
o.one = 1;
2020
o.two = 'two';
2121
o.three = true;
2222

23-
console.assert(Flatted.stringify(a) === '[["0",1,"1",true],"two"]', 'values in Array');
24-
console.assert(Flatted.stringify(o) === '[{"o":"0","one":1,"two":"1","three":true},"two"]', 'values in Object');
23+
console.assert(TJS.stringify(a) === '[["0",1,"1",true],"two"]', 'values in Array');
24+
console.assert(TJS.stringify(o) === '[{"o":"0","one":1,"two":"1","three":true},"two"]', 'values in Object');
2525

2626

2727
a.push(o);
2828
o.a = a;
2929

30-
console.assert(Flatted.stringify(a) === '[["0",1,"1",true,"2"],"two",{"o":"2","one":1,"two":"1","three":true,"a":"0"}]', 'object in Array');
31-
console.assert(Flatted.stringify(o) === '[{"o":"0","one":1,"two":"1","three":true,"a":"2"},"two",["2",1,"1",true,"0"]]', 'array in Object');
30+
console.assert(TJS.stringify(a) === '[["0",1,"1",true,"2"],"two",{"o":"2","one":1,"two":"1","three":true,"a":"0"}]', 'object in Array');
31+
console.assert(TJS.stringify(o) === '[{"o":"0","one":1,"two":"1","three":true,"a":"2"},"two",["2",1,"1",true,"0"]]', 'array in Object');
3232

3333
a.push({test: 'OK'}, [1, 2, 3]);
3434
o.test = {test: 'OK'};
3535
o.array = [1, 2, 3];
3636

37-
console.assert(Flatted.stringify(a) === '[["0",1,"1",true,"2","3","4"],"two",{"o":"2","one":1,"two":"1","three":true,"a":"0","test":"5","array":"6"},{"test":"7"},[1,2,3],{"test":"7"},[1,2,3],"OK"]', 'objects in Array');
38-
console.assert(Flatted.stringify(o) === '[{"o":"0","one":1,"two":"1","three":true,"a":"2","test":"3","array":"4"},"two",["2",1,"1",true,"0","5","6"],{"test":"7"},[1,2,3],{"test":"7"},[1,2,3],"OK"]', 'objects in Object');
37+
console.assert(TJS.stringify(a) === '[["0",1,"1",true,"2","3","4"],"two",{"o":"2","one":1,"two":"1","three":true,"a":"0","test":"5","array":"6"},{"test":"7"},[1,2,3],{"test":"7"},[1,2,3],"OK"]', 'objects in Array');
38+
console.assert(TJS.stringify(o) === '[{"o":"0","one":1,"two":"1","three":true,"a":"2","test":"3","array":"4"},"two",["2",1,"1",true,"0","5","6"],{"test":"7"},[1,2,3],{"test":"7"},[1,2,3],"OK"]', 'objects in Object');
3939

40-
a = Flatted.parse(Flatted.stringify(a));
41-
o = Flatted.parse(Flatted.stringify(o));
40+
a = TJS.parse(TJS.stringify(a));
41+
o = TJS.parse(TJS.stringify(o));
4242

4343
console.assert(a[0] === a, 'parsed Array');
4444
console.assert(o.o === o, 'parsed Object');
@@ -67,25 +67,25 @@ console.assert(
6767

6868
console.assert(o.a === o.a[0] && o === o.a[4], 'object recursive values are OK');
6969

70-
console.assert(Flatted.parse(Flatted.stringify(1)) === 1, 'numbers can be parsed too');
71-
console.assert(Flatted.parse(Flatted.stringify(false)) === false, 'booleans can be parsed too');
72-
console.assert(Flatted.parse(Flatted.stringify(null)) === null, 'null can be parsed too');
73-
console.assert(Flatted.parse(Flatted.stringify('test')) === 'test', 'strings can be parsed too');
70+
console.assert(TJS.parse(TJS.stringify(1)) === 1, 'numbers can be parsed too');
71+
console.assert(TJS.parse(TJS.stringify(false)) === false, 'booleans can be parsed too');
72+
console.assert(TJS.parse(TJS.stringify(null)) === null, 'null can be parsed too');
73+
console.assert(TJS.parse(TJS.stringify('test')) === 'test', 'strings can be parsed too');
7474

7575
var d = new Date;
76-
console.assert(Flatted.parse(Flatted.stringify(d)) === d.toISOString(), 'dates can be parsed too');
76+
console.assert(TJS.parse(TJS.stringify(d)) === d.toISOString(), 'dates can be parsed too');
7777

78-
console.assert(Flatted.parse(
79-
Flatted.stringify(d),
78+
console.assert(TJS.parse(
79+
TJS.stringify(d),
8080
function (key, value) {
8181
if (typeof value === 'string' && /^[0-9:.ZT-]+$/.test(value))
8282
return new Date(value);
8383
return value;
8484
}
8585
) instanceof Date, 'dates can be revived too');
8686

87-
console.assert(Flatted.parse(
88-
Flatted.stringify({
87+
console.assert(TJS.parse(
88+
TJS.stringify({
8989
sub: {
9090
one23: 123,
9191
date: d
@@ -104,31 +104,31 @@ console.assert(Flatted.parse(
104104

105105
(function () {
106106
var special = "\\x7e"; // \x7e is ~
107-
//console.log(Flatted.stringify({a:special}));
108-
//console.log(Flatted.parse(Flatted.stringify({a:special})).a);
109-
console.assert(Flatted.parse(Flatted.stringify({a:special})).a === special, 'no problem with simulation');
107+
//console.log(TJS.stringify({a:special}));
108+
//console.log(TJS.parse(TJS.stringify({a:special})).a);
109+
console.assert(TJS.parse(TJS.stringify({a:special})).a === special, 'no problem with simulation');
110110
special = "~\\x7e";
111-
console.assert(Flatted.parse(Flatted.stringify({a:special})).a === special, 'no problem with special char');
111+
console.assert(TJS.parse(TJS.stringify({a:special})).a === special, 'no problem with special char');
112112
}());
113113

114114
(function () {
115115
var o = {a: 'a', b: 'b', c: function(){}, d: {e: 123}},
116116
a = JSON.stringify(o),
117-
b = Flatted.stringify(o);
117+
b = TJS.stringify(o);
118118

119119
console.assert(
120-
JSON.stringify(JSON.parse(a)) === JSON.stringify(Flatted.parse(b)),
120+
JSON.stringify(JSON.parse(a)) === JSON.stringify(TJS.parse(b)),
121121
'works as JSON.parse'
122122
);
123123
console.assert(
124-
Flatted.stringify(o, function(key, value){
124+
TJS.stringify(o, function(key, value){
125125
if (!key || key === 'a') return value;
126-
}) === '[{"a":"1"},"a"]',
126+
}) === '[{"a":"1","b":"_0","c":"_0","d":"_0"},"a",["u"]]',
127127
'accept callback'
128128
);
129129
console.assert(
130130
JSON.stringify(
131-
Flatted.parse('[{"a":"1"},"a"]', function(key, value){
131+
TJS.parse('[{"a":"1"},"a"]', function(key, value){
132132
if (key === 'a') return 'b';
133133
return value;
134134
})
@@ -148,8 +148,8 @@ console.assert(Flatted.parse(
148148
o.c.e = o;
149149
o.c.f = o.d;
150150
o.b = o.c;
151-
before = Flatted.stringify(o);
152-
o = Flatted.parse(before);
151+
before = TJS.stringify(o);
152+
o = TJS.parse(before);
153153
console.assert(
154154
o.b === o.c &&
155155
o.c.e === o &&
@@ -166,12 +166,12 @@ console.assert(Flatted.parse(
166166
o.a = o;
167167
o.b = o;
168168
console.assert(
169-
Flatted.stringify(o, function (key, value) {
169+
TJS.stringify(o, function (key, value) {
170170
if (!key || key === 'a') return value;
171-
}) === '[{"a":"0"}]',
171+
}) === '[{"a":"0","b":"_0"},["u"]]',
172172
'callback invoked'
173173
);
174-
o = Flatted.parse('[{"a":"0"}]', function (key, value) {
174+
o = TJS.parse('[{"a":"0"}]', function (key, value) {
175175
if (!key) {
176176
value.b = value;
177177
}
@@ -189,7 +189,7 @@ console.assert(Flatted.parse(
189189
o['\\x7e'] = '\\x7e';
190190
o.test = '~';
191191

192-
o = Flatted.parse(Flatted.stringify(o));
192+
o = TJS.parse(TJS.stringify(o));
193193
console.assert(o['~'] === o && o.test === '~', 'still intact');
194194
o = {
195195
a: [
@@ -201,7 +201,7 @@ console.assert(Flatted.parse(
201201
o['~'] = o.a;
202202
o['~~'] = o.a;
203203
o['~~~'] = o.a;
204-
o = Flatted.parse(Flatted.stringify(o));
204+
o = TJS.parse(TJS.stringify(o));
205205
console.assert(
206206
o === o.a[3] &&
207207
o === o.o &&
@@ -240,8 +240,8 @@ console.assert(Flatted.parse(
240240
output
241241
;
242242
item.value = item;
243-
str = Flatted.stringify(original);
244-
output = Flatted.parse(str);
243+
str = TJS.stringify(original);
244+
output = TJS.parse(str);
245245
console.assert(str === '[{"outer":"1"},["2"],{"a":"3","c":"4","one":"5","many":"6","e":"7"},"b","d",{"name":"8","value":"5"},["5"],"f","TEST"]', 'string is correct');
246246
console.assert(
247247
original.outer[0].one.name === output.outer[0].one.name &&
@@ -288,19 +288,19 @@ console.assert(Flatted.parse(
288288
p: 4
289289
}
290290
},
291-
str = Flatted.stringify(nested),
291+
str = TJS.stringify(nested),
292292
output
293293
;
294294
console.assert(str === '[{"prop":"1","a":"2","b":"3"},{"value":123},["4","5"],{"e":"6","t":"7","p":4},{},{"b":"8"},"f",{"a":"9"},["10"],"sup",{"a":1,"d":2,"c":"7","z":"11","h":1},{"g":2,"a":"7","b":"12","f":6},{"r":4,"u":"7","c":5}]', 'string is OK');
295-
output = Flatted.parse(str);
295+
output = TJS.parse(str);
296296
console.assert(output.b.t.a === 'sup' && output.a[1].b[0].c === output.b.t, 'so is the object');
297297
}());
298298

299299
(function () {
300300
var o = {bar: 'something ~ baz'};
301-
var s = Flatted.stringify(o);
301+
var s = TJS.stringify(o);
302302
console.assert(s === '[{"bar":"1"},"something ~ baz"]', 'string is correct');
303-
var oo = Flatted.parse(s);
303+
var oo = TJS.parse(s);
304304
console.assert(oo.bar === o.bar, 'parse is correct');
305305
}());
306306

@@ -325,9 +325,9 @@ console.assert(Flatted.parse(
325325
o.c.cc.cca = o.c;
326326
o.c.cd.cda = o.c.ca.caa;
327327

328-
var s = Flatted.stringify(o);
328+
var s = TJS.stringify(o);
329329
console.assert(s === '[{"a":"1","b":"0","c":"2"},{"aa":"3"},{"ca":"4","cb":"5","cc":"6","cd":"7","ce":"8","cf":"9"},{"aaa":"10"},{"caa":"4"},{"cba":"5"},{"cca":"2"},{"cda":"4"},"value2","value3","value1"]', 'string is correct');
330-
var oo = Flatted.parse(s);
330+
var oo = TJS.parse(s);
331331
console.assert(
332332
oo.a.aa.aaa = 'value1'
333333
&& oo === oo.b
@@ -357,8 +357,8 @@ console.assert(Flatted.parse(
357357
original.a1.a2[0] = original.a1;
358358
original.a4[0] = original.a1.a3[0];
359359

360-
json = Flatted.stringify(original);
361-
restored = Flatted.parse(json);
360+
json = TJS.stringify(original);
361+
restored = TJS.parse(json);
362362

363363
console.assert(restored.a1.a2[0] === restored.a1, '~a1~a2~0 === ~a1');
364364
console.assert(restored.a4[0] = restored.a1.a3[0], '~a4 === ~a1~a3~0');
@@ -369,30 +369,30 @@ if (typeof Symbol !== 'undefined') {
369369
var o = {a: 1};
370370
var a = [1, Symbol('test'), 2];
371371
o[Symbol('test')] = 123;
372-
console.assert(('[' + JSON.stringify(o) + ']') === Flatted.stringify(o), 'Symbol is OK too');
373-
console.assert(('[' + JSON.stringify(a) + ']') === Flatted.stringify(a), 'non symbol is OK too');
372+
console.assert(('[' + JSON.stringify(o) + ']') === TJS.stringify(o), 'Symbol is OK too');
373+
console.assert(('[[1,"_0",2],["s;test"]]') === TJS.stringify(a), 'non symbol is OK too');
374374
}());
375375
}
376376

377377
(function () {
378378
var args = [{a:[1]}, null, ' '];
379-
console.assert(Flatted.stringify.apply(null, args) === "[{\n \"a\": \"1\"\n},[\n 1\n]]", 'extra args same as JSON');
379+
console.assert(TJS.stringify.apply(null, args) === "[{\n \"a\": \"1\"\n},[\n 1\n]]", 'extra args same as JSON');
380380
}());
381381

382382
(function () {
383383
var o = {a: 1, b: {a: 1, b: 2}};
384384
var json = JSON.stringify(o, ['b']);
385385
console.assert(
386-
Flatted.stringify(o, ['b']) === '[{"b":"1"},{"b":2}]',
386+
TJS.stringify(o, ['b']) === '[{"a":"_0","b":"1"},{"a":"_0","b":2},["u"]]',
387387
'whitelisted ["b"]: '+ json
388388
);
389389
}());
390390

391391
(function () {
392392
var a = { b: { '': { c: { d: 1 } } } };
393393
a._circular = a.b[''];
394-
var json = Flatted.stringify(a);
395-
var nosj = Flatted.parse(json);
394+
var json = TJS.stringify(a);
395+
var nosj = TJS.parse(json);
396396
console.assert(
397397
nosj._circular === nosj.b[''] &&
398398
JSON.stringify(nosj._circular) === JSON.stringify(a._circular),
@@ -405,3 +405,17 @@ if (typeof Symbol !== 'undefined') {
405405
'objects copied with circular empty keys are the same'
406406
);
407407
}());
408+
409+
(function () {
410+
isSetsEqual = (a, b) => a.size === b.size && [...a].every(value => b.has(value));
411+
isMapsEqual = (a, b) => a.size === b.size && [...a].every(([key]) => { return a.get(key) === b.get(key); });
412+
413+
console.assert(TJS.parse(TJS.stringify({x: undefined, y: undefined})).hasOwnProperty('x'), 'retains undefined keys');
414+
console.assert(TJS.parse(TJS.stringify(undefined)) === undefined, 'supports undefined');
415+
console.assert(TJS.parse(TJS.stringify(Infinity)) === Infinity, 'supports non-finite numbers');
416+
console.assert(TJS.parse(TJS.stringify(900719925474099123n)) === 900719925474099123n, 'supports bigint');
417+
console.assert(TJS.parse(TJS.stringify(/^a-z$/gi)).toString() === '/^a-z$/gi', 'supports regex');
418+
console.assert(TJS.parse(TJS.stringify(Symbol('tjs'))) === Symbol.for('tjs'), 'supports symbol');
419+
console.assert(isSetsEqual(TJS.parse(TJS.stringify(new Set([1, 2, 3]))), new Set([1, 2, 3])), 'supports set');
420+
console.assert(isSetsEqual(TJS.parse(TJS.stringify(new Map([[1, '1'], [2, '2']]))), new Map([[1, '1'], [2, '2']])), 'supports map');
421+
}());

0 commit comments

Comments
 (0)