Welcome to mirror list, hosted at ThFree Co, Russian Federation.

queryable.js « utils « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bde3ea66238f285836fbd2be6959e46d8a20aa6c (plain)
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
const { inspect } = require('util')
const t = require('tap')
const Queryable = require('../../../lib/utils/queryable.js')

t.test('retrieve single nested property', async t => {
  const fixture = {
    foo: {
      bar: 'bar',
      baz: 'baz',
    },
    lorem: {
      ipsum: 'ipsum',
    },
  }
  const q = new Queryable(fixture)
  const query = 'foo.bar'
  t.strictSame(q.query(query), { [query]: 'bar' },
    'should retrieve property value when querying for dot-sep name')
})

t.test('query', async t => {
  const fixture = {
    o: 'o',
    single: [
      'item',
    ],
    w: [
      'a',
      'b',
      'c',
    ],
    list: [
      {
        name: 'first',
      },
      {
        name: 'second',
      },
    ],
    foo: {
      bar: 'bar',
      baz: 'baz',
    },
    lorem: {
      ipsum: 'ipsum',
      dolor: [
        'a',
        'b',
        'c',
        {
          sit: [
            'amet',
          ],
        },
      ],
    },
    a: [
      [
        [
          {
            b: [
              [
                {
                  c: 'd',
                },
              ],
            ],
          },
        ],
      ],
    ],
  }
  const q = new Queryable(fixture)
  t.strictSame(
    q.query(['foo.baz', 'lorem.dolor[0]']),
    {
      'foo.baz': 'baz',
      'lorem.dolor[0]': 'a',
    },
    'should retrieve property values when querying for multiple dot-sep names')
  t.strictSame(
    q.query('lorem.dolor[3].sit[0]'),
    {
      'lorem.dolor[3].sit[0]': 'amet',
    },
    'should retrieve property from nested array items')
  t.strictSame(
    q.query('a[0][0][0].b[0][0].c'),
    {
      'a[0][0][0].b[0][0].c': 'd',
    },
    'should retrieve property from deep nested array items')
  t.strictSame(
    q.query('o'),
    {
      o: 'o',
    },
    'should retrieve single level property value')
  t.strictSame(
    q.query('list.name'),
    {
      'list[0].name': 'first',
      'list[1].name': 'second',
    },
    'should automatically expand arrays')
  t.strictSame(
    q.query(['list.name']),
    {
      'list[0].name': 'first',
      'list[1].name': 'second',
    },
    'should automatically expand multiple arrays')
  t.strictSame(
    q.query('w'),
    {
      w: ['a', 'b', 'c'],
    },
    'should return arrays')
  t.strictSame(
    q.query('single'),
    {
      single: 'item',
    },
    'should return single item')
  t.strictSame(
    q.query('missing'),
    undefined,
    'should return undefined')
  t.strictSame(
    q.query('missing[bar]'),
    undefined,
    'should return undefined also')
  t.throws(() => q.query('lorem.dolor[]'),
    { code: 'EINVALIDSYNTAX' },
    'should throw if using empty brackets notation'
  )
  t.throws(() => q.query('lorem.dolor[].sit[0]'),
    { code: 'EINVALIDSYNTAX' },
    'should throw if using nested empty brackets notation'
  )

  const qq = new Queryable({
    foo: {
      bar: 'bar',
    },
  })
  t.strictSame(
    qq.query(''),
    {
      '': {
        foo: {
          bar: 'bar',
        },
      },
    },
    'should return an object with results in an empty key'
  )
})

t.test('missing key', async t => {
  const fixture = {
    foo: {
      bar: 'bar',
    },
  }
  const q = new Queryable(fixture)
  const query = 'foo.missing'
  t.equal(q.query(query), undefined,
    'should retrieve no results')
})

