-
Notifications
You must be signed in to change notification settings - Fork 84
/
test.js
1268 lines (1031 loc) · 40.4 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
'use strict'
var expect = require('chai').expect,
objectPath = require('./index.js')
function getTestObj () {
return {
a: 'b',
b: {
c: [],
d: ['a', 'b'],
e: [{}, {f: 'g'}],
f: 'i'
}
}
}
describe('get', function () {
it('should return the value using unicode key', function () {
var obj = {
'15\u00f8C': {
'3\u0111': 1
}
}
expect(objectPath.get(obj, '15\u00f8C.3\u0111')).to.be.equal(1)
expect(objectPath.get(obj, ['15\u00f8C', '3\u0111'])).to.be.equal(1)
})
it('should return the value using dot in key', function () {
var obj = {
'a.b': {
'looks.like': 1
}
}
expect(objectPath.get(obj, 'a.b.looks.like')).to.be.equal(void 0)
expect(objectPath.get(obj, ['a.b', 'looks.like'])).to.be.equal(1)
})
it('should return the value under shallow object', function () {
var obj = getTestObj()
expect(objectPath.get(obj, 'a')).to.be.equal('b')
expect(objectPath.get(obj, ['a'])).to.be.equal('b')
})
it('should work with number path', function () {
var obj = getTestObj()
expect(objectPath.get(obj.b.d, 0)).to.be.equal('a')
expect(objectPath.get(obj.b, 0)).to.be.equal(void 0)
})
it('should return the value under deep object', function () {
var obj = getTestObj()
expect(objectPath.get(obj, 'b.f')).to.be.equal('i')
expect(objectPath.get(obj, ['b', 'f'])).to.be.equal('i')
})
it('should return the value under array', function () {
var obj = getTestObj()
expect(objectPath.get(obj, 'b.d.0')).to.be.equal('a')
expect(objectPath.get(obj, ['b', 'd', 0])).to.be.equal('a')
})
it('should return the value under array deep', function () {
var obj = getTestObj()
expect(objectPath.get(obj, 'b.e.1.f')).to.be.equal('g')
expect(objectPath.get(obj, ['b', 'e', 1, 'f'])).to.be.equal('g')
})
it('should return undefined for missing values under object', function () {
var obj = getTestObj()
expect(objectPath.get(obj, 'a.b')).to.not.exist
expect(objectPath.get(obj, ['a', 'b'])).to.not.exist
})
it('should return undefined for missing values under array', function () {
var obj = getTestObj()
expect(objectPath.get(obj, 'b.d.5')).to.not.exist
expect(objectPath.get(obj, ['b', 'd', '5'])).to.not.exist
})
it('should return the value under integer-like key', function () {
var obj = {'1a': 'foo'}
expect(objectPath.get(obj, '1a')).to.be.equal('foo')
expect(objectPath.get(obj, ['1a'])).to.be.equal('foo')
})
it('should return the default value when the key doesnt exist', function () {
var obj = {'1a': 'foo'}
expect(objectPath.get(obj, '1b', null)).to.be.equal(null)
expect(objectPath.get(obj, ['1b'], null)).to.be.equal(null)
})
it('should return the default value when path is empty', function () {
var obj = {'1a': 'foo'}
expect(objectPath.get(obj, '', null)).to.be.deep.equal({'1a': 'foo'})
expect(objectPath.get(obj, [])).to.be.deep.equal({'1a': 'foo'})
expect(objectPath.get({}, ['1'])).to.be.equal(undefined)
})
it('should return the default value when object is null or undefined', function () {
expect(objectPath.get(null, 'test', 'a')).to.be.deep.equal('a')
expect(objectPath.get(undefined, 'test', 'a')).to.be.deep.equal('a')
})
it(
'should not fail on an object with a null prototype',
function assertSuccessForObjWithNullProto () {
var foo = 'FOO'
var objWithNullProto = Object.create(null)
objWithNullProto.foo = foo
expect(objectPath.get(objWithNullProto, 'foo')).to.equal(foo)
}
)
it('should skip non own properties', function () {
var Base = function (enabled) {
}
Base.prototype = {
one: {
two: true
}
}
var Extended = function () {
Base.call(this, true)
}
Extended.prototype = Object.create(Base.prototype)
var extended = new Extended()
expect(objectPath.get(extended, ['one', 'two'])).to.be.equal(undefined)
extended.enabled = true
expect(objectPath.get(extended, 'enabled')).to.be.equal(true)
expect(objectPath.get(extended, 'one')).to.be.equal(undefined)
})
it('[security] should not get magic properties in default mode', function () {
expect(objectPath.get({}, '__proto__')).to.be.undefined
expect(objectPath.get({}, [['__proto__']])).to.be.undefined
function Clazz() {}
Clazz.prototype.test = []
expect(objectPath.get(new Clazz(), '__proto__')).to.be.undefined
expect(objectPath.get(new Clazz(), [['__proto__']])).to.be.undefined
expect(objectPath.get(new Clazz(), ['constructor', 'prototype'])).to.be.undefined
})
it('[security] should not get magic properties in inheritedProps mode', function () {
expect(function() {
objectPath.withInheritedProps.get({}, '__proto__')
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.get({}, [['__proto__']])
}).to.throw('For security reasons')
function Clazz() {}
Clazz.prototype.test = 'original'
expect(function() {
objectPath.withInheritedProps.get(new Clazz(), '__proto__')
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.get(new Clazz(), [['__proto__']])
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.get(new Clazz(), ['constructor', 'prototype'])
}).to.throw('For security reasons')
})
})
describe('set', function () {
it('should set the value using unicode key', function () {
var obj = {
'15\u00f8C': {
'3\u0111': 1
}
}
objectPath.set(obj, '15\u00f8C.3\u0111', 2)
expect(objectPath.get(obj, '15\u00f8C.3\u0111')).to.be.equal(2)
objectPath.set(obj, '15\u00f8C.3\u0111', 3)
expect(objectPath.get(obj, ['15\u00f8C', '3\u0111'])).to.be.equal(3)
})
it('should set the value using dot in key', function () {
var obj = {
'a.b': {
'looks.like': 1
}
}
objectPath.set(obj, ['a.b', 'looks.like'], 2)
expect(objectPath.get(obj, ['a.b', 'looks.like'])).to.be.equal(2)
})
it('should set value under shallow object', function () {
var obj = getTestObj()
objectPath.set(obj, 'c', {m: 'o'})
expect(obj).to.include.nested.property('c.m', 'o')
obj = getTestObj()
objectPath.set(obj, ['c'], {m: 'o'})
expect(obj).to.include.nested.property('c.m', 'o')
})
it('should set value using number path', function () {
var obj = getTestObj()
objectPath.set(obj.b.d, 0, 'o')
expect(obj).to.have.nested.property('b.d.0', 'o')
})
it('should set value under deep object', function () {
var obj = getTestObj()
objectPath.set(obj, 'b.c', 'o')
expect(obj).to.have.nested.property('b.c', 'o')
obj = getTestObj()
objectPath.set(obj, ['b', 'c'], 'o')
expect(obj).to.have.nested.property('b.c', 'o')
})
it('should set value under array', function () {
var obj = getTestObj()
objectPath.set(obj, 'b.e.1.g', 'f')
expect(obj).to.have.nested.property('b.e.1.g', 'f')
obj = getTestObj()
objectPath.set(obj, ['b', 'e', 1, 'g'], 'f')
expect(obj).to.have.nested.property('b.e.1.g', 'f')
obj = {}
objectPath.set(obj, 'b.0', 'a')
objectPath.set(obj, 'b.1', 'b')
expect(obj.b).to.be.deep.equal(['a', 'b'])
})
it('should create intermediate objects', function () {
var obj = getTestObj()
objectPath.set(obj, 'c.d.e.f', 'l')
expect(obj).to.have.nested.property('c.d.e.f', 'l')
obj = getTestObj()
objectPath.set(obj, ['c', 'd', 'e', 'f'], 'l')
expect(obj).to.have.nested.property('c.d.e.f', 'l')
})
it('should create intermediate arrays', function () {
var obj = getTestObj()
objectPath.set(obj, 'c.0.1.m', 'l')
expect(obj.c).to.be.an('array')
expect(obj.c[0]).to.be.an('array')
expect(obj).to.have.nested.property('c.0.1.m', 'l')
obj = getTestObj()
objectPath.set(obj, ['c', '0', 1, 'm'], 'l')
expect(obj.c).to.be.an('object')
expect(obj.c[0]).to.be.an('array')
expect(obj).to.have.nested.property('c.0.1.m', 'l')
})
it('should set value under integer-like key', function () {
var obj = getTestObj()
objectPath.set(obj, '1a', 'foo')
expect(obj).to.have.nested.property('1a', 'foo')
obj = getTestObj()
objectPath.set(obj, ['1a'], 'foo')
expect(obj).to.have.nested.property('1a', 'foo')
})
it('should set value under empty array', function () {
var obj = []
objectPath.set(obj, [0], 'foo')
expect(obj[0]).to.be.equal('foo')
obj = []
objectPath.set(obj, '0', 'foo')
expect(obj[0]).to.be.equal('foo')
})
it('[security] should not set magic properties in default mode', function () {
objectPath.set({}, '__proto__.injected', 'this is bad')
expect(Object.prototype.injected).to.be.undefined
objectPath.set({}, [['__proto__'], 'injected'], 'this is bad')
expect(Object.prototype.injected).to.be.undefined
objectPath.set({}, ['__proto__'], {})
expect(Object.prototype.toString).to.be.a('function')
function Clazz() {}
Clazz.prototype.test = 'original'
objectPath.set({}, ['__proto__'], {test: 'this is bad'})
expect(Clazz.prototype.test).to.be.equal('original')
objectPath.set(new Clazz(), '__proto__.test', 'this is bad')
expect(Clazz.prototype.test).to.be.equal('original')
objectPath.set(new Clazz(), [['__proto__'], 'test'], 'this is bad')
expect(Clazz.prototype.test).to.be.equal('original')
objectPath.set(new Clazz(), 'constructor.prototype.test', 'this is bad')
expect(Clazz.prototype.test).to.be.equal('original')
})
it('[security] should throw an exception if trying to set magic properties in inheritedProps mode', function () {
expect(function() {
objectPath.withInheritedProps.set({}, '__proto__.injected', 'this is bad')
expect(Object.prototype.injected).to.be.undefined
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.set({}, [['__proto__'], 'injected'], 'this is bad')
expect(Object.prototype.injected).to.be.undefined
}).to.throw('For security reasons')
function Clazz() {}
Clazz.prototype.test = 'original'
expect(function() {
objectPath.withInheritedProps.set(new Clazz(), '__proto__.test', 'this is bad')
expect(Clazz.prototype.test).to.be.equal('original')
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.set(new Clazz(), 'constructor.prototype.test', 'this is bad')
expect(Clazz.prototype.test).to.be.equal('original')
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.set({}, 'constructor.prototype.injected', 'this is OK')
expect(Object.prototype.injected).to.be.undefined
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.set({}, [['constructor'], 'prototype', 'injected'], 'this is bad')
expect(Object.prototype.injected).to.be.undefined
}).to.throw('For security reasons')
})
})
describe('push', function () {
it('should push value to existing array using unicode key', function () {
var obj = getTestObj()
objectPath.push(obj, 'b.\u1290c', 'l')
expect(obj).to.have.nested.property('b.\u1290c.0', 'l')
objectPath.push(obj, ['b', '\u1290c'], 'l')
expect(obj).to.have.nested.property('b.\u1290c.1', 'l')
})
it('should push value to existing array using dot key', function () {
var obj = getTestObj()
objectPath.push(obj, ['b', 'z.d'], 'l')
expect(objectPath.get(obj, ['b', 'z.d', 0])).to.be.equal('l')
})
it('should push value to existing array', function () {
var obj = getTestObj()
objectPath.push(obj, 'b.c', 'l')
expect(obj).to.have.nested.property('b.c.0', 'l')
obj = getTestObj()
objectPath.push(obj, ['b', 'c'], 'l')
expect(obj).to.have.nested.property('b.c.0', 'l')
})
it('should push value to new array', function () {
var obj = getTestObj()
objectPath.push(obj, 'b.h', 'l')
expect(obj).to.have.nested.property('b.h.0', 'l')
obj = getTestObj()
objectPath.push(obj, ['b', 'h'], 'l')
expect(obj).to.have.nested.property('b.h.0', 'l')
})
it('should push value to existing array using number path', function () {
var obj = getTestObj()
objectPath.push(obj.b.e, 0, 'l')
expect(obj).to.have.nested.property('b.e.0.0', 'l')
})
it('[security] should not push within prototype properties in default mode', function () {
function Clazz() {}
Clazz.prototype.test = []
objectPath.push(new Clazz(), '__proto__.test', 'pushed')
expect(Clazz.prototype.test).to.be.deep.equal([])
objectPath.push(new Clazz(), [['__proto__'], 'test'], 'pushed')
expect(Clazz.prototype.test).to.be.deep.equal([])
objectPath.push(new Clazz(), 'constructor.prototype.test', 'pushed')
expect(Clazz.prototype.test).to.be.deep.equal([])
})
it('[security] should not push within prototype properties in inheritedProps mode', function () {
function Clazz() {}
Clazz.prototype.test = []
expect(function() {
objectPath.withInheritedProps.push(new Clazz(), '__proto__.test', 'pushed')
expect(Clazz.prototype.test).to.be.deep.equal([])
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.push(new Clazz(), [['__proto__'], 'test'], 'pushed')
expect(Clazz.prototype.test).to.be.deep.equal([])
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.push(new Clazz(), 'constructor.prototype.test', 'pushed')
expect(Clazz.prototype.test).to.be.deep.equal([])
}).to.throw('For security reasons')
})
})
describe('ensureExists', function () {
it('should create the path if it does not exists', function () {
var obj = getTestObj()
var oldVal = objectPath.ensureExists(obj, 'b.g.1.l', 'test')
expect(oldVal).to.not.exist
expect(obj).to.have.nested.property('b.g.1.l', 'test')
oldVal = objectPath.ensureExists(obj, 'b.g.1.l', 'test1')
expect(oldVal).to.be.equal('test')
expect(obj).to.have.nested.property('b.g.1.l', 'test')
oldVal = objectPath.ensureExists(obj, 'b.\u8210', 'ok')
expect(oldVal).to.not.exist
expect(obj).to.have.nested.property('b.\u8210', 'ok')
oldVal = objectPath.ensureExists(obj, ['b', 'dot.dot'], 'ok')
expect(oldVal).to.not.exist
expect(objectPath.get(obj, ['b', 'dot.dot'])).to.be.equal('ok')
})
it('should return the object if path is empty', function () {
var obj = getTestObj()
expect(objectPath.ensureExists(obj, [], 'test')).to.have.property('a', 'b')
})
it('Issue #26', function () {
var any = {}
objectPath.ensureExists(any, ['1', '1'], {})
expect(any).to.be.an('object')
expect(any[1]).to.be.an('object')
expect(any[1][1]).to.be.an('object')
})
it('[security] should not set magic properties in default mode', function () {
objectPath.ensureExists({}, '__proto__.injected', 'this is bad')
expect(Object.prototype.injected).to.be.undefined
objectPath.ensureExists({}, [['__proto__'], 'injected'], 'this is bad')
expect(Object.prototype.injected).to.be.undefined
objectPath.ensureExists({}, ['__proto__'], {})
expect(Object.prototype.toString).to.be.a('function')
function Clazz() {}
Clazz.prototype.test = 'original'
objectPath.ensureExists({}, ['__proto__'], {test: 'this is bad'})
expect(Clazz.prototype.test).to.be.equal('original')
objectPath.ensureExists(new Clazz(), '__proto__.test', 'this is bad')
expect(Clazz.prototype.test).to.be.equal('original')
objectPath.ensureExists(new Clazz(), [['__proto__'], 'test'], 'this is bad')
expect(Clazz.prototype.test).to.be.equal('original')
objectPath.ensureExists(new Clazz(), 'constructor.prototype.test', 'this is bad')
expect(Clazz.prototype.test).to.be.equal('original')
})
it('[security] should throw an exception if trying to set magic properties in inheritedProps mode', function () {
expect(function() {objectPath.withInheritedProps.ensureExists({}, '__proto__.injected', 'this is bad')})
.to.throw('For security reasons')
expect(Object.prototype.injected).to.be.undefined
expect(function() {
objectPath.withInheritedProps.ensureExists({}, [['__proto__'], 'injected'], 'this is bad')
expect(Object.prototype.injected).to.be.undefined
}).to.throw('For security reasons')
function Clazz() {}
Clazz.prototype.test = 'original'
expect(function() {
objectPath.withInheritedProps.ensureExists(new Clazz(), '__proto__.test', 'this is bad')
expect(Clazz.prototype.test).to.be.equal('original')
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.ensureExists(new Clazz(), 'constructor.prototype.test', 'this is bad')
expect(Clazz.prototype.test).to.be.equal('original')
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.ensureExists({}, 'constructor.prototype.injected', 'this is OK')
expect(Object.prototype.injected).to.be.undefined
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.ensureExists({}, [['constructor'], 'prototype', 'injected'], 'this is bad')
expect(Object.prototype.injected).to.be.undefined
}).to.throw('For security reasons')
})
})
describe('coalesce', function () {
it('should return the first non-undefined value', function () {
var obj = {
should: {have: 'prop'}
}
expect(objectPath.coalesce(obj, [
'doesnt.exist',
['might', 'not', 'exist'],
'should.have'
])).to.equal('prop')
})
it('should work with falsy values (null, 0, \'\', false)', function () {
var obj = {
is: {
false: false,
null: null,
empty: '',
zero: 0
}
}
expect(objectPath.coalesce(obj, [
'doesnt.exist',
'is.zero'
])).to.equal(0)
expect(objectPath.coalesce(obj, [
'doesnt.exist',
'is.false'
])).to.equal(false)
expect(objectPath.coalesce(obj, [
'doesnt.exist',
'is.null'
])).to.equal(null)
expect(objectPath.coalesce(obj, [
'doesnt.exist',
'is.empty'
])).to.equal('')
})
it('returns defaultValue if no paths found', function () {
var obj = {
doesnt: 'matter'
}
expect(objectPath.coalesce(obj, ['some.inexistant', 'path', ['on', 'object']], 'false')).to.equal('false')
})
it('works with unicode and dot keys', function () {
var obj = {
'\u7591': true,
'dot.dot': false
}
expect(objectPath.coalesce(obj, ['1', '\u7591', 'a.b'])).to.equal(true)
expect(objectPath.coalesce(obj, ['1', ['dot.dot'], '\u7591'])).to.equal(false)
})
})
describe('empty', function () {
it('should ignore invalid arguments safely', function () {
var obj = {}
expect(objectPath.empty()).to.equal(void 0)
expect(objectPath.empty(obj, 'path')).to.equal(void 0)
expect(objectPath.empty(obj, '')).to.equal(void 0)
obj.path = true
expect(objectPath.empty(obj, 'inexistant')).to.equal(void 0)
expect(objectPath.empty(null, 'path')).to.equal(void 0)
expect(objectPath.empty(void 0, 'path')).to.equal(void 0)
})
it('should empty each path according to their types', function () {
function Instance () {
this.notOwn = true
}
/*istanbul ignore next: not part of code */
Instance.prototype.test = function () {
}
/*istanbul ignore next: not part of code */
Instance.prototype.arr = []
var
obj = {
string: 'some string',
array: ['some', 'array', [1, 2, 3]],
number: 21,
boolean: true,
object: {
some: 'property',
sub: {
'property': true
},
nullProp: null,
undefinedProp: void 0
},
instance: new Instance()
}
/*istanbul ignore next: not part of code */
obj['function'] = function () {
}
objectPath.empty(obj, ['array', '2'])
expect(obj.array[2]).to.deep.equal([])
objectPath.empty(obj, 'object.sub')
expect(obj.object.sub).to.deep.equal({})
objectPath.empty(obj, 'object.nullProp')
expect(obj.object.nullProp).to.equal(null)
objectPath.empty(obj, 'object.undefinedProp')
expect(obj.object.undefinedProp).to.equal(void 0)
expect(obj.object).to.have.property('undefinedProp')
objectPath.empty(obj, 'object.notAProp')
expect(obj.object.notAProp).to.equal(void 0)
expect(obj.object).to.not.have.property('notAProp')
objectPath.empty(obj, 'instance.test')
//instance.test is not own property, so it shouldn't be emptied
expect(obj.instance.test).to.be.a('function')
expect(Instance.prototype.test).to.be.a('function')
objectPath.empty(obj, 'string')
objectPath.empty(obj, 'number')
objectPath.empty(obj, 'boolean')
objectPath.empty(obj, 'function')
objectPath.empty(obj, 'array')
objectPath.empty(obj, 'object')
objectPath.empty(obj, 'instance')
expect(obj.string).to.equal('')
expect(obj.array).to.deep.equal([])
expect(obj.number).to.equal(0)
expect(obj.boolean).to.equal(false)
expect(obj.object).to.deep.equal({})
expect(obj.instance.notOwn).to.be.an('undefined')
expect(obj.instance.arr).to.be.an('array')
expect(obj['function']).to.equal(null)
})
it('[security] should not empty prototype properties in default mode', function () {
function Clazz() {}
Clazz.prototype.test = 'original'
objectPath.empty(new Clazz(), '__proto__')
expect(Clazz.prototype.test).to.be.equal('original')
objectPath.empty(new Clazz(), [['__proto__']])
expect(Clazz.prototype.test).to.be.equal('original')
objectPath.empty(new Clazz(), 'constructor.prototype')
expect(Clazz.prototype.test).to.be.equal('original')
})
it('[security] should throw an exception if trying to delete prototype properties in inheritedProps mode', function () {
function Clazz() {}
Clazz.prototype.test = 'original'
expect(function() {
objectPath.withInheritedProps.empty(new Clazz(), '__proto__')
expect(Clazz.prototype.test).to.be.equal('original')
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.empty(new Clazz(), 'constructor.prototype')
expect(Clazz.prototype.test).to.be.equal('original')
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.empty({}, [['constructor'], 'prototype'])
expect(Clazz.prototype.test).to.be.equal('original')
}).to.throw('For security reasons')
})
})
describe('del', function () {
it('should work with number path', function () {
var obj = getTestObj()
objectPath.del(obj.b.d, 1)
expect(obj.b.d).to.deep.equal(['a'])
})
it('should remove null and undefined props (but not explode on nested)', function () {
var obj = {nullProp: null, undefinedProp: void 0}
expect(obj).to.have.property('nullProp')
expect(obj).to.have.property('undefinedProp')
objectPath.del(obj, 'nullProp.foo')
objectPath.del(obj, 'undefinedProp.bar')
expect(obj).to.have.property('nullProp')
expect(obj).to.have.property('undefinedProp')
expect(obj).to.deep.equal({nullProp: null, undefinedProp: void 0})
objectPath.del(obj, 'nullProp')
objectPath.del(obj, 'undefinedProp')
expect(obj).to.not.have.property('nullProp')
expect(obj).to.not.have.property('undefinedProp')
expect(obj).to.deep.equal({})
})
it('should delete deep paths', function () {
var obj = getTestObj()
expect(objectPath.del(obj)).to.be.equal(obj)
objectPath.set(obj, 'b.g.1.0', 'test')
objectPath.set(obj, 'b.g.1.1', 'test')
objectPath.set(obj, 'b.h.az', 'test')
objectPath.set(obj, 'b.\ubeef', 'test')
objectPath.set(obj, ['b', 'dot.dot'], 'test')
expect(obj).to.have.nested.property('b.g.1.0', 'test')
expect(obj).to.have.nested.property('b.g.1.1', 'test')
expect(obj).to.have.nested.property('b.h.az', 'test')
expect(obj).to.have.nested.property('b.\ubeef', 'test')
objectPath.del(obj, 'b.h.az')
expect(obj).to.not.have.nested.property('b.h.az')
expect(obj).to.have.nested.property('b.h')
objectPath.del(obj, 'b.g.1.1')
expect(obj).to.not.have.nested.property('b.g.1.1')
expect(obj).to.have.nested.property('b.g.1.0', 'test')
objectPath.del(obj, 'b.\ubeef')
expect(obj).to.not.have.nested.property('b.\ubeef')
objectPath.del(obj, ['b', 'dot.dot'])
expect(objectPath.get(obj, ['b', 'dot.dot'])).to.be.equal(void 0)
objectPath.del(obj, ['b', 'g', '1', '0'])
expect(obj).to.not.have.nested.property('b.g.1.0')
expect(obj).to.have.nested.property('b.g.1')
expect(objectPath.del(obj, ['b'])).to.not.have.nested.property('b.g')
expect(obj).to.be.deep.equal({'a': 'b'})
})
it('should remove items from existing array', function () {
var obj = getTestObj()
objectPath.del(obj, 'b.d.0')
expect(obj.b.d).to.have.length(1)
expect(obj.b.d).to.be.deep.equal(['b'])
objectPath.del(obj, 'b.d.0')
expect(obj.b.d).to.have.length(0)
expect(obj.b.d).to.be.deep.equal([])
})
it('[security] should not delete prototype properties in default mode', function () {
objectPath.del({}, '__proto__.valueOf')
expect(Object.prototype.valueOf).to.be.a('function')
objectPath.del({}, [['__proto__'], 'valueOf'])
expect(Object.prototype.valueOf).to.be.a('function')
function Clazz() {}
Clazz.prototype.test = 'original'
objectPath.del(new Clazz(), '__proto__.test')
expect(Clazz.prototype.test).to.be.equal('original')
objectPath.del(new Clazz(), [['__proto__'], 'test'])
expect(Clazz.prototype.test).to.be.equal('original')
objectPath.del(new Clazz(), 'constructor.prototype.test')
expect(Clazz.prototype.test).to.be.equal('original')
})
it('[security] should throw an exception if trying to delete prototype properties in inheritedProps mode', function () {
expect(function() {
objectPath.withInheritedProps.del({}, '__proto__.valueOf')
expect(Object.prototype.valueOf).to.be.a('function')
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.del({}, [['__proto__'], 'valueOf'])
expect(Object.prototype.valueOf).to.be.a('function')
}).to.throw('For security reasons')
function Clazz() {}
Clazz.prototype.test = 'original'
expect(function() {
objectPath.withInheritedProps.del(new Clazz(), '__proto__.test')
expect(Clazz.prototype.test).to.be.equal('original')
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.del(new Clazz(), 'constructor.prototype.test', 'this is bad')
expect(Clazz.prototype.test).to.be.equal('original')
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.del({}, [['constructor'], 'prototype', 'test'])
expect(Clazz.prototype.test).to.be.equal('original')
}).to.throw('For security reasons')
})
})
describe('insert', function () {
it('should insert value into existing array', function () {
var obj = getTestObj()
objectPath.insert(obj, 'b.c', 'asdf')
expect(obj).to.have.nested.property('b.c.0', 'asdf')
expect(obj).to.not.have.nested.property('b.c.1')
})
it('should create intermediary array', function () {
var obj = getTestObj()
objectPath.insert(obj, 'b.c.0', 'asdf')
expect(obj).to.have.nested.property('b.c.0.0', 'asdf')
})
it('should insert in another index', function () {
var obj = getTestObj()
objectPath.insert(obj, 'b.d', 'asdf', 1)
expect(obj).to.have.nested.property('b.d.1', 'asdf')
expect(obj).to.have.nested.property('b.d.0', 'a')
expect(obj).to.have.nested.property('b.d.2', 'b')
})
it('should handle sparse array', function () {
var obj = getTestObj()
obj.b.d = new Array(4)
obj.b.d[0] = 'a'
obj.b.d[1] = 'b'
objectPath.insert(obj, 'b.d', 'asdf', 3)
expect(obj.b.d).to.have.members([
'a',
'b',
,
,
'asdf'
])
})
it('[security] should not insert within prototype properties in default mode', function () {
function Clazz() {}
Clazz.prototype.test = []
objectPath.insert(new Clazz(), '__proto__.test', 'inserted', 0)
expect(Clazz.prototype.test).to.be.deep.equal([])
objectPath.insert(new Clazz(), [['__proto__'], 'test'], 'inserted', 0)
expect(Clazz.prototype.test).to.be.deep.equal([])
objectPath.insert(new Clazz(), 'constructor.prototype.test', 'inserted', 0)
expect(Clazz.prototype.test).to.be.deep.equal([])
})
it('[security] should not insert within prototype properties in inheritedProps mode', function () {
function Clazz() {}
Clazz.prototype.test = []
expect(function() {
objectPath.withInheritedProps.insert(new Clazz(), '__proto__.test', 'inserted', 0)
expect(Clazz.prototype.test).to.be.deep.equal([])
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.insert(new Clazz(), [['__proto__'], 'test'], 'inserted', 0)
expect(Clazz.prototype.test).to.be.deep.equal([])
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.insert(new Clazz(), 'constructor.prototype.test', 'inserted', 0)
expect(Clazz.prototype.test).to.be.deep.equal([])
}).to.throw('For security reasons')
expect(function() {
objectPath.withInheritedProps.insert(new Clazz().constructor, 'prototype.test', 'inserted', 0)
expect(Clazz.prototype.test).to.be.deep.equal([])
}).to.throw('For security reasons')
})
})
describe('has', function () {
it('should return false for empty object', function () {
expect(objectPath.has({}, 'a')).to.be.equal(false)
})
it('should handle empty paths properly', function () {
var obj = getTestObj()
expect(objectPath.has(obj, '')).to.be.equal(false)
expect(objectPath.has(obj, [''])).to.be.equal(false)
obj[''] = 1
expect(objectPath.has(obj, '')).to.be.equal(true)
expect(objectPath.has(obj, [''])).to.be.equal(true)
expect(objectPath.has(obj, [])).to.be.equal(true)
expect(objectPath.has(null, [])).to.be.equal(false)
})
it('should test under shallow object', function () {
var obj = getTestObj()
expect(objectPath.has(obj, 'a')).to.be.equal(true)
expect(objectPath.has(obj, ['a'])).to.be.equal(true)
expect(objectPath.has(obj, 'z')).to.be.equal(false)
expect(objectPath.has(obj, ['z'])).to.be.equal(false)
})
it('should work with number path', function () {
var obj = getTestObj()
expect(objectPath.has(obj.b.d, 0)).to.be.equal(true)
expect(objectPath.has(obj.b, 0)).to.be.equal(false)
expect(objectPath.has(obj.b.d, 10)).to.be.equal(false)
expect(objectPath.has(obj.b, 10)).to.be.equal(false)
})
it('should test under deep object', function () {
var obj = getTestObj()
expect(objectPath.has(obj, 'b.f')).to.be.equal(true)
expect(objectPath.has(obj, ['b', 'f'])).to.be.equal(true)
expect(objectPath.has(obj, 'b.g')).to.be.equal(false)
expect(objectPath.has(obj, ['b', 'g'])).to.be.equal(false)
})
it('should test value under array', function () {
var obj = {
b: ['a']
}
obj.b[3] = {o: 'a'}
expect(objectPath.has(obj, 'b.0')).to.be.equal(true)
expect(objectPath.has(obj, 'b.1')).to.be.equal(true)
expect(objectPath.has(obj, 'b.3.o')).to.be.equal(true)
expect(objectPath.has(obj, 'b.3.qwe')).to.be.equal(false)
expect(objectPath.has(obj, 'b.4')).to.be.equal(false)
})
it('should test the value under array deep', function () {
var obj = getTestObj()
expect(objectPath.has(obj, 'b.e.1.f')).to.be.equal(true)
expect(objectPath.has(obj, ['b', 'e', 1, 'f'])).to.be.equal(true)
expect(objectPath.has(obj, 'b.e.1.f.g.h.i')).to.be.equal(false)
expect(objectPath.has(obj, ['b', 'e', 1, 'f', 'g', 'h', 'i'])).to.be.equal(false)
})
it('should test the value under integer-like key', function () {
var obj = {'1a': 'foo'}
expect(objectPath.has(obj, '1a')).to.be.equal(true)
expect(objectPath.has(obj, ['1a'])).to.be.equal(true)
})
it('should distinct nonexistent key and key = undefined', function () {
var obj = {}
expect(objectPath.has(obj, 'key')).to.be.equal(false)
obj.key = undefined
expect(objectPath.has(obj, 'key')).to.be.equal(true)
})
it('should work with deep undefined/null values', function () {
var obj = {}
expect(objectPath.has(obj, 'missing.test')).to.be.equal(false)
obj.missing = null
expect(objectPath.has(obj, 'missing.test')).to.be.equal(false)
obj.sparseArray = [1, undefined, 3]
expect(objectPath.has(obj, 'sparseArray.1.test')).to.be.equal(false)
})
})
describe('bind object', function () {
// just get one scenario from each feature, so whole functionality is proxied well
it('should return the value under shallow object', function () {
var obj = getTestObj()
var model = objectPath(obj)
expect(model.get('a')).to.be.equal('b')
expect(model.get(['a'])).to.be.equal('b')
})
it('should set value under shallow object', function () {
var obj = getTestObj()
var model = objectPath(obj)
model.set('c', {m: 'o'})