-
Notifications
You must be signed in to change notification settings - Fork 15
/
test.js
1346 lines (1130 loc) · 36 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const { test } = require('tap')
const { stringify } = require('./index.js')
const clone = require('clone')
test('toJSON receives array keys as string', function (assert) {
const object = [{
toJSON (key) {
assert.equal(key, '0')
return 42
}
}]
const expected = JSON.stringify(object)
let actual = stringify(object)
assert.equal(actual, expected)
actual = stringify(object, ['0'])
assert.equal(actual, expected)
// @ts-expect-error
actual = stringify(object, (key, value) => value)
assert.equal(actual, expected)
actual = stringify(object, null, 2)
assert.equal(actual, '[\n 42\n]')
assert.end()
})
test('circular reference to root', function (assert) {
const fixture = { name: 'Tywin Lannister' }
fixture.circle = fixture
const expected = JSON.stringify(
{ circle: '[Circular]', name: 'Tywin Lannister' }
)
const actual = stringify(fixture)
assert.equal(actual, expected)
assert.end()
})
test('nested circular reference to root', function (assert) {
const fixture = { name: 'Tywin\n\t"Lannister' }
fixture.id = { circle: fixture }
const expected = JSON.stringify(
{ id: { circle: '[Circular]' }, name: 'Tywin\n\t"Lannister' }
)
const actual = stringify(fixture)
assert.equal(actual, expected)
assert.end()
})
test('throw if circularValue is set to TypeError', function (assert) {
const noCircularStringify = stringify.configure({ circularValue: TypeError })
const object = { number: 42, boolean: true, string: 'Yes!' }
object.circular = object
assert.throws(() => noCircularStringify(object), TypeError)
assert.end()
})
test('throw if circularValue is set to Error', function (assert) {
const noCircularStringify = stringify.configure({ circularValue: Error })
const object = { number: 42, boolean: true, string: 'Yes!' }
object.circular = object
assert.throws(() => noCircularStringify(object), TypeError)
assert.end()
})
test('child circular reference', function (assert) {
const fixture = { name: 'Tywin Lannister', child: { name: 'Tyrion\n\t"Lannister'.repeat(20) } }
fixture.child.dinklage = fixture.child
const expected = JSON.stringify({
child: {
dinklage: '[Circular]', name: 'Tyrion\n\t"Lannister'.repeat(20)
},
name: 'Tywin Lannister'
})
const actual = stringify(fixture)
assert.equal(actual, expected)
assert.end()
})
test('nested child circular reference', function (assert) {
const fixture = { name: 'Tywin Lannister', child: { name: 'Tyrion Lannister' } }
fixture.child.actor = { dinklage: fixture.child }
const expected = JSON.stringify({
child: {
actor: { dinklage: '[Circular]' }, name: 'Tyrion Lannister'
},
name: 'Tywin Lannister'
})
const actual = stringify(fixture)
assert.equal(actual, expected)
assert.end()
})
test('circular objects in an array', function (assert) {
const fixture = { name: 'Tywin Lannister' }
fixture.hand = [fixture, fixture]
const expected = JSON.stringify({
hand: ['[Circular]', '[Circular]'], name: 'Tywin Lannister'
})
const actual = stringify(fixture)
assert.equal(actual, expected)
assert.end()
})
test('nested circular references in an array', function (assert) {
const fixture = {
name: 'Tywin Lannister',
offspring: [{ name: 'Tyrion Lannister' }, { name: 'Cersei Lannister' }]
}
fixture.offspring[0].dinklage = fixture.offspring[0]
fixture.offspring[1].headey = fixture.offspring[1]
const expected = JSON.stringify({
name: 'Tywin Lannister',
offspring: [
{ dinklage: '[Circular]', name: 'Tyrion Lannister' },
{ headey: '[Circular]', name: 'Cersei Lannister' }
]
})
const actual = stringify(fixture)
assert.equal(actual, expected)
assert.end()
})
test('circular arrays', function (assert) {
const fixture = []
fixture.push(fixture, fixture)
const expected = JSON.stringify(['[Circular]', '[Circular]'])
const actual = stringify(fixture)
assert.equal(actual, expected)
assert.end()
})
test('nested circular arrays', function (assert) {
const fixture = []
fixture.push(
{ name: 'Jon Snow', circular: fixture },
{ name: 'Ramsay Bolton', circular: fixture }
)
const expected = JSON.stringify([
{ circular: '[Circular]', name: 'Jon Snow' },
{ circular: '[Circular]', name: 'Ramsay Bolton' }
])
const actual = stringify(fixture)
assert.equal(actual, expected)
assert.end()
})
test('repeated non-circular references in objects', function (assert) {
const daenerys = { name: 'Daenerys Targaryen' }
const fixture = {
motherOfDragons: daenerys,
queenOfMeereen: daenerys
}
const expected = JSON.stringify(fixture)
const actual = stringify(fixture)
assert.equal(actual, expected)
assert.end()
})
test('repeated non-circular references in arrays', function (assert) {
const daenerys = { name: 'Daenerys Targaryen' }
const fixture = [daenerys, daenerys]
const expected = JSON.stringify(fixture)
const actual = stringify(fixture)
assert.equal(actual, expected)
assert.end()
})
test('double child circular reference', function (assert) {
// create circular reference
const child = { name: 'Tyrion Lannister' }
child.dinklage = child
// include it twice in the fixture
const fixture = { name: 'Tywin Lannister', childA: child, childB: child }
const cloned = clone(fixture)
const expected = JSON.stringify({
childA: {
dinklage: '[Circular]', name: 'Tyrion Lannister'
},
childB: {
dinklage: '[Circular]', name: 'Tyrion Lannister'
},
name: 'Tywin Lannister'
})
const actual = stringify(fixture)
assert.equal(actual, expected)
// check if the fixture has not been modified
assert.same(fixture, cloned)
assert.end()
})
test('child circular reference with toJSON', function (assert) {
// Create a test object that has an overridden `toJSON` property
TestObject.prototype.toJSON = function () { return { special: 'case' } }
function TestObject () {}
// Creating a simple circular object structure
const parentObject = {}
parentObject.childObject = new TestObject()
// @ts-expect-error
parentObject.childObject.parentObject = parentObject
// Creating a simple circular object structure
const otherParentObject = new TestObject()
// @ts-expect-error
otherParentObject.otherChildObject = {}
// @ts-expect-error
otherParentObject.otherChildObject.otherParentObject = otherParentObject
// Making sure our original tests work
// @ts-expect-error
assert.same(parentObject.childObject.parentObject, parentObject)
// @ts-expect-error
assert.same(otherParentObject.otherChildObject.otherParentObject, otherParentObject)
// Should both be idempotent
assert.equal(stringify(parentObject), '{"childObject":{"special":"case"}}')
assert.equal(stringify(otherParentObject), '{"special":"case"}')
// Therefore the following assertion should be `true`
// @ts-expect-error
assert.same(parentObject.childObject.parentObject, parentObject)
// @ts-expect-error
assert.same(otherParentObject.otherChildObject.otherParentObject, otherParentObject)
assert.end()
})
test('null object', function (assert) {
const expected = JSON.stringify(null)
const actual = stringify(null)
assert.equal(actual, expected)
assert.end()
})
test('null property', function (assert) {
const obj = { f: null }
const expected = JSON.stringify(obj)
const actual = stringify(obj)
assert.equal(actual, expected)
assert.end()
})
test('null property', function (assert) {
const obj = { toJSON () { return null } }
const expected = JSON.stringify(obj)
const actual = stringify(obj)
assert.equal(actual, expected)
assert.end()
})
test('nested child circular reference in toJSON', function (assert) {
const circle = { some: 'data' }
circle.circle = circle
const a = {
b: {
toJSON: function () {
// @ts-expect-error
a.b = 2
return '[Redacted]'
}
},
baz: {
circle,
toJSON: function () {
// @ts-expect-error
a.baz = circle
return '[Redacted]'
}
}
}
const o = {
a,
bar: a
}
const expected = JSON.stringify({
a: {
b: '[Redacted]',
baz: '[Redacted]'
},
bar: {
b: 2,
baz: {
circle: '[Circular]',
some: 'data'
}
}
})
const actual = stringify(o)
assert.equal(actual, expected)
assert.end()
})
test('invalid replacer being ignored', function (assert) {
const obj = { a: true }
// @ts-expect-error
const actual = stringify(obj, 'invalidReplacer')
// @ts-expect-error
const expected = stringify(obj, 'invalidReplacer')
assert.equal(actual, expected)
assert.end()
})
test('replacer removing elements', function (assert) {
const replacer = function (k, v) {
assert.type(k, 'string')
if (k === 'remove') return
if (k === '0') typedkeysInReplacer = true
return v
}
const obj = { f: null, remove: true, typed: new Int32Array(1) }
let typedkeysInReplacer = false
const expected = JSON.stringify(obj, replacer)
assert.ok(typedkeysInReplacer)
typedkeysInReplacer = false
let actual = stringify(obj, replacer)
assert.ok(typedkeysInReplacer)
typedkeysInReplacer = false
assert.equal(actual, expected)
obj.obj = obj
actual = stringify(obj, replacer)
assert.equal(actual, '{"f":null,"obj":"[Circular]","typed":{"0":0}}')
assert.end()
})
test('replacer removing elements and indentation', function (assert) {
const replacer = function (k, v) {
if (k === 'remove') return
return v
}
const obj = { f: null, remove: true }
const expected = JSON.stringify(obj, replacer, 2)
const actual = stringify(obj, replacer, 2)
assert.equal(actual, expected)
assert.end()
})
test('replacer removing all elements', function (assert) {
const replacer = function (k, v) {
assert.type(k, 'string')
if (k !== '') return
return k
}
const obj = [{ f: null, remove: true }]
let expected = JSON.stringify(obj, replacer)
let actual = stringify(obj, replacer)
assert.equal(actual, expected)
expected = JSON.stringify({ toJSON () { return obj } }, replacer)
actual = stringify({ toJSON () { return obj } }, replacer)
assert.equal(actual, expected)
assert.end()
})
test('replacer removing all elements and indentation', function (assert) {
const replacer = function (k, v) {
if (k !== '') return
return k
}
const obj = [{ f: null, remove: true }]
const expected = JSON.stringify(obj, replacer, 2)
const actual = stringify(obj, replacer, 2)
assert.equal(actual, expected)
assert.end()
})
test('array replacer', function (assert) {
const replacer = ['f', 1, null]
const obj = { f: null, null: true, 1: false }
// The null element will be ignored!
// @ts-expect-error
const expected = JSON.stringify(obj, replacer)
// @ts-expect-error
let actual = stringify(obj, replacer)
assert.equal(actual, expected)
// @ts-expect-error
obj.f = obj
// @ts-expect-error
actual = stringify({ toJSON () { return obj } }, replacer)
assert.equal(actual, expected.replace('null', '"[Circular]"'))
assert.end()
})
test('empty array replacer', function (assert) {
const replacer = []
const obj = { f: null, null: true, 1: false }
// The null element will be removed!
const expected = JSON.stringify(obj, replacer)
const actual = stringify(obj, replacer)
assert.equal(actual, expected)
assert.end()
})
test('array replacer and indentation', function (assert) {
const replacer = ['f', 1, null]
const obj = { f: null, null: true, 1: [false, -Infinity, 't'] }
// The null element will be removed!
// @ts-expect-error
const expected = JSON.stringify(obj, replacer, 2)
// @ts-expect-error
const actual = stringify(obj, replacer, 2)
assert.equal(actual, expected)
assert.end()
})
test('indent zero', function (assert) {
const obj = { f: null, null: true, 1: false }
const expected = JSON.stringify(obj, null, 0)
const actual = stringify(obj, null, 0)
assert.equal(actual, expected)
assert.end()
})
test('replacer and indentation without match', function (assert) {
const replacer = function (k, v) {
if (k === '') return v
}
const obj = { f: 1, b: null, c: 't', d: Infinity, e: true }
const expected = JSON.stringify(obj, replacer, ' ')
const actual = stringify(obj, replacer, ' ')
assert.equal(actual, expected)
assert.end()
})
test('array replacer and indentation without match', function (assert) {
const replacer = ['']
const obj = { f: 1, b: null, c: 't', d: Infinity, e: true }
const expected = JSON.stringify(obj, replacer, ' ')
const actual = stringify(obj, replacer, ' ')
assert.equal(actual, expected)
assert.end()
})
test('indentation without match', function (assert) {
const obj = { f: undefined }
const expected = JSON.stringify(obj, undefined, 3)
const actual = stringify(obj, undefined, 3)
assert.equal(actual, expected)
assert.end()
})
test('array nulls and indentation', function (assert) {
const obj = [null, null]
const expected = JSON.stringify(obj, undefined, 3)
const actual = stringify(obj, undefined, 3)
assert.equal(actual, expected)
assert.end()
})
test('array nulls, replacer and indentation', function (assert) {
const obj = [null, Infinity, 5, true, false]
const expected = JSON.stringify(obj, (_, v) => v, 3)
const actual = stringify(obj, (_, v) => v, 3)
assert.equal(actual, expected)
assert.end()
})
test('array nulls and replacer', function (assert) {
const obj = [null, Infinity, 5, true, false, [], {}]
const expected = JSON.stringify(obj, (_, v) => v)
const actual = stringify(obj, (_, v) => v)
assert.equal(actual, expected)
assert.end()
})
test('array nulls, array replacer and indentation', function (assert) {
const obj = [null, null, [], {}]
// @ts-expect-error
const expected = JSON.stringify(obj, [false], 3)
// @ts-expect-error
const actual = stringify(obj, [false], 3)
assert.equal(actual, expected)
assert.end()
})
test('array and array replacer', function (assert) {
const obj = [null, null, 't', Infinity, true, false, [], {}]
const expected = JSON.stringify(obj, [2])
const actual = stringify(obj, [2])
assert.equal(actual, expected)
assert.end()
})
test('indentation with elements', function (assert) {
const obj = { a: 1, b: [null, 't', Infinity, true] }
const expected = JSON.stringify(obj, null, 5)
const actual = stringify(obj, null, 5)
assert.equal(actual, expected)
assert.end()
})
test('object with undefined values', function (assert) {
let obj = { a: 1, c: undefined, b: 'hello', d: [], e: {} }
let expected = JSON.stringify(obj)
let actual = stringify(obj)
assert.equal(actual, expected)
// @ts-expect-error
obj = { b: 'hello', a: undefined, c: 1 }
expected = JSON.stringify(obj)
actual = stringify(obj)
assert.equal(actual, expected)
assert.end()
})
test('undefined values and indented', function (assert) {
const obj1 = { a: 1, c: undefined, b: 'hello' }
let expected = JSON.stringify(obj1, null, 2)
let actual = stringify(obj1, null, 2)
assert.equal(actual, expected)
const obj2 = { b: 'hello', a: undefined, c: 1 }
expected = JSON.stringify(obj2)
actual = stringify(obj2)
assert.equal(actual, expected)
assert.end()
})
test('bigint option', function (assert) {
const stringifyNoBigInt = stringify.configure({ bigint: false })
const stringifyBigInt = stringify.configure({ bigint: true })
const obj = { a: 1n }
const actualBigInt = stringifyBigInt(obj, null, 1)
const actualNoBigInt = stringifyNoBigInt(obj, null, 1)
const actualDefault = stringify(obj, null, 1)
const expectedBigInt = '{\n "a": 1\n}'
const expectedNoBigInt = '{}'
assert.equal(actualNoBigInt, expectedNoBigInt)
assert.throws(() => JSON.stringify(obj, null, 1), TypeError)
assert.equal(actualBigInt, expectedBigInt)
assert.equal(actualDefault, expectedBigInt)
// @ts-expect-error
assert.throws(() => stringify.configure({ bigint: null }), /bigint/)
assert.end()
})
test('bigint option with replacer', function (assert) {
const stringifyBigInt = stringify.configure({ bigint: true })
const obj = { a: new BigUint64Array([1n]), 0: 1n }
const actualArrayReplacer = stringifyBigInt(obj, ['0', 'a'])
const actualFnReplacer = stringifyBigInt(obj, (k, v) => v)
const expected = '{"0":1,"a":{"0":1}}'
assert.equal(actualArrayReplacer, expected)
assert.equal(actualFnReplacer, expected)
assert.end()
})
test('bigint and typed array with indentation', function (assert) {
const obj = { a: 1n, t: new Int8Array(1) }
const expected = '{\n "a": 1,\n "t": {\n "0": 0\n }\n}'
const actual = stringify(obj, null, 1)
assert.equal(actual, expected)
assert.end()
})
test('bigint and typed array without indentation', function (assert) {
const obj = { a: 1n, t: new Int8Array(1) }
const expected = '{"a":1,"t":{"0":0}}'
const actual = stringify(obj, null, 0)
assert.equal(actual, expected)
assert.end()
})
test('no bigint without indentation', function (assert) {
const stringifyNoBigInt = stringify.configure({ bigint: false })
const obj = { a: 1n, t: new Int8Array(1) }
const expected = '{"t":{"0":0}}'
const actual = stringifyNoBigInt(obj, null, 0)
assert.equal(actual, expected)
assert.end()
})
test('circular value option should allow strings and null', function (assert) {
let stringifyCircularValue = stringify.configure({ circularValue: 'YEAH!!!' })
const obj = {}
obj.circular = obj
const expected = '{"circular":"YEAH!!!"}'
const actual = stringifyCircularValue(obj)
assert.equal(actual, expected)
assert.equal(stringify(obj), '{"circular":"[Circular]"}')
stringifyCircularValue = stringify.configure({ circularValue: null })
assert.equal(stringifyCircularValue(obj), '{"circular":null}')
assert.end()
})
test('circular value option should throw for invalid values', function (assert) {
// @ts-expect-error
assert.throws(() => stringify.configure({ circularValue: { objects: 'are not allowed' } }), /circularValue/)
assert.end()
})
test('circular value option set to undefined should skip serialization', function (assert) {
const stringifyCircularValue = stringify.configure({ circularValue: undefined })
const obj = { a: 1 }
obj.circular = obj
obj.b = [2, obj]
const expected = '{"a":1,"b":[2,null]}'
const actual = stringifyCircularValue(obj)
assert.equal(actual, expected)
assert.end()
})
test('non-deterministic', function (assert) {
const stringifyNonDeterministic = stringify.configure({ deterministic: false })
const obj = { b: true, a: false }
const expected = JSON.stringify(obj)
const actual = stringifyNonDeterministic(obj)
assert.equal(actual, expected)
// @ts-expect-error
assert.throws(() => stringify.configure({ deterministic: 1 }), /deterministic/)
assert.end()
})
test('non-deterministic with replacer', function (assert) {
const stringifyNonDeterministic = stringify.configure({ deterministic: false, bigint: false })
const obj = { b: true, a: 5n, c: Infinity, d: 4, e: [Symbol('null'), 5, Symbol('null')] }
const keys = Object.keys(obj)
const expected = stringify(obj, ['b', 'c', 'd', 'e'])
const actualA = stringifyNonDeterministic(obj, keys)
assert.equal(actualA, expected)
const actualB = stringifyNonDeterministic(obj, (k, v) => v)
assert.equal(actualB, expected)
assert.end()
})
test('non-deterministic with indentation', function (assert) {
const stringifyNonDeterministic = stringify.configure({ deterministic: false, bigint: false })
const obj = { b: true, a: 5, c: Infinity, d: false, e: [Symbol('null'), 5, Symbol('null')] }
const expected = JSON.stringify(obj, null, 1)
const actual = stringifyNonDeterministic(obj, null, 1)
assert.equal(actual, expected)
assert.end()
})
test('check typed arrays', function (assert) {
const obj = [null, null, new Float32Array(99), Infinity, Symbol('null'), true, false, [], {}, Symbol('null')]
const expected = JSON.stringify(obj)
const actual = stringify(obj)
assert.equal(actual, expected)
assert.end()
})
test('check small typed arrays with extra properties', function (assert) {
const obj = new Uint8Array(0)
// @ts-expect-error
obj.foo = true
let expected = JSON.stringify(obj)
const actualA = stringify(obj)
assert.equal(actualA, expected)
expected = JSON.stringify(obj, null, 2)
const actualB = stringify(obj, null, 2)
assert.equal(actualB, expected)
expected = JSON.stringify(obj, ['foo'])
const actualC = stringify(obj, ['foo'])
assert.equal(actualC, expected)
expected = JSON.stringify(obj, (a, b) => b)
const actualD = stringify(obj, (a, b) => b)
assert.equal(actualD, expected)
assert.end()
})
test('trigger sorting fast path for objects with lots of properties', function (assert) {
const keys = []
const obj = {}
for (let i = 0; i < 1e4; i++) {
obj[`a${i}`] = i
keys.push(`a${i}`)
}
const start = Date.now()
stringify(obj)
assert.ok(Date.now() - start < 100)
const now = Date.now()
const actualTime = now - start
keys.sort()
const expectedTime = Date.now() - now
assert.ok(Math.abs(actualTime - expectedTime) < 50)
assert.end()
})
test('maximum spacer length', function (assert) {
const input = { a: 0 }
const expected = `{\n${' '.repeat(10)}"a": 0\n}`
assert.equal(stringify(input, null, 11), expected)
assert.equal(stringify(input, null, 1e5), expected)
assert.equal(stringify(input, null, ' '.repeat(11)), expected)
assert.equal(stringify(input, null, ' '.repeat(1e3)), expected)
assert.end()
})
test('indent properly; regression test for issue #16', function (assert) {
const o = {
collections: {},
config: {
label: 'Some\ttext\t',
options: { toJSON () { return { exportNotes: true } } },
preferences: []
},
items: [{
creators: [{ lastName: 'Lander' }, { toJSON () { return null } }],
date: { toJSON () { return '01/01/1989' } }
}]
}
const arrayReplacer = ['config', 'items', 'options', 'circular', 'preferences', 'creators']
const indentedJSON = JSON.stringify(o, null, 2)
const indentedJSONArrayReplacer = JSON.stringify(o, arrayReplacer, 2)
const indentedJSONArrayEmpty = JSON.stringify(o, [], 2)
const indentedJSONReplacer = JSON.stringify(o, (k, v) => v, 2)
assert.equal(
stringify(o, null, 2),
indentedJSON
)
assert.equal(
stringify(o, arrayReplacer, 2),
indentedJSONArrayReplacer
)
assert.equal(
stringify(o, [], 2),
indentedJSONArrayEmpty
)
assert.equal(
stringify(o, (k, v) => v, 2),
indentedJSONReplacer
)
o.items[0].circular = o
const circularReplacement = '"items": [\n {\n "circular": "[Circular]",\n'
const circularIdentifier = '"items": [\n {\n'
assert.equal(
stringify(o, arrayReplacer, 2),
indentedJSONArrayReplacer.replace(circularIdentifier, circularReplacement)
)
assert.equal(
stringify(o, null, 2),
indentedJSON.replace(circularIdentifier, circularReplacement)
)
assert.equal(
stringify(o, (k, v) => v, 2),
indentedJSONReplacer.replace(circularIdentifier, circularReplacement)
)
assert.end()
})
test('should stop if max depth is reached', (assert) => {
const serialize = stringify.configure({
maximumDepth: 5
})
const nested = {}
const MAX_DEPTH = 10
let currentNestedObject = null
for (let i = 0; i < MAX_DEPTH; i++) {
const k = 'nest_' + i
if (!currentNestedObject) {
currentNestedObject = nested
}
currentNestedObject[k] = {
foo: 'bar'
}
currentNestedObject = currentNestedObject[k]
}
const res = serialize(nested)
assert.ok(res.indexOf('"nest_4":"[Object]"'))
assert.end()
})
test('should serialize only first 10 elements', (assert) => {
const serialize = stringify.configure({
maximumBreadth: 10
})
const breadth = {}
const MAX_BREADTH = 100
for (let i = 0; i < MAX_BREADTH; i++) {
const k = 'key_' + i
breadth[k] = 'foobar'
}
const res = serialize(breadth)
const expected = '{"key_0":"foobar","key_1":"foobar","key_10":"foobar","key_11":"foobar","key_12":"foobar","key_13":"foobar","key_14":"foobar","key_15":"foobar","key_16":"foobar","key_17":"foobar","...":"90 items not stringified"}'
assert.equal(res, expected)
assert.end()
})
test('should serialize only first 10 elements with custom replacer and indentation', (assert) => {
const serialize = stringify.configure({
maximumBreadth: 10,
maximumDepth: 1
})
const breadth = { a: Array.from({ length: 100 }, (_, i) => i) }
const MAX_BREADTH = 100
for (let i = 0; i < MAX_BREADTH; i++) {
const k = 'key_' + i
breadth[k] = 'foobar'
}
const res = serialize(breadth, (k, v) => v, 2)
const expected = `{
"a": "[Array]",
"key_0": "foobar",
"key_1": "foobar",
"key_10": "foobar",
"key_11": "foobar",
"key_12": "foobar",
"key_13": "foobar",
"key_14": "foobar",
"key_15": "foobar",
"key_16": "foobar",
"...": "91 items not stringified"
}`
assert.equal(res, expected)
assert.end()
})
test('maximumDepth config', function (assert) {
const obj = { a: { b: { c: 1 }, a: [1, 2, 3] } }
const serialize = stringify.configure({
maximumDepth: 2
})
const result = serialize(obj, (key, val) => val)
assert.equal(result, '{"a":{"a":"[Array]","b":"[Object]"}}')
const res2 = serialize(obj, ['a', 'b'])
assert.equal(res2, '{"a":{"a":"[Array]","b":{}}}')
const json = JSON.stringify(obj, ['a', 'b'])
assert.equal(json, '{"a":{"a":[1,2,3],"b":{}}}')
const res3 = serialize(obj, null, 2)
assert.equal(res3, `{
"a": {
"a": "[Array]",
"b": "[Object]"
}
}`)
const res4 = serialize(obj)
assert.equal(res4, '{"a":{"a":"[Array]","b":"[Object]"}}')
assert.end()
})
test('maximumBreadth config', function (assert) {
const obj = { a: ['a', 'b', 'c', 'd', 'e'] }
const serialize = stringify.configure({
maximumBreadth: 3
})
const result = serialize(obj, (key, val) => val)
assert.equal(result, '{"a":["a","b","c","... 1 item not stringified"]}')
const res2 = serialize(obj, ['a', 'b'])
assert.equal(res2, '{"a":["a","b","c","... 1 item not stringified"]}')
const res3 = serialize(obj, null, 2)
assert.equal(res3, `{
"a": [
"a",
"b",
"c",
"... 1 item not stringified"
]
}`)
const res4 = serialize({ a: { a: 1, b: 1, c: 1, d: 1, e: 1 } }, null, 2)
assert.equal(res4, `{
"a": {
"a": 1,
"b": 1,
"c": 1,
"...": "2 items not stringified"
}
}`)
assert.end()
})
test('limit number of keys with array replacer', function (assert) {
const replacer = ['a', 'b', 'c', 'd', 'e']
const obj = {
a: 'a',
b: 'b',
c: 'c',
d: 'd',
e: 'e',
f: 'f',
g: 'g',
h: 'h'
}
const serialize = stringify.configure({
maximumBreadth: 3
})
const res = serialize(obj, replacer, 2)
const expected = `{
"a": "a",
"b": "b",
"c": "c",
"d": "d",
"e": "e"
}`
assert.equal(res, expected)
assert.end()
})
test('limit number of keys in array', (assert) => {
const serialize = stringify.configure({
maximumBreadth: 3
})
const arr = []
const MAX_BREADTH = 100
for (let i = 0; i < MAX_BREADTH; i++) {
arr.push(i)
}
const res = serialize(arr)
const expected = '[0,1,2,"... 96 items not stringified"]'
assert.equal(res, expected)
assert.end()
})
test('limit number of keys in typed array', (assert) => {
const serialize = stringify.configure({
maximumBreadth: 3
})
const MAX = 100
const arr = new Int32Array(MAX)
for (let i = 0; i < MAX; i++) {
arr[i] = i
}
// @ts-expect-error we want to explicitly test this behavior.
arr.foobar = true
const res = serialize(arr)
const expected = '{"0":0,"1":1,"2":2,"...":"98 items not stringified"}'
assert.equal(res, expected)
const res2 = serialize(arr, (a, b) => b)
assert.equal(res2, expected)