t.test('no data object', async t => {
  t.throws(
    () => new Queryable(),
    { code: 'ENOQUERYABLEOBJ' },
    'should throw ENOQUERYABLEOBJ error'
  )
  t.throws(
    () => new Queryable(1),
    { code: 'ENOQUERYABLEOBJ' },
    'should throw ENOQUERYABLEOBJ error'
  )
})

t.test('get values', async t => {
  const q = new Queryable({
    foo: {
      bar: 'bar',
    },
  })
  t.equal(q.get('foo.bar'), 'bar', 'should retrieve value')
  t.equal(q.get('missing'), undefined, 'should return undefined')
})

t.test('set property values', async t => {
  const fixture = {
    foo: {
      bar: 'bar',
    },
  }
  const q = new Queryable(fixture)
  q.set('foo.baz', 'baz')
  t.strictSame(
    q.toJSON(),
    {
      foo: {
        bar: 'bar',
        baz: 'baz',
      },
    },
    'should add new property and its assigned value'
  )
  q.set('foo[lorem.ipsum]', 'LOREM IPSUM')
  t.strictSame(
    q.toJSON(),
    {
      foo: {
        bar: 'bar',
        baz: 'baz',
        'lorem.ipsum': 'LOREM IPSUM',
      },
    },
    'should be able to set square brackets props'
  )
  q.set('a.b[c.d]', 'omg')
  t.strictSame(
    q.toJSON(),
    {
      foo: {
        bar: 'bar',
        baz: 'baz',
        'lorem.ipsum': 'LOREM IPSUM',
      },
      a: {
        b: {
          'c.d': 'omg',
        },
      },
    },
    'should be able to nest square brackets props'
  )
  q.set('a.b[e][f.g][1.0.0]', 'multiple')
  t.strictSame(
    q.toJSON(),
    {
      foo: {
        bar: 'bar',
        baz: 'baz',
        'lorem.ipsum': 'LOREM IPSUM',
      },
      a: {
        b: {
          'c.d': 'omg',
          e: {
            'f.g': {
              '1.0.0': 'multiple',
            },
          },
        },
      },
    },
    'should be able to nest multiple square brackets props'
  )
  q.set('a.b[e][f.g][2.0.0].author.name', 'Ruy Adorno')
  t.strictSame(
    q.toJSON(),
    {
      foo: {
        bar: 'bar',
        baz: 'baz',
        'lorem.ipsum': 'LOREM IPSUM',
      },
      a: {
        b: {
          'c.d': 'omg',
          e: {
            'f.g': {
              '1.0.0': 'multiple',
              '2.0.0': {
                author: {
                  name: 'Ruy Adorno',
                },
              },
            },
          },
        },
      },
    },
    'should be able to use dot-sep notation after square bracket props'
  )
  q.set('a.b[e][f.g][2.0.0].author[url]', 'https://npmjs.com')
  t.strictSame(
    q.toJSON(),
    {
      foo: {
        bar: 'bar',
        baz: 'baz',
        'lorem.ipsum': 'LOREM IPSUM',
      },
      a: {
        b: {
          'c.d': 'omg',
          e: {
            'f.g': {
              '1.0.0': 'multiple',
              '2.0.0': {
                author: {
                  name: 'Ruy Adorno',
                  url: 'https://npmjs.com',
                },
              },
            },
          },
        },
      },
    },
    'should be able to have multiple, separated, square brackets props'
  )
  q.set('a.b[e][f.g][2.0.0].author[foo][bar].lorem.ipsum[dolor][sit][amet].omg', 'O_O')
  t.strictSame(
    q.toJSON(),
    {
      foo: {
        bar: 'bar',
        baz: 'baz',
        'lorem.ipsum': 'LOREM IPSUM',
      },
      a: {
        b: {
          'c.d': 'omg',
          e: {
            'f.g': {
              '1.0.0': 'multiple',
              '2.0.0': {
                author: {
                  name: 'Ruy Adorno',
                  url: 'https://npmjs.com',
                  foo: {
                    bar: {
                      lorem: {
                        ipsum: {
                          dolor: {
                            sit: {
                              amet: {
                                omg: 'O_O',
                              },
                            },
                          },
                        },
                      },
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
    'many many times...'
  )
  t.throws(
    () => q.set('foo.bar.nest', 'should throw'),
    { code: 'EOVERRIDEVALUE' },
    'should throw if trying to override a literal value with an object'
  )
  q.set('foo.bar.nest', 'use the force!', { force: true })
  t.strictSame(
    q.toJSON().foo,
    {
      bar: {
        nest: 'use the force!',
      },
      baz: 'baz',
      'lorem.ipsum': 'LOREM IPSUM',
    },
    'should allow overriding literal values when using force option'
  )

  const qq = new Queryable({})
  qq.set('foo.bar.baz', 'BAZ')
  t.strictSame(
    qq.toJSON(),
    {
      foo: {
        bar: {
          baz: 'BAZ',
        },
      },
    },
    'should add new props to qq object'
  )
  qq.set('foo.bar.bario', 'bario')
  t.strictSame(
    qq.toJSON(),
    {
      foo: {
        bar: {
          baz: 'BAZ',
          bario: 'bario',
        },
      },
    },
    'should add new props to a previously existing object'
  )
  qq.set('lorem', 'lorem')
  t.strictSame(
    qq.toJSON(),
    {
      foo: {
        bar: {
          baz: 'BAZ',
          bario: 'bario',
        },
      },
      lorem: 'lorem',
    },
    'should append new props added to object later'
  )
  qq.set('foo.bar[foo.bar]', 'foo.bar.with.dots')
  t.strictSame(
    qq.toJSON(),
    {
      foo: {
        bar: {
          'foo.bar': 'foo.bar.with.dots',
          baz: 'BAZ',
          bario: 'bario',
        },
      },
      lorem: 'lorem',
    },
    'should append new props added to object later'
  )
})

t.test('set arrays', async t => {
  const q = new Queryable({})

  q.set('foo[1]', 'b')
  t.strictSame(
    q.toJSON(),
    {
      foo: [
        undefined,
        'b',
      ],
    },
    'should be able to set items in an array using index references'
  )

  q.set('foo[0]', 'a')
  t.strictSame(
    q.toJSON(),
    {
      foo: [
        'a',
        'b',
      ],
    },
    'should be able to set a previously missing item to an array'
  )

  q.set('foo[2]', 'c')
  t.strictSame(
    q.toJSON(),
    {
      foo: [
        'a',
        'b',
        'c',
      ],
    },
    'should be able to append more items to an array'
  )

  q.set('foo[2]', 'C')
  t.strictSame(
    q.toJSON(),
    {
      foo: [
        'a',
        'b',
        'C',
      ],
    },
    'should be able to override array items'
  )

  t.throws(
    () => q.set('foo[2].bar', 'bar'),
    { code: 'EOVERRIDEVALUE' },
    'should throw if trying to override an array literal item with an obj'
  )

  q.set('foo[2].bar', 'bar', { force: true })
  t.strictSame(
    q.toJSON(),
    {
      foo: [
        'a',
        'b',
        { bar: 'bar' },
      ],
    },
    'should be able to override an array string item with an obj'
  )

  q.set('foo[3].foo', 'surprise surprise, another foo')
  t.strictSame(
    q.toJSON(),
    {
      foo: [
        'a',
        'b',
        { bar: 'bar' },
        {
          foo: 'surprise surprise, another foo',
        },
      ],
    },
    'should be able to append more items to an array'
  )

  q.set('foo[3].foo', 'FOO')
  t.strictSame(
    q.toJSON(),
    {
      foo: [
        'a',
        'b',
        { bar: 'bar' },
        {
          foo: 'FOO',
        },
      ],
    },
    'should be able to override property of an obj inside an array'
  )

  const qq = new Queryable({})
  qq.set('foo[0].bar[1].baz.bario[0][0][0]', 'something')
  t.strictSame(
    qq.toJSON(),
    {
      foo: [
        {
          bar: [
            undefined,
            {
              baz: {
                bario: [[['something']]],
              },
            },
          ],
        },
      ],
    },
    'should append as many arrays as necessary'
  )
  qq.set('foo[0].bar[1].baz.bario[0][1][0]', 'something else')
  t.strictSame(
    qq.toJSON(),
    {
      foo: [
        {
          bar: [
            undefined,
            {
              baz: {
                bario: [[
                  ['something'],
                  ['something else'],
                ]],
              },
            },
          ],
        },
      ],
    },
    'should append as many arrays as necessary'
  )
  qq.set('foo', null)
  t.strictSame(
    qq.toJSON(),
    {
      foo: null,
    },
    'should be able to set a value to null'
  )
  qq.set('foo.bar', 'bar')
  t.strictSame(
    qq.toJSON(),
    {
      foo: {
        bar: 'bar',
      },
    },
    'should be able to replace a null value with properties'
  )

  const qqq = new Queryable({
    arr: [
      'a',
      'b',
    ],
  })

  qqq.set('arr[]', 'c')
  t.strictSame(
    qqq.toJSON(),
    {
      arr: [
        'a',
        'b',
        'c',
      ],
    },
    'should be able to append to array using empty bracket notation'
  )

  qqq.set('arr[].foo', 'foo')
  t.strictSame(
    qqq.toJSON(),
    {
      arr: [
        'a',
        'b',
        'c',
        {
          foo: 'foo',
        },
      ],
    },
    'should be able to append objects to array using empty bracket notation'
  )

  qqq.set('arr[].bar.name', 'BAR')
  t.strictSame(
    qqq.toJSON(),
    {
      arr: [
        'a',
        'b',
        'c',
        {
          foo: 'foo',
        },
        {
          bar: {
            name: 'BAR',
          },
        },
      ],
    },
    'should be able to append more objects to array using empty brackets'
  )

  qqq.set('foo.bar.baz[].lorem.ipsum', 'something')
  t.strictSame(
    qqq.toJSON(),
    {
      arr: [
        'a',
        'b',
        'c',
        {
          foo: 'foo',
        },
        {
          bar: {
            name: 'BAR',
          },
        },
      ],
      foo: {
        bar: {
          baz: [
            {
              lorem: {
                ipsum: 'something',
              },
            },
          ],
        },
      },
    },
    'should be able to append to array using empty brackets in nested objs'
  )

  qqq.set('foo.bar.baz[].lorem.array[]', 'new item')
  t.strictSame(
    qqq.toJSON(),
    {
      arr: [
        'a',
        'b',
        'c',
        {
          foo: 'foo',
        },
        {
          bar: {
            name: 'BAR',
          },
        },
      ],
      foo: {
        bar: {
          baz: [
            {
              lorem: {
                ipsum: 'something',
              },
            },
            {
              lorem: {
                array: [
                  'new item',
                ],
              },
            },
          ],
        },
      },
    },
    'should be able to append to array using empty brackets in nested objs'
  )

  const qqqq = new Queryable({
    arr: [
      'a',
      'b',
    ],
  })
  t.throws(
    () => qqqq.set('arr.foo', 'foo'),
    { code: 'ENOADDPROP' },
    'should throw an override error'
  )

  qqqq.set('arr.foo', 'foo', { force: true })
  t.strictSame(
    qqqq.toJSON(),
    {
      arr: {
        0: 'a',
        1: 'b',
        foo: 'foo',
      },
    },
    'should be able to override arrays with objects when using force=true'
  )

  qqqq.set('bar[]', 'item', { force: true })
  t.strictSame(
    qqqq.toJSON(),
    {
      arr: {
        0: 'a',
        1: 'b',
        foo: 'foo',
      },
      bar: [
        'item',
      ],
    },
    'should be able to create new array with item when using force=true'
  )

  qqqq.set('bar[]', 'something else', { force: true })
  t.strictSame(
    qqqq.toJSON(),
    {
      arr: {
        0: 'a',
        1: 'b',
        foo: 'foo',
      },
      bar: [
        'item',
        'something else',
      ],
    },
    'should be able to append items to arrays when using force=true'
  )

  const qqqqq = new Queryable({
    arr: [
      null,
    ],
  })
  qqqqq.set('arr[]', 'b')
  t.strictSame(
    qqqqq.toJSON(),
    {
      arr: [
        null,
        'b',
      ],
    },
    'should be able to append items with empty items'
  )
  qqqqq.set('arr[0]', 'a')
  t.strictSame(
    qqqqq.toJSON(),
    {
      arr: [
        'a',
        'b',
      ],
    },
    'should be able to replace empty items in an array'
  )
  qqqqq.set('lorem.ipsum', 3)
  t.strictSame(
    qqqqq.toJSON(),
    {
      arr: [
        'a',
        'b',
      ],
      lorem: {
        ipsum: 3,
      },
    },
    'should be able to replace empty items in an array'
  )
  t.throws(
    () => qqqqq.set('lorem[]', 4),
    { code: 'ENOAPPEND' },
    'should throw error if using empty square bracket in an non-array item'
  )
  qqqqq.set('lorem[0]', 3)
  t.strictSame(
    qqqqq.toJSON(),
    {
      arr: [
        'a',
        'b',
      ],
      lorem: {
        0: 3,
        ipsum: 3,
      },
    },
    'should be able add indexes as props when finding an object'
  )
  qqqqq.set('lorem.1', 3)
  t.strictSame(
    qqqqq.toJSON(),
    {
      arr: [
        'a',
        'b',
      ],
      lorem: {
        0: 3,
        1: 3,
        ipsum: 3,
      },
    },
    'should be able add numeric props to an obj'
  )
})

t.test('delete values', async t => {
  const q = new Queryable({
    foo: {
      bar: {
        lorem: 'lorem',
      },
    },
  })
  q.delete('foo.bar.lorem')
  t.strictSame(
    q.toJSON(),
    {
      foo: {
        bar: {},
      },
    },
    'should delete queried item'
  )
  q.delete('foo')
  t.strictSame(
    q.toJSON(),
    {},
    'should delete nested items'
  )
  q.set('foo.a.b.c[0]', 'value')
  q.delete('foo.a.b.c[0]')
  t.strictSame(
    q.toJSON(),
    {
      foo: {
        a: {
          b: {
            c: [],
          },
        },
      },
    },
    'should delete array item'
  )
  // creates an array that has an implicit empty first item
  q.set('foo.a.b.c[1][0].foo.bar[0][0]', 'value')
  q.delete('foo.a.b.c[1]')
  t.strictSame(
    q.toJSON(),
    {
      foo: {
        a: {
          b: {
            c: [null],
          },
        },
      },
    },
    'should delete array item'
  )
})

t.test('logger', async t => {
  const q = new Queryable({})
  q.set('foo.bar[0].baz', 'baz')
  t.strictSame(
    inspect(q, { depth: 10 }),
    inspect({
      foo: {
        bar: [
          {
            baz: 'baz',
          },
        ],
      },
    }, { depth: 10 }),
    'should retrieve expected data'
  )
})

t.test('bracket lovers', async t => {
  const q = new Queryable({})
  q.set('[iLoveBrackets]', 'seriously?')
  t.strictSame(
    q.toJSON(),
    {
      '[iLoveBrackets]': 'seriously?',
    },
    'should be able to set top-level props using square brackets notation'
  )

  t.equal(q.get('[iLoveBrackets]'), 'seriously?',
    'should bypass square bracket in top-level properties')

  q.set('[0]', '-.-')
  t.strictSame(
    q.toJSON(),
    {
      '[iLoveBrackets]': 'seriously?',
      '[0]': '-.-',
    },
    'any top-level item can not be parsed with square bracket notation'
  )
})