-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathobject.js
2298 lines (1854 loc) · 96.7 KB
/
object.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
namespace('Object', function () {
'use strict';
group('Chainable', function() {
equal(new Sugar.Object({foo:'bar'}).raw, {foo:'bar'}, 'argument should be converted to object');
equal(typeof new Sugar.Object('foo').raw, 'string', 'primitive should not be coerced into object');
equal(new Sugar.Object({foo:'bar',boo:'mar'}).keys().raw, ['foo','boo'], 'should have keys as instance');
});
group('Static Extended', function() {
if (!isExtendedMode()) {
// Testing basic instance methods have been
// mapped over as static, but only in extended mode.
return;
};
// Just test a few basic methods to ensure they've been mapped.
equal(Object.get({foo:'bar'}, 'foo'), 'bar', 'Object.get was extended');
equal(Object.isArray(['a']), true, 'Object.isArray was extended');
equal(Object.fromQueryString('a=b'), {a:'b'}, 'Object.fromQueryString was extended');
equal(Object.toQueryString({c:'d'}), 'c=d', 'Object.toQueryString was extended');
});
method('fromQueryString', function() {
test(Object, ['foo=bar&moo=car'], {foo:'bar',moo:'car'}, 'basic');
test(Object, ['foo=bar&moo=3'], {foo:'bar',moo:3}, 'with numbers | auto');
test(Object, ['foo=bar&moo=3', {auto:false}], {foo:'bar',moo:'3'}, 'with numbers');
test(Object, ['foo=bar&moo=true'], {foo:'bar',moo:true}, 'with true | auto');
test(Object, ['foo=bar&moo=false'], {foo:'bar',moo:false}, 'with false | auto');
test(Object, ['foo=bar&moo=true', {auto:false}], {foo:'bar',moo:'true'}, 'with true not | auto');
test(Object, ['foo=bar&moo=false', {auto:false}], {foo:'bar',moo:'false'}, 'with false not | auto');
test(Object, ['foo=bar3'], {foo:'bar3'}, 'number in back');
test(Object, ['foo=3bar'], {foo:'3bar'}, 'number up front');
test(Object, ['foo=345'], {foo:345}, 'numbers only | auto');
test(Object, ['foo&bar'], {foo:null,bar:null}, 'undefined without = | auto');
test(Object, ['foo&bar', {auto:false}], {foo:'',bar:''}, 'undefined without = | not auto');
test(Object, ['foo=&bar='], {foo:null,bar:null}, 'undefined params | auto');
test(Object, ['foo=&bar=', {auto:false}], {foo:'',bar:''}, 'undefined params | not auto');
test(Object, ['foo[]=bar&foo[]=car'], {'foo[]':['bar','car']}, 'deep strings with default');
test(Object, ['foo[]=bar&foo[]=car',{auto:false}], {'foo[]':'car'}, 'deep strings with default | not auto');
test(Object, ['foo[]=bar&foo[]=car', {deep:true}], {'foo':['bar','car']}, 'deep strings with deep');
test(Object, ['foo[bar]=tee&foo[car]=hee', {deep:true}], { foo: { bar: 'tee', car: 'hee' } }, 'handles keys');
test(Object, ['foo[cap][map]=3', {deep:true}], {foo:{cap:{map:3}}}, 'deep keys');
test(Object, ['foo[cap][map][]=3', {deep:true}], {foo:{cap:{map:[3]}}}, 'nested with trailing array');
test(Object, ['foo[moo]=1&bar[far]=2', {deep:true}], {foo:{moo:1},bar:{far:2}}, 'sister objects');
test(Object, ['foo[cap][map]=3', {deep:true,auto:false}], {foo:{cap:{map:'3'}}}, 'deep keys not auto');
test(Object, ['foo[cap][map][]=3', {deep:true,auto:false}], {foo:{cap:{map:['3']}}}, 'nested with trailing array not auto');
test(Object, ['foo[moo]=1&bar[far]=2', {deep:true,auto:false}], {foo:{moo:'1'},bar:{far:'2'}}, 'sister objects not auto');
test(Object, ['f[]=a&f[]=b&f[]=c&f[]=d&f[]=e&f[]=f',{deep:true}], { f: ['a','b','c','d','e','f'] }, 'large array');
test(Object, ['foo[][]=a&foo[][]=b',{deep:true}], {foo:[['a'],['b']]}, 'nested arrays separate');
test(Object, ['foo[][]=3&foo[][]=4',{deep:true}], {foo:[[3],[4]]}, 'nested arrays together');
test(Object, ['foo[][]=3&foo[][]=4',{deep:true,auto:false}], {foo:[['3'],['4']]}, 'nested arrays together not auto');
var qs = 'foo[cap][map]=true&foo[cap][pap]=false';
test(Object, [qs,{deep:true}], {foo:{cap:{map:true,pap:false}}}, 'nested boolean not auto');
test(Object, [qs,{deep:true,auto:false}], {foo:{cap:{ map:'true',pap:'false'}}}, 'nested boolean auto');
test(Object, ['foo[3]=hardy&foo[10]=har har', {deep:true}], {foo:{3:'hardy',10:'har har'}}, 'array keys will construct object');
test(Object, ['text=What%20is%20going%20on%20here%3f%3f&url=http://animalsbeingdicks.com/page/2'], { text: 'What is going on here??', url: 'http://animalsbeingdicks.com/page/2' }, 'handles partially escaped params');
test(Object, ['text=What%20is%20going%20on%20here%3f%3f&url=http%3A%2F%2Fanimalsbeingdicks.com%2Fpage%2F2'], { text: 'What is going on here??', url: 'http://animalsbeingdicks.com/page/2' }, 'handles fully escaped params');
test(Object, ['foo%3Dbar=car'], {'foo=bar':'car'}, 'handles = in encoded keys');
test(Object, ['foo%2Cbar=car'], {'foo,bar':'car'}, 'handles , in encoded keys');
test(Object, ['foo=bar%3Dcar'], {'foo':'bar=car'}, 'handles = in encoded values');
test(Object, ['foo=bar%2Ccar'], {'foo':'bar,car'}, 'handles , in encoded values');
test(Object, ['url=http%3A%2F%2Fwww.site.com%2Fslug%3Fin%3D%2Fuser%2Fjoeyblake'], { url: 'http://www.site.com/slug?in=/user/joeyblake' }, 'equal must be escaped as well');
test(Object, ['http://fake.com?foo=bar'], { foo: 'bar' }, 'handles whole URLs');
test(Object, {}, 'will not die if no arguments');
if (typeof window !== 'undefined') {
equal(typeof run(Object, 'fromQueryString', [window.location]), 'object', 'can handle just window.location');
}
// Automatic casting
test(Object, ['foo=3.14156'], { foo: 3.14156 }, 'float values');
test(Object, ['foo=3.14156', {auto:false}], { foo: '3.14156' }, 'float values not automatic');
test(Object, ['foo=127.0.0.1'], { foo: '127.0.0.1' }, 'IP addresses not treated as numbers');
test(Object, ['zip=00165'], { zip: 165 }, 'zipcodes are treated as numbers if auto');
test(Object, ['zip=00165',{auto:false}], { zip: '00165' }, 'zipcodes are not treated as numbers if not auto');
test(Object, ['foo[=bar'], { 'foo[': 'bar' }, 'opening bracket does not trigger deep parameters');
test(Object, ['foo='], { foo: null }, 'auto | null');
test(Object, ['foo=0'], { foo: 0 }, 'auto | zero');
test(Object, ['foo=-0'], { foo: -0 }, 'auto | negative zero');
test(Object, ['foo=.5'], { foo: .5 }, 'auto | .5');
test(Object, ['foo=0.5'], { foo: .5 }, 'auto | 0.5');
test(Object, ['foo=0.00'], { foo: 0 }, 'auto | 0.00');
test(Object, ['foo=1'], { foo: 1 }, 'auto | 1');
test(Object, ['foo=-1'], { foo:-1 }, 'auto | -1');
test(Object, ['foo=-0.5'], { foo: -.5 }, 'auto | -0.5');
test(Object, ['foo=-.5'], { foo: -.5 }, 'auto | -.5');
test(Object, ['foo=-.0025'], { foo: -.0025 }, 'auto | -.0025');
test(Object, ['foo=-0.0025'], { foo: -.0025 }, 'auto | -0.0025');
test(Object, ['foo=.0025'], { foo: .0025 }, 'auto | .0025');
test(Object, ['foo=0.0025'], { foo: .0025 }, 'auto | 0.0025');
test(Object, ['foo=0x89'], { foo: '0x89' }, 'auto | should not cast 0x89');
test(Object, ['foo=1e25'], { foo: '1e25' }, 'auto | should not cast 1e25');
test(Object, ['foo=#fff'], { foo: '#fff' }, 'auto | should not cast #fff');
test(Object, ['foo=1.2.3'], { foo: '1.2.3'}, 'auto | should not cast 1.2.3');
test(Object, ['foo=Infinity'],{ foo: 'Infinity' }, 'auto | should not cast Infinity');
test(Object, ['foo=99,999'], { foo: '99,999' }, 'auto | should not cast numbers with commas');
test(Object, ['foo=24px'], { foo: '24px' }, 'auto | should not cast 24px');
test(Object, ['foo=5-'], { foo: '5-' }, 'auto | should not cast 5-');
test(Object, ['foo=bar&foo=car'], {'foo':['bar','car']}, 'two keys detected by auto');
test(Object, ['foo=bar&foo=car&foo=moo'], {'foo':['bar','car','moo']}, 'three keys detected by auto');
test(Object, ['foo=bar&foo=car', {deep:true}], {'foo':['bar','car']}, 'two keys detected by auto');
test(Object, ['foo=bar&foo=car&foo=moo', {deep:true}], {'foo':['bar','car','moo']}, 'three keys detected by auto');
// Separators
test(Object, ['user_name=Harry'], {'user_name':'Harry'}, 'without separator');
test(Object, ['user_name=Harry', {separator:'_'}], {'user':{name:'Harry'}}, 'with separator');
test(Object, ['user_name_first=Harry', {separator:'_'}], {'user':{name:{first:'Harry'}}}, 'with separator deeper');
test(Object, ['user|name=Harry'], {'user|name':'Harry'}, 'without separator | pipe');
test(Object, ['user|name=Harry', {separator:'|'}], {'user':{name:'Harry'}}, 'with separator | pipe');
test(Object, ['user|name|first=Harry', {separator:'|'}], {'user':{name:{first:'Harry'}}}, 'with separator deeper | pipe');
// Cast function
var toFoo = function() { return 'foo'; }
test(Object, ['foo=bar', {transform:toFoo}], {foo:'foo'}, 'transform foo');
test(Object, ['foo=3', {transform:toFoo}], {foo:'foo'}, 'transform foo before auto conversion');
test(Object, ['foo=true', {transform:toFoo}], {foo:'foo'}, 'transform foo before boolean conversion');
test(Object, ['foo[]=true', {transform:toFoo}], {'foo[]':'foo'}, 'transform foo on brackets');
var toEmpty = function() { return ''; }
test(Object, ['foo=bar', {transform:toEmpty}], {foo:''}, 'transform empty string');
var count = 0;
var testTransformArguments = function(val, key, obj, str) {
equal(val, 'bar', 'first argument should be the value');
equal(key, 'foo', 'second argument should be the key');
count++;
}
run(Object, 'fromQueryString', ['foo=bar', {transform:testTransformArguments}]);
equal(count, 1, 'should have run once');
var count = 0;
var expectedKeys = ['foo[name]', 'moo[]'];
var expectedValues = ['bar', 'beer'];
var capturedObj;
var testTransformArgumentsDeep = function(val, key, obj) {
equal(key, expectedKeys[count], 'first argument');
equal(val, expectedValues[count], 'second argument');
capturedObj = obj;
count++;
}
var result = run(Object, 'fromQueryString', ['foo[name]=bar&moo[]=beer', {transform:testTransformArgumentsDeep}]);
equal(capturedObj, result, 'third argument should be equal to the result');
equal(count, 2, 'should have run twice');
var onlyUserName = function(val, key) {
if (key === 'user_name') {
return 'Harry';
}
}
test(Object, ['user_name=moo&user_id=12345', {transform:onlyUserName}], {user_name:'Harry',user_id:12345}, 'only user name');
var numeralToBoolean = function(val) {
if (val === '1' || val === '0') {
return !!+val;
}
}
var subject = 'user[profile][agreed]=1&user[address][street]=12345%20Foo%20St.&user[profile][friends][]=Mary&user[profile][friends][]=Jerry&user[profile][paid]=0';
var expected = {
user: {
profile: {
paid: false,
agreed: true,
friends: ['Mary', 'Jerry']
},
address: {
street: '12345 Foo St.'
}
}
}
test(Object, [subject, {deep:true,transform:numeralToBoolean}], expected, 'complex object with numeral cast to boolean');
var toArray = function(val, key, obj) {
if (key === 'foo' && !obj[key]) {
return [val];
}
}
test(Object, ['foo=bar', {transform:toArray}], {'foo':['bar']}, 'single can still be converted to array with cast function');
});
method('has', function() {
test({foo:'bar'}, ['foo'], true, 'finds a property');
test({foo:'bar'}, ['baz'], false, 'does not find a nonexistant property');
test({foo:{a:'b'}}, ['foo.a'], true, 'works on deep properties');
test({foo:{a:'b'}}, ['foo[a]'], true, 'works on deep properties with bracket syntax');
test({foo:{a:'b'}}, [['foo','a']], true, 'works on deep properties with array');
test({ hasOwnProperty: true, foo: 'bar' }, ['foo'], true, 'local hasOwnProperty is ignored');
function Foo() {}
Foo.prototype.bar = 'bar';
test(new Foo, ['bar'], false, 'Does not work on not own properties by default');
test(new Foo, ['bar', true], true, 'Allows not own properties with flag');
test([], ['forEach'], false, 'Built in prototype methods | off');
test([], ['forEach', true], true, 'Built in prototype methods | off');
});
method('get', function() {
if (isExtendedMode()) {
// Object.get can never be extended
// as it conflicts with native getters.
return;
}
var obj = {
'a.b.c': 'surprise',
a: {
b: {
c: {
foo: 'bar'
},
str: 'hi',
num: 5,
und: undefined,
nul: null,
arr: [1]
},
str: 'hi',
num: 5,
und: undefined,
nul: null,
arr: [1]
},
str: 'hi',
num: 5,
und: undefined,
nul: null,
arr: [1]
};
// Array
test(obj, [['str']], 'hi', 'array | string');
test(obj, [['num']], 5, 'array | number');
test(obj, [['und']], undefined, 'array | undefined');
test(obj, [['nul']], null, 'array | null');
test(obj, [['arr']], [1], 'array | array');
test(obj, [['non']], undefined, 'array | non-existent');
test(obj, [['a','b','c','foo']], 'bar', 'array | accesses deep property');
test(obj, [['a.b.c']], 'surprise', 'array | allows key with dot notation');
test(obj, [{0:'a',1:'b',2:'c',3:'foo',length:4}], 'bar', 'array | array-like');
// Dot syntax
test(obj, ['str'], 'hi', 'flat string');
test(obj, ['num'], 5, 'flat number');
test(obj, ['und'], undefined, 'flat undefined');
test(obj, ['nul'], null, 'flat null');
test(obj, ['arr'], [1], 'flat array');
test(obj, ['non'], undefined, 'flat non-existent');
test(obj, ['a.str'], 'hi', 'one level | string');
test(obj, ['a.num'], 5, 'one level | number');
test(obj, ['a.und'], undefined, 'one level | undefined');
test(obj, ['a.nul'], null, 'one level | null');
test(obj, ['a.arr'], [1], 'one level | array');
test(obj, ['a.non'], undefined, 'one level | non-existent');
test(obj, ['a.b.str'], 'hi', 'two levels | string');
test(obj, ['a.b.num'], 5, 'two levels | number');
test(obj, ['a.b.und'], undefined, 'two levels | undefined');
test(obj, ['a.b.nul'], null, 'two levels | null');
test(obj, ['a.b.arr'], [1], 'two levels | array');
test(obj, ['a.b.non'], undefined, 'two levels | non-existent');
test(obj, ['arr.0'], 1, 'flat array property');
test(obj, ['a.arr.0'], 1, 'one level | array property');
test(obj, ['a.b.arr.0'], 1, 'two levels | array property');
test(obj, ['a.b.c'], { foo: 'bar' }, 'deep inner object');
equal(obj['a.b.c'], 'surprise', 'flat shadowing property can still be accessed');
test(obj, ['a.b.c.foo'], 'bar', 'deep');
test(obj, ['a.b.b'], undefined, 'deep last non-existent');
test(obj, ['c.b.a'], undefined, 'deep none exist');
test(obj, ['.'], undefined, 'single dot');
test({}, [], undefined, 'no arguments');
test({'undefined':1}, [undefined], undefined, 'undefined should not be coerced to string');
test({'null':1}, [null], undefined, 'null should not be coerced to string');
test({3:1}, [3], 1, 'number should be coerced to string');
test({'undefined':1}, ['undefined'], 1, '"undefined" is found');
test({'null':1}, ['null'], 1, '"null" is found');
test({'':1}, [''], 1, 'empty string as key');
test({'':{'':2}}, ['.'], 2, 'nested empty string as key');
test(Object, [undefined, 'a'], undefined, 'flat property on undefined');
test(Object, [undefined, 'a.b'], undefined, 'deep property on undefined');
test(Object, [null, 'a'], undefined, 'flat property on null');
test(Object, [null, 'a.b'], undefined, 'deep property on null');
test({}, ['a'], undefined, 'flat property on empty object');
test({}, ['a.b'], undefined, 'deep property on empty object');
test(NaN, ['a'], undefined, 'flat property on NaN');
test(NaN, ['a.b'], undefined, 'deep property on NaN');
test('', ['a'], undefined, 'flat property on empty string');
test('', ['a.b'], undefined, 'deep property on empty string');
test('foo', ['a'], undefined, 'flat property on non-empty string');
test('foo', ['a.b'], undefined, 'deep property on non-empty string');
test(['a','b'], [0], 'a', 'array property found');
test(['a','b'], [1], 'b', 'array property found');
test(['a','b'], ['0'], 'a', 'array property found by string');
test(['a','b'], ['1'], 'b', 'array property found by string');
test([{foo:'bar'}], ['0.foo'], 'bar', 'array deep property');
test({foo:['bar']}, ['foo.0'], 'bar', 'object array property');
test([[['bar']]], ['0.0.0'], 'bar', 'deep array');
test({users:{993425:{name:'Harry'}}}, ['users.993425.name'], 'Harry', 'gets ids in objects');
// Bracket syntax
test(['a'], ['[0]'], 'a', 'simple bracket');
test([['a']], ['[0][0]'], 'a', 'deep array index | 2');
test([[['a']]], ['[0][0][0]'], 'a', 'deep array index | 3');
test([[[{a:'a'}]]], ['[0][0][0].a'], 'a', 'deep array index and dot');
test([[[{a:'a'}]]], ['0[0][0].a'], 'a', 'deep array index with no brackets starting');
test([[[{a:'a'}]]], ['[-1][-1][-1].a'], 'a', 'deep array index negative');
test([], ['[0]'], undefined, 'index in empty array');
test({a:['foo','bar']}, ['a'], ['foo','bar'], 'simple prop');
test({a:['foo','bar']}, ['a[0]'], 'foo', 'index 0');
test({a:['foo','bar']}, ['a[1]'], 'bar', 'index 1');
test({a:['foo','bar']}, ['a[2]'], undefined, 'index 2');
test({a:['foo','bar']}, ['a[-1]'], 'bar', 'index -1');
test({a:['foo','bar']}, ['a[-2]'], 'foo', 'index -2');
test({a:['foo','bar']}, ['a[-3]'], undefined, 'index -3');
test({a:['foo','bar']}, ['a[]'], undefined, 'null index');
test({a:['foo','bar']}, ['a.0'], 'foo', 'index 0 | dot');
test({a:['foo','bar']}, ['a.1'], 'bar', 'index 1 | dot');
test({a:['foo','bar']}, ['a.2'], undefined, 'index 2 | dot');
test({a:['foo','bar']}, ['a.-1'], undefined, 'index -1 | dot');
test({a:['foo','bar']}, ['a.-2'], undefined, 'index -2 | dot');
test({a:['foo','bar']}, ['a.-3'], undefined, 'index -3 | dot');
test({a:[{b:'b'},{c:'c'}]}, ['a[0].b'], 'b', 'index followed by dot');
// Range syntax
test(['foo','bar','cat'], ['[0..1]'], ['foo','bar'], 'range syntax | 0..1');
test(['foo','bar','cat'], ['[1..2]'], ['bar','cat'], 'range syntax | 1..2');
test(['foo','bar','cat'], ['[1..3]'], ['bar','cat'], 'range syntax | 1..3');
test(['foo','bar','cat'], ['[0..0]'], ['foo'], 'range syntax | -1..0');
test(['foo','bar','cat'], ['[0..-1]'], ['foo','bar','cat'], 'range syntax | 0..-1');
test(['foo','bar','cat'], ['[-1..0]'], [], 'range syntax | -1..0');
test(['foo','bar','cat'], ['[-1..-1]'], ['cat'], 'range syntax | -1..-1');
test(['foo','bar','cat'], ['[-2..-1]'], ['bar','cat'], 'range syntax | -2..-1');
test(['foo','bar','cat'], ['[-3..-1]'], ['foo','bar','cat'], 'range syntax | -3..-1');
test(['foo','bar','cat'], ['[-4..-1]'], ['foo','bar','cat'], 'range syntax | -4..-1');
test(['foo','bar','cat'], ['[-4..-3]'], ['foo'], 'range syntax | -4..-3');
test(['foo','bar','cat'], ['[-5..-4]'], [], 'range syntax | -5..-4');
test(['foo','bar','cat'], ['[0..]'], ['foo','bar','cat'], 'range syntax | 0..');
test(['foo','bar','cat'], ['[..1]'], ['foo','bar'], 'range syntax | ..1');
test(['foo','bar','cat'], ['[..]'], ['foo','bar','cat'], 'range syntax | ..');
test(['foo','bar','cat'], ['..'], undefined, 'range syntax | .. should be undefined');
test({a:['foo','bar','cat']}, ['a[0..1]'], ['foo','bar'], 'range syntax | nested bracket');
test({a:{b:['foo','bar','cat']}}, ['a.b[0..1]'], ['foo','bar'], 'range syntax | dot and bracket');
test({a:{b:[{d:'final'},{d:'fight'}]}}, ['a.b[0..1].d'], ['final','fight'], 'range syntax | dot and bracket with trailing');
raisesError(function(){ run({foo:'bar'}, 'get', ['[0..1]']); }, 'range syntax | should raise error when used on object', TypeError);
var complex = [[[{x:'a'},{x:'b'},{x:'c'}],[{x:'d'},{x:'e'},{x:'f'}],[{x:'g'},{x:'h'},{x:'i'}]]];
test(complex[0], ['[0..1][0..1]'], [[{x:'a'},{x:'b'}],[{x:'d'},{x:'e'}]], 'range syntax | compound brackets');
test(complex, ['[0][0..1][0..1]'], [[{x:'a'},{x:'b'}],[{x:'d'},{x:'e'}]], 'range syntax | compound brackets in 0');
test(complex, ['[0][0..1][0..1].x'], [['a','b'],['d','e']], 'range syntax | compound brackets with trailing dot');
var tree = {
f: [{
f: [
{f:['a','b','c']},
{f:['d','e','f']},
{f:['g','h','i']}
]
}, {
f: [
{f:['j','k','l']},
{f:['m','n','o']},
{f:['p','q','r']}
]
}, {
f: [
{f:['s','t','u']},
{f:['v','w','x']},
{f:['y','z','!']}
]
}]
};
test(tree, ['f[0..1].f[0..1].f[0..1]'], [[['a','b'],['d','e']],[['j','k'],['m','n']]], 'range syntax | tree');
var Foo = function() {};
var Bar = function() { this.c = 'inst-c'; };
Foo.a = 'class-a';
Foo.prototype.a = 'foo-a';
Foo.prototype.b = 'foo-b';
Foo.prototype.c = 'foo-c';
Foo.prototype.d = {e:'e'};
Bar.prototype = new Foo;
Bar.prototype.b = 'bar-b';
var instFoo = new Foo();
var instBar = new Bar();
test(Foo, ['a'], 'class-a', 'Class method class-a');
test(Foo.prototype, ['a'], 'foo-a', 'Foo.prototype.a | has own');
test(Bar.prototype, ['a'], undefined, 'Bar.prototype.a | not own');
test(Foo.prototype, ['b'], 'foo-b', 'Foo.prototype.b | has own');
test(Bar.prototype, ['b'], 'bar-b', 'Bar.prototype.b | has own');
test(Bar.prototype, ['a', true], 'foo-a', 'Bar.prototype.a | allow not own');
test(Foo.prototype, ['c'], 'foo-c', 'Foo.prototype.c | has own');
test(Foo.prototype, ['d.e'], 'e', 'Foo.prototype.d.e | has own');
test(instFoo, ['a'], undefined, 'foo.a | not own');
test(instBar, ['a'], undefined, 'bar.a | not own');
test(instFoo, ['b'], undefined, 'foo.b | not own');
test(instBar, ['b'], undefined, 'bar.b | not own');
test(instFoo, ['c'], undefined, 'foo.c | not own');
test(instBar, ['c'], 'inst-c', 'bar.c | has own');
test(instFoo, ['d.e'], undefined, 'foo.d | not own');
test(instBar, ['d.e'], undefined, 'bar.d | not own');
test(instFoo, ['a', true], 'foo-a', 'foo.a | allow not own');
test(instBar, ['a', true], 'foo-a', 'bar.a | allow not own');
test(instFoo, ['b', true], 'foo-b', 'foo.b | allow not own');
test(instBar, ['b', true], 'bar-b', 'bar.b | allow not own');
test(instFoo, ['c', true], 'foo-c', 'foo.c | allow not own');
test(instBar, ['c', true], 'inst-c', 'bar.c | allow not own');
test(instFoo, ['d.e', true], 'e', 'foo.d.e | allow not own');
test(instBar, ['d.e', true], 'e', 'bar.d.e | allow not own');
test(Object, [Array, 'prototype.every'], Array.prototype.every, 'built in prototype');
test([], ['every'], undefined, 'built in instance | not own');
test([], ['every', true], Array.prototype.every, 'built in instance | allow not own');
if (testDefinePropertySupport) {
// Non-enumerable
var obj = {};
Object.defineProperty(obj, 'foo', {
enumerable: false,
value: 3
});
Object.defineProperty(obj, 'bar', {
enumerable: false,
value: {}
});
Object.defineProperty(obj.bar, 'car', {
enumerable: false,
value: 'hi'
});
test(Object, [obj, 'foo'], 3, 'works on non-enumerable properties');
test(Object, [obj, 'bar.car'], 'hi', 'works on deep non-enumerable properties');
}
});
method('set', function() {
if (isExtendedMode()) {
// Object.set can never be extended
// as it conflicts with native setters.
return;
}
var obj = {};
run(obj, 'set', ['foo.bar', 'car']);
equal(obj.foo.bar, 'car', 'Basic flat property is set on original object');
test({}, ['.','x'], {'':{'':'x'}}, 'single dot');
test({'':1}, ['','x'], {'':'x'}, 'empty string as key');
var obj = {};
var result = run(Object, 'set', [obj, 'foo', 'bar']);
equal(obj.foo, 'bar', 'Basic flat property is set on original object');
equal(result === obj, true, 'returned value is the original object');
var obj = {};
run(obj, 'set', ['foo.bar', 'car']);
equal(obj.foo.bar, 'car', 'Basic flat property is set on original object');
// Arrays
test({}, [['str'], 'hi'], {str:'hi'}, 'array | string');
test({}, [['num'], 5], {num:5}, 'array | number');
test({}, [['und'], undefined], {}, 'array | undefined');
test({}, [['nul'], null], {nul:null}, 'array | null');
test({}, [['arr'], [1]], {arr:[1]}, 'array | array');
test({}, [['obj'], {a:'b'}], {obj:{a:'b'}}, 'array | object');
test({}, [['a','b','c'],'foo'], {a:{b:{c:'foo'}}}, 'array | set deep property');
test({}, [['a.b.c'],'foo'], {'a.b.c':'foo'}, 'array | allows key with dot notation');
test({}, [{0:'a',1:'b',2:'c',length:3},'foo'], {a:{b:{c:'foo'}}}, 'array | array-like');
// Dot syntax
test({}, ['str', 'hi'], {str:'hi'}, 'flat | string');
test({}, ['num', 5], {num:5}, 'flat | number');
test({}, ['und', undefined], {}, 'flat | undefined is not set');
test({}, ['nul', null], {nul:null}, 'flat | null');
test({}, ['arr', [1]], {arr:[1]}, 'flat | array');
test({}, ['obj', {a:'b'}], {obj:{a:'b'}}, 'flat | object');
test({}, ['a.str', 'hi'], {a:{str:'hi'}}, 'one level | string');
test({}, ['a.num', 5], {a:{num:5}}, 'one level | number');
test({}, ['a.und', undefined], {a:{}}, 'one level | undefined is not set');
test({}, ['a.nul', null], {a:{nul:null}}, 'one level | null');
test({}, ['a.arr', [1]], {a:{arr:[1]}}, 'one level | array');
test({}, ['a.obj', {a:'b'}], {a:{obj:{a:'b'}}}, 'one level | object');
test({}, ['a.b.str', 'hi'], {a:{b:{str:'hi'}}}, 'two levels | string');
test({}, ['a.b.num', 5], {a:{b:{num:5}}}, 'two levels | number');
test({}, ['a.b.und', undefined], {a:{b:{}}}, 'two levels | undefined is not set');
test({}, ['a.b.nul', null], {a:{b:{nul:null}}}, 'two levels | null');
test({}, ['a.b.arr', [1]], {a:{b:{arr:[1]}}}, 'two levels | array');
test({}, ['a.b.obj', {a:'b'}], {a:{b:{obj:{a:'b'}}}}, 'two levels | object');
test({}, ['0', 'x'], {0:'x'}, 'numeric index on object');
test({}, ['0.foo', 'x'], {0:{foo:'x'}}, 'keyword after numeric index');
test({}, ['foo.0', 'x'], {foo:{0:'x'}}, 'numeric index after keyword');
test({}, ['foo.bar.0', 'x'], {foo:{bar:{0:'x'}}}, 'numeric index two deep');
test({}, ['foo.0.bar', 'x'], {foo:{0:{bar:'x'}}}, 'numeric index in the middle');
test({}, ['a','x'], {a:'x'}, 'flat property on empty object');
test({}, ['a.b','x'], {a:{b:'x'}}, 'deep property on empty object');
test(Object, [[], '0', 'x'], ['x'], 'numeric index on array');
test(Object, [['a','b'], 0,'x'], ['x','b'], 'array property set | 0');
test(Object, [['a','b'], 1,'x'], ['a','x'], 'array property set | 1');
test(Object, [['a','b'], '0','x'], ['x','b'], 'array property set by string | 0');
test(Object, [['a','b'], '1','x'], ['a','x'], 'array property set by string | 1');
test(Object, [[{foo:'bar'}], '0.foo', 'x'], [{foo:'x'}], 'array deep property');
test(Object, [{foo:['bar']}, 'foo.0','x'], {foo:['x']}, 'object array property');
test(Object, [[[['bar']]], '0.0.0', 'x'], [[['x']]], 'deep array');
var obj = {
a: {
b: {
c: 'bar'
}
}
};
test(testClone(obj), ['a.b.c', 'x'], {a:{b:{c:'x'}}}, 'deep');
test(testClone(obj), ['a.b.b', 'x'], {a:{b:{c:'bar',b:'x'}}}, 'deep last non-existent');
test(testClone(obj), ['c.b.a', 'x'], {a:{b:{c:'bar'}},c:{b:{a:'x'}}}, 'deep none exist');
test({}, ['.','x'], {'':{'':'x'}}, 'single dot');
test({}, [], {}, 'no arguments');
test({}, [undefined, 'x'], {}, 'undefined should be ignored');
test({}, [null, 'x'], {}, 'null should ignored');
test({}, [3, 'x'], {3:'x'}, 'number should be coerced to string');
test({3:1}, [3,'x'], {3:'x'}, 'coerced number is set');
test({'':1}, ['','x'], {'':'x'}, 'empty string as key');
test({'':{'':2}}, ['.','x'], {'':{'':'x'}}, 'nested empty string as key');
raisesError(function(){ run(Object, 'set', [undefined, 'a', 'x']); }, 'should raise error on undefined');
raisesError(function(){ run(Object, 'set', [null, 'a', 'x']); }, 'should raise error on null');
raisesError(function(){ run(Object, 'set', [NaN, 'a', 'x']); }, 'should raise error on NaN');
raisesError(function(){ run(Object, 'set', ['foo', 'a', 'x']); }, 'should raise error on string');
raisesError(function(){ run(Object, 'set', ['foo', '[0]', 'x']); }, 'should raise error on string with bracket syntax');
raisesError(function(){ run(Object, 'set', [{a:undefined}, 'a.b', 'x']); }, 'should raise error on undefined deep');
raisesError(function(){ run(Object, 'set', [{a:null}, 'a.b', 'x']); }, 'should raise error on null deep');
raisesError(function(){ run(Object, 'set', [{a:NaN}, 'a.b', 'x']); }, 'should raise error on NaN deep');
raisesError(function(){ run(Object, 'set', [{a:'foo'}, 'a.b', 'x']); }, 'should raise error on string deep');
raisesError(function(){ run(Object, 'set', [{a:'foo'}, 'a[0]', 'x']); }, 'should raise error on string deep with bracket syntax');
test({}, ['users.993425.name', 'Harry'], {users:{993425:{name:'Harry'}}}, 'allows IDs as strings');
var sparse = testGetSparseArray;
// Bracket syntax
test([], ['[0]','foo'], ['foo'], 'setting index 0 of array');
test([], ['[1]','foo'], testGetSparseArray(1,'foo'), 'setting index 1 of array');
test([], ['[-1]','foo'], testGetSparseArray(-1,'foo'), 'negative index set');
test([], ['[0][0]','foo'], [['foo']], 'nested index 0 0');
test([], ['[1][0]','foo'], testGetSparseArray(1,['foo']), 'nested index 1 0');
test([], ['[0][1]','foo'], [testGetSparseArray(1,'foo')], 'nested index 0 1');
test([], ['[1][1]','foo'], testGetSparseArray(1,testGetSparseArray(1, 'foo')), 'nested index 1 1');
test(['bar'], ['[0]','foo'], ['foo'], 'setting index 0 of existing');
test(['bar','car'], ['[1]','foo'], ['bar','foo'], 'setting index 1 of existing');
test(['bar'], ['[-1]','foo'], ['foo'], 'setting index -1 of existing');
test(['bar','car'], ['[-1]','foo'], ['bar','foo'], 'setting index -1 of existing');
test({}, ['f[0]','foo'], {f:['foo']}, 'setting index 0 | deep');
test({}, ['f[1]','foo'], {f:testGetSparseArray(1,'foo')}, 'setting index 1 | deep');
test({}, ['f[-1]','foo'], {f:testGetSparseArray(-1,'foo')}, 'negative index set | deep');
test({}, ['f[0][0]','foo'], {f:[['foo']]}, 'nested index 0 0 | deep');
test({}, ['f[1][0]','foo'], {f:testGetSparseArray(1,['foo'])}, 'nested index 1 0 | deep');
test({}, ['f[0][1]','foo'], {f:[testGetSparseArray(1,'foo')]}, 'nested index 0 1 | deep');
test({}, ['f[1][1]','foo'], {f:testGetSparseArray(1,testGetSparseArray(1, 'foo'))}, 'nested index 1 1 | deep');
test({}, ['f[0].x','foo'], {f:[{x:'foo'}]}, 'setting index 0 | deep with trailing');
test({}, ['f[1].x','foo'], {f:testGetSparseArray(1,{x:'foo'})}, 'setting index 1 | deep with trailing');
test({}, ['f[-1].x','foo'], {f:testGetSparseArray(-1,{x:'foo'})}, 'negative index set | deep with trailing');
test({}, ['f[0][0].x','foo'], {f:[[{x:'foo'}]]}, 'nested index 0 0 | deep with trailing');
test({}, ['f[1][0].x','foo'], {f:testGetSparseArray(1,[{x:'foo'}])}, 'nested index 1 0 | deep with trailing');
test({}, ['f[0][1].x','foo'], {f:[testGetSparseArray(1,{x:'foo'})]}, 'nested index 0 1 | deep with trailing');
test({}, ['f[1][1].x','foo'], {f:testGetSparseArray(1,testGetSparseArray(1, {x:'foo'}))}, 'nested index 1 1 | deep with trailing');
test({}, ['a.b[0].x','foo'], {a:{b:[{x:'foo'}]}}, 'setting index 0 | 2 in front and trailing');
test({}, ['a.b[1].x','foo'], {a:{b:testGetSparseArray(1, {x:'foo'})}}, 'setting index 1 | 2 in front and trailing');
test({}, ['a.b[-1].x','foo'], {a:{b:testGetSparseArray(-1, {x:'foo'})}}, 'negative index set | 2 in front and trailing');
test({}, ['a.b[0][0].x','foo'], {a:{b:[[{x:'foo'}]]}}, 'nested index 0 0 | 2 in front and trailing');
test({}, ['a.b[1][0].x','foo'], {a:{b:testGetSparseArray(1, [{x:'foo'}])}}, 'nested index 1 0 | 2 in front and trailing');
test({}, ['a.b[0][1].x','foo'], {a:{b:[testGetSparseArray(1,{x:'foo'})]}}, 'nested index 0 1 | 2 in front and trailing');
test({}, ['a.b[1][1].x','foo'], {a:{b:testGetSparseArray(1,testGetSparseArray(1,{x:'foo'}))}}, 'nested index 1 1 | 2 in front and trailing');
test({}, ['a.b[0].x[0]','foo'], {a:{b:[{x:['foo']}]}}, 'setting index 0 | 2 in front and trailing index');
test({}, ['a.b[1].x[0]','foo'], {a:{b:testGetSparseArray(1,{x:['foo']})}}, 'setting index 1 | 2 in front and trailing index');
test({}, ['a.b[-1].x[0]','foo'], {a:{b:testGetSparseArray(-1,{x:['foo']})}}, 'negative index set | 2 in front and trailing index');
test({}, ['a.b[0][0].x[0]','foo'], {a:{b:[[{x:['foo']}]]}}, 'nested index 0 0 | 2 in front and trailing index');
test({}, ['a.b[1][0].x[0]','foo'], {a:{b:testGetSparseArray(1,[{x:['foo']}])}}, 'nested index 1 0 | 2 in front and trailing index');
test({}, ['a.b[0][1].x[0]','foo'], {a:{b:[testGetSparseArray(1,{x:['foo']})]}}, 'nested index 0 1 | 2 in front and trailing index');
test({}, ['a.b[1][1].x[0]','foo'], {a:{b:testGetSparseArray(1,testGetSparseArray(1,{x:['foo']}))}}, 'nested index 1 1 | 2 in front and trailing index');
test({}, ['a.b[0].x[0].z','foo'], {a:{b:[{x:[{z:'foo'}]}]}}, 'setting index 0 | 2 in front and trailing index');
test({}, ['a.b[1].x[0].z','foo'], {a:{b:testGetSparseArray(1,{x:[{z:'foo'}]})}}, 'setting index 1 | 2 in front and trailing index');
test({}, ['a.b[-1].x[0].z','foo'], {a:{b:testGetSparseArray(-1,{x:[{z:'foo'}]})}}, 'negative index set | 2 in front and trailing index');
test({}, ['a.b[0][0].x[0].z','foo'], {a:{b:[[{x:[{z:'foo'}]}]]}}, 'nested index 0 0 | 2 in front and trailing index');
test({}, ['a.b[1][0].x[0].z','foo'], {a:{b:testGetSparseArray(1,[{x:[{z:'foo'}]}])}}, 'nested index 1 0 | 2 in front and trailing index');
test({}, ['a.b[0][1].x[0].z','foo'], {a:{b:[testGetSparseArray(1,{x:[{z:'foo'}]})]}}, 'nested index 0 1 | 2 in front and trailing index');
test({}, ['a.b[1][1].x[0].z','foo'], {a:{b:testGetSparseArray(1,testGetSparseArray(1,{x:[{z:'foo'}]}))}}, 'nested index 1 1 | 2 in front and trailing index');
test({}, ['f[0].x.y','foo'], {f:[{x:{y:'foo'}}]}, 'setting index 0 | 2 in back after index');
test({}, ['f[1].x.y','foo'], {f:testGetSparseArray(1,{x:{y:'foo'}})}, 'setting index 1 | 2 in back after index');
test({}, ['f[-1].x.y','foo'], {f:testGetSparseArray(-1, {x:{y:'foo'}})}, 'negative index set | 2 in back after index');
test({}, ['f[0][0].x.y','foo'], {f:[[{x:{y:'foo'}}]]}, 'nested index 0 0 | 2 in back after index');
test({}, ['f[1][0].x.y','foo'], {f:testGetSparseArray(1,[{x:{y:'foo'}}])}, 'nested index 1 0 | 2 in back after index');
test({}, ['f[0][1].x.y','foo'], {f:[testGetSparseArray(1,{x:{y:'foo'}})]}, 'nested index 0 1 | 2 in back after index');
test({}, ['f[1][1].x.y','foo'], {f:testGetSparseArray(1,testGetSparseArray(1,{x:{y:'foo'}}))}, 'nested index 1 1 | 2 in back after index');
test(['foo'], ['[-1]','bar'], ['bar'], 'negative index means last element in bracket syntax');
test({f:['foo']}, ['f[-1]','bar'], {f:['bar']}, 'negative index means last element in bracket syntax | deep');
var arr = ['foo'];
arr[-1] = 'bar';
test(['foo'], ['-1','bar'], arr, 'negative index can still be set without brackets');
test({f:['foo']}, ['f.-1','bar'], {f:arr}, 'negative index can still be set without brackets | deep');
// Push syntax
test([], ['[]','foo'], ['foo'], 'push | simple array');
test(['a'], ['[]','foo'], ['a','foo'], 'push | simple array push with existing');
test({x:['a']}, ['x[]', 'foo'], {x:['a','foo']}, 'push | array deep');
test({x:{y:['a']}}, ['x.y[]', 'foo'], {x:{y:['a','foo']}}, 'push | array 2 deep');
test({}, ['x[]', 'foo'], {x:['foo']}, 'push | non-existent array');
test([], ['[].x','foo'], [{x:'foo'}], 'creates namespace when trailing exists');
test([], ['[].x.y','foo'], [{x:{y:'foo'}}], 'creates namespace when 2 trailing exist');
test({}, ['a[].x.y','foo'], {a:[{x:{y:'foo'}}]}, 'creates namespace when leading exists');
// Range syntax
test([], ['[0..1]', 'wow'], ['wow','wow'], 'range');
test([], ['[0..1][0..1]', 'wow'], [['wow','wow'],['wow','wow']], 'range | nested');
test([], ['[0..1].car', 'wow'], [{car:'wow'},{car:'wow'}], 'range | trailing');
test({}, ['foo[0..1]', 'wow'], {foo:['wow','wow']}, 'range | leading');
test({}, ['foo.bar[0..1].car.far','wow'], {foo:{'bar':[{car:{far:'wow'}},{car:{far:'wow'}}]}}, 'range | complex');
test([], ['[0][0..1]', 'wow'], [['wow','wow']], 'range | leading bracket');
test([], ['[1][0..1]', 'wow'], testGetSparseArray(1, ['wow','wow']), 'range | leading bracket | 1');
test([], ['[0..1][0]', 'wow'], [['wow'],['wow']], 'range | trailing bracket');
test([], ['[0..1][1]', 'wow'], [testGetSparseArray(1, 'wow'),testGetSparseArray(1, 'wow')], 'range | trailing bracket | 1');
test([], ['[9][2][0..1][3][5]', 'wow'], sparse(9, sparse(2, [sparse(3, sparse(5, 'wow')),sparse(3, sparse(5, 'wow'))])), 'range | bracket complex');
var inner = sparse(1, {car:'wow'});
test({}, ['foo[3].bar[4..5][1].car', 'wow'], {foo:sparse(3,{bar:sparse(4,inner,inner)})}, 'range | quite complex');
// Class instances
var Foo = function() { this.a = 'a'; };
var Bar = function() { this.b = 'b'; };
Foo.prototype = new Bar;
Bar.prototype.c = 'c';
var f = new Foo();
equal(f.hasOwnProperty('a'), true, 'instance setup | a is own');
equal(f.hasOwnProperty('b'), false, 'instance setup | b is not own');
equal(f.hasOwnProperty('c'), false, 'instance setup | c is not own');
run(Object, 'set', [f, 'a', 'x']);
run(Object, 'set', [f, 'b', 'x']);
run(Object, 'set', [f, 'c', 'x']);
equal(f.hasOwnProperty('a'), true, 'a is set');
equal(f.hasOwnProperty('b'), true, 'b is set');
equal(f.hasOwnProperty('c'), true, 'c is set');
if (f.__proto__) {
equal(f.__proto__.b, 'b', 'b is shadowed');
equal(f.__proto__.c, 'c', 'c is shadowed');
}
run(Object, 'set', [Array, 'prototype.whee', 'x']);
equal(Array.prototype.whee, 'x', 'works on built-ins');
delete Array.prototype['whee'];
if (testDefinePropertySupport) {
// Non-enumerable
var obj = {};
Object.defineProperty(obj, 'foo', {
writable: true,
enumerable: false,
value: 3
});
Object.defineProperty(obj, 'bar', {
writable: true,
enumerable: false,
value: {}
});
Object.defineProperty(obj.bar, 'car', {
writable: true,
enumerable: false,
value: 'hi'
});
run(Object, 'set', [obj, 'foo', 'x']);
equal(obj.foo, 'x', 'Non-enumerable property set');
equal(obj.bar.car, 'hi', 'deep non-enumerable property exists');
run(Object, 'set', [obj, 'bar.car', 'x']);
equal(obj.bar.car, 'x', 'deep non-enumerable property set');
}
});
method('values', function() {
test({foo:'bar'}, ['bar'], 'Values should be received');
var called = false;
var fn = function(val, o) {
equal(val, 'bar', 'First argument should be value');
equal(o, obj, 'Second argument should be the object');
called = true;
}
// Issue #525
var result = [{foo:'foo'},{bar:'bar'}].map(Sugar.Object.values);
equal(result, [['foo'],['bar']], 'non-function argument should not be called');
});
method('invert', function() {
test({foo:'bar'}, [], {bar:'foo'}, 'basic invert');
test({foo:{bar:'baz'}}, [], {'[object Object]':'foo'}, 'deep objects are simply stringified');
test({foo:['bar','baz']}, [], {'bar,baz':'foo'}, 'arrays are stringified');
test({foo:1,bar:1}, [], {1:'bar'}, 'collisions are overwritten by default');
test({length:15}, [], {15:'length'}, 'works with "length"');
test({foo:1,bar:1}, [true], {1:['foo','bar']}, 'collisions allow multi with flag');
var result = [{a:1},{b:2},{c:3}].map(Sugar.Object.invert);
equal(result, [{1:'a'},{2:'b'},{3:'c'}], 'can be iterated with map');
});
method('isObject', function() {
var Person = function() {};
var p = new Person();
test({}, true, '{}');
test(new Object({}), true, 'new Object()');
test([], false, '[]');
test(new Sugar.Object(), false, 'chainable');
test(new Array(1,2,3), false, 'new Array(1,2,3)');
test(new RegExp(), false, 'new RegExp()');
test(new Date(), false, 'new Date()');
test(function() {}, false, 'function() {}');
test(1, false, '1');
test('wasabi', false, '"wasabi"');
test(NaN, false, 'NaN');
test(false, false, 'false');
test(true, false, 'true');
test(p, false, 'instance');
function Foo() {}
Foo.prototype = { foo: 3 };
test(new Foo, false, 'Object with inherited properties');
if (Object.create) {
test(Object.create(null), true, 'Object with null prototype');
}
test(Object, [null], false, 'null');
test(Object, [undefined], false, 'undefined');
});
method('isArray', function() {
test({}, false, '{}');
test([], true, '[]');
test(new Array(1,2,3), true, 'new Array(1,2,3)');
test(new RegExp(), false, 'new RegExp()');
test(new Date(), false, 'new Date()');
test(function() {}, false, 'function() {}');
test(1, false, '1');
test('wasabi', false, '"wasabi"');
test(NaN, false, 'NaN');
test(false, false, 'false');
test(true, false, 'true');
test(Object, [null], false, 'null');
test(Object, [undefined], false, 'undefined');
});
method('isBoolean', function() {
test({}, false, '{}');
test([], false, '[]');
test(new RegExp(), false, 'new RegExp()');
test(new Date(), false, 'new Date()');
test(function() {}, false, 'function() {}');
test(1, false, '1');
test('wasabi', false, '"wasabi"');
test(NaN, false, 'NaN');
test(false, true, 'false');
test(true, true, 'true');
test(Object, [null], false, 'null');
test(Object, [undefined], false, 'undefined');
});
method('isDate', function() {
test({}, false, '{}');
test([], false, '[]');
test(new RegExp(), false, 'new RegExp()');
test(new Date(), true, 'new Date()');
test(function() {}, false, 'function() {}');
test(1, false, '1');
test('wasabi', false, '"wasabi"');
test(NaN, false, 'NaN');
test(Object, [null], false, 'null');
test(Object, [undefined], false, 'undefined');
});
method('isFunction', function() {
test({}, false, '{}');
test([], false, '[]');
test(new RegExp(), false, 'new RegExp()');
test(new Date(), false, 'new Date()');
test(function() {}, true, 'function() {}');
test(new Function(), true, 'new Function()');
test(1, false, '1');
test('wasabi', false, '"wasabi"');
test(NaN, false, 'NaN');
test(false, false, 'false');
test(true, false, 'true');
test(Object, [null], false, 'null');
test(Object, [undefined], false, 'undefined');
});
method('isNumber', function() {
test({}, false, '{}');
test([], false, '[]');
test(new RegExp(), false, 'new RegExp()');
test(new Date(), false, 'new Date()');
test(function() {}, false, 'function() {}');
test(new Function(), false, 'new Function()');
test(1, true, '1');
test(0, true, '0');
test(-1, true, '-1');
test(new Number('3'), true, 'new Number("3")');
test('wasabi', false, '"wasabi"');
test(NaN, true, 'NaN');
test(false, false, 'false');
test(true, false, 'true');
test(Object, [null], false, 'null');
test(Object, [undefined], false, 'undefined');
});
method('isString', function() {
test({}, false, '{}');
test([], false, '[]');
test(new RegExp(), false, 'new RegExp()');
test(new Date(), false, 'new Date()');
test(function() {}, false, 'function() {}');
test(new Function(), false, 'new Function()');
test(1, false, '1');
test('wasabi', true, '"wasabi"');
test(new String('wasabi'), true, 'new String("wasabi")');
test(NaN, false, 'NaN');
test(false, false, 'false');
test(true, false, 'true');
test(Object, [null], false, 'null');
test(Object, [undefined], false, 'undefined');
});
method('isRegExp', function() {
test({}, false, '{}');
test([], false, '[]');
test(new RegExp(), true, 'new RegExp()');
test(/afda/, true, '/afda/');
test(new Date(), false, 'new Date()');
test(function() {}, false, 'function() {}');
test(new Function(), false, 'new Function()');
test(1, false, '1');
test('wasabi', false, '"wasabi"');
test(NaN, false, 'NaN');
test(false, false, 'false');
test(true, false, 'true');
test(Object, [null], false, 'null');
test(Object, [undefined], false, 'undefined');
});
method('isArguments', function() {
test({}, false, '{}');
test([], false, '[]');
test(new Array(1,2,3), false, 'new Array(1,2,3)');
test(new RegExp(), false, 'new RegExp()');
test(new Date(), false, 'new Date()');
test(function() {}, false, 'function() {}');
test(1, false, '1');
test('wasabi', false, '"wasabi"');
test(NaN, false, 'NaN');
test(false, false, 'false');
test(true, false, 'true');
test((function(){ return arguments; })(), true, 'arguments object with 0 length');
test((function(){ return arguments; })(1,2,3), true, 'arguments object with 3 length');
test(Object, [null], false, 'null');
test(Object, [undefined], false, 'undefined');
});
method('isError', function() {
test(new Error(), true, 'Error');
test(new TypeError(), true, 'TypeError');
test(new RangeError(), true, 'RangeError');
test(new EvalError(), true, 'EvalError');
test(new URIError(), true, 'URIError');
test(new SyntaxError(), true, 'SyntaxError');
test(new ReferenceError(), true, 'ReferenceError');
test('Error!', false, 'Error!');
});
method('isSet', function() {
if (typeof Set === 'undefined') return;
test(new Set(), true, '{}');
test(new Set(['1','2','3']), true, '{1,2,3}');
test([], false, 'Array');
test({}, false, 'Object');
});
method('isMap', function() {
if (typeof Map === 'undefined') return;
test(new Map(), true, '{}');
test([], false, 'Array');
test({}, false, 'Object');
});
method('merge', function() {
// Basic no-conflict merging
test({a:'a'}, [{b:'b'}], {a:'a',b:'b'}, 'string');
test({a:'a'}, [{b:8}], {a:'a',b:8}, 'number');
test({a:'a'}, [{b:true}], {a:'a',b:true}, 'boolean');