forked from sdawood/json-tots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.spec.js
917 lines (829 loc) · 38.7 KB
/
transform.spec.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
/* eslint-disable camelcase */
const jp = require('jsonpath');
const F = require('functional-pipelines');
const traverse = require('traverse');
const {transform, reRenderTags, applyPolicies} = require('./transform');
const document = Object.freeze({
id: 123,
title: 'Bicycle 123',
description: 'Bicycle 123 is a fabulous item that you have to spend all your money on',
updatedAt: '2017-10-13T10:37:47',
bicycleType: 'Hybrid',
brand: 'Brand-Company C',
price: 500,
color: ['Red', 'Black', 'White'],
productCategory: 'Bicycle',
inStock: true,
inStockCount: '100',
quantityOnHand: null,
relatedItems: [341, 472, 649],
tags: {
hot: {author: 'anonymousUser1', timestamp: '2016MMDDHHmmssSSS'},
seasonal: {author: 'anonymousUser2', timestamp: '2017MMDDHHmmssSSS'},
personalTransportation: {author: 'memberUser3', timestamp: '2015MMDDHHmmssSSS'},
'tag-name-with-dash': {author: 'memberUser4', timestamp: '2015MMDDHHmmssSSS'},
'tag name with spaces': {author: 'memberUser5', timestamp: '2015MMDDHHmmssSSS'},
'tag.name.with.dots': {author: 'memberUser6', timestamp: '2015MMDDHHmmssSSS'}
},
pictures: [
{
view: 'front',
images: [{big: 'http://example.com/products/123_front.jpg'}, {thumbnail: 'http://example.com/products/123_front_small.jpg'}]
},
{
view: 'rear',
images: [{big: 'http://example.com/products/123_rear.jpg'}, {thumbnail: 'http://example.com/products/123_rear_small.jpg'}]
},
{
view: 'side',
images: [{big: 'http://example.com/products/123_left_side.jpg'}, {thumbnail: 'http://example.com/products/123_left_side_small.jpg'}]
}
],
productReview: {
fiveStar: [
{
author: '[email protected]',
'first.name': 'user1',
comment: 'Excellent! Can\'t recommend it highly enough! Buy it!',
score: 5,
viewAs: '*****'
},
{
author: '[email protected]',
'first.name': 'user2',
comment: 'Do yourself a favor and buy this.',
score: 5,
viewAs: '*****'
}
],
oneStar: [
{
author: '[email protected]',
'first.name': 'user3',
comment: 'Terrible product! Do no buy this.',
score: 1,
viewAs: '*----'
}
]
},
comment: '/HOME/This product sells out quickly during the summer',
'Safety.Warning.On.Root': 'Always wear a helmet' // attribute name with `.`
});
describe('jsonpath deref and string template interpolation', () => {
const template = {
name: '{{title}} [{{description}}] http://items/{{title}}',
reviews: {
eula: 'read and agree and let us get on with it',
high: '{{productReview.fiveStar[0].comment}}',
low: '{{productReview.oneStar[0].comment}}',
disclaimer: 'Ad: {{comment}}',
version: 10,
details: null,
active: true
},
safety: '{{["Safety.Warning.On.Root"]}}',
topRaters: ['{{productReview.fiveStar[0]["first.name"]}}', '{{productReview.fiveStar[1]["first.name"]}}', '{{productReview.oneStar[0]["first.name"]}}', 10, null, true],
topTaggers: '{{tags["tag-name-with-dash"].author}} - {{tags["tag name with spaces"].author}} - {{tags["tag.name.with.dots"].author}}',
oneScore: '{{productReview..score}}', // <- * means get exactly one search results. The value is substituted as is unless the place holder is a part of a bigger string, in that case it is replaced into the string template
users: '{{..author}}',
version: 10,
details: null,
active: true
};
const expectedResult = {
name: `${document.title} [${document.description}] http://items/${document.title}`,
reviews: {
eula: 'read and agree and let us get on with it',
high: document.productReview.fiveStar[0].comment,
low: document.productReview.oneStar[0].comment,
disclaimer: `Ad: ${document.comment}`,
version: 10,
details: null,
active: true
},
safety: document['Safety.Warning.On.Root'],
topRaters: ['user1', 'user2', 'user3', 10, null, true], // array in template didn't have special semantics
topTaggers: 'memberUser4 - memberUser5 - memberUser6',
oneScore: 1,
users: '[email protected]',
version: 10,
details: null,
active: true
};
let result;
const templateClone = traverse(template).clone();
const documentClone = traverse(document).clone();
beforeEach(() => {
result = transform(templateClone)(documentClone);
});
it('does not mutate the template', () => {
expect(templateClone).toEqual(template);
});
it('does not mutate the source', () => {
expect(documentClone).toEqual(document);
});
it('handles 1..* levels of nesting, and special characters in attribute names', () => {
expect(result).toEqual(expectedResult);
});
});
describe('simple interpolation with query modifiers with [ NO ] arguments + constrains', () => {
const template = {
name: '{{title}} [{{description}}] http://items/{{title}}',
reviews: {
eula: 'read and agree and let us get on with it',
high: '{{productReview.fiveStar[0].comment}}',
low: '{{productReview.oneStar[0].comment}}',
disclaimer: 'Ad: {{comment}}'
},
safety: '{{["Safety.Warning.On.Root"]}}',
topRaters: '{{productReview.fiveStar[0]["first.name"]}} - {{productReview.fiveStar[1]["first.name"]}} - {{productReview.oneStar[0]["first.name"]}}',
topTaggers: '{{tags["tag-name-with-dash"].author}} - {{tags["tag name with spaces"].author}} - {{tags["tag.name.with.dots"].author}}',
scores: '{+{productReview..score}}', // <- * means get one or more search results. The value is substituted as is unless the place holder is a part of a bigger string, in that case it is replaced into the string template
oneScore: '{{productReview..score}}', // <- * means get exactly one search results. The value is substituted as is unless the place holder is a part of a bigger string, in that case it is replaced into the string template
users: '{+{..author}}', // take all matches
optional: '{?{["not.there"]}}', // key would vanish if value doesn't exist
optionalInContext: 'Value for not.there = {?{["not.there"]}}', // would evaluate to '' if value doesn't exist
required: '{!{["not.there"]}}' // should be set to null if value doesn't exist
};
const expectedResult = {
name: `${document.title} [${document.description}] http://items/${document.title}`,
reviews: {
eula: 'read and agree and let us get on with it',
high: document.productReview.fiveStar[0].comment,
low: document.productReview.oneStar[0].comment,
disclaimer: `Ad: ${document.comment}`
},
safety: document['Safety.Warning.On.Root'],
topRaters: 'user1 - user2 - user3',
topTaggers: 'memberUser4 - memberUser5 - memberUser6',
scores: [5, 5, 1],
oneScore: 1,
users: ['anonymousUser1', 'anonymousUser2', 'memberUser3', 'memberUser4', 'memberUser5', 'memberUser6', '[email protected]', '[email protected]', '[email protected]'],
optionalInContext: 'Value for not.there = ',
required: null
};
let result;
const templateClone = traverse(template).clone();
const documentClone = traverse(document).clone();
beforeEach(() => {
result = transform(templateClone)(documentClone);
});
it('handles 1..* levels of nesting, and special characters in attribute names', () => {
expect(result).toEqual(expectedResult);
});
it('does not mutate the template', () => {
expect(templateClone).toEqual(template);
});
it('does not mutate the source', () => {
expect(documentClone).toEqual(document);
});
});
describe('simple interpolation with query modifiers [+] with arguments + constrains [!?] with arguments', () => {
const template = {
name: '{{title}} [{{description}}] http://items/{{title}}',
// updatedAt: '{!asDate{updatedAt}}', // support removed from syntax
// inStockCount: '{!asInt{inStockCount}}', // support removed from syntax
reviews: {
eula: 'read and agree and let us get on with it',
high: '{{productReview.fiveStar[0].comment}}',
low: '{{productReview.oneStar[0].comment}}',
disclaimer: 'Ad: {{comment}}'
},
safety: '{{["Safety.Warning.On.Root"]}}',
topRaters: '{{productReview.fiveStar[0]["first.name"]}} - {{productReview.fiveStar[1]["first.name"]}} - {{productReview.oneStar[0]["first.name"]}}',
topTaggers: '{{tags["tag-name-with-dash"].author}} - {{tags["tag name with spaces"].author}} - {{tags["tag.name.with.dots"].author}}',
scores: '{+2{productReview..score}}', // <- * means get one or more search results. The value is substituted as is unless the place holder is a part of a bigger string, in that case it is replaced into the string template
oneScore: '{{productReview..score}}', // <- * means get exactly one search results. The value is substituted as is unless the place holder is a part of a bigger string, in that case it is replaced into the string template
users: '{+100{..author}}', // take all matches
top5users: '{+5{..author}}', // take n matches
skip2users: '{-2{..author}}', // take n matches
optional1: '{?=default {["not.there"]}}', // lookup from sources['default']
optional2: '{?=default:OPTIONAL DEFAULT VALUE 2 {["not.there"]}}', // use default value provided
optional2Quoted: '{?=default:"OPTIONAL DEFAULT VALUE 2" {["not.there"]}}', // use default value provided
optional3: '{?=default:optional-default-value-3 {["not.there"]}}', // use default value provided
optionalInContext1: 'Value for not.there = {?=default: {["not.there"]}}', // use default value provided ''
optionalInContext2: 'Value for not.there = {?=default:"" {["not.there"]}}', // use default value provided ''
optionalInContext3: 'Value for not.there = {?=default:default value {["not.there"]}}', // use default value provided ''
optionalInContext3Quoted: 'Value for not.there = {?=default:"default value in quotes" {["not.there"]}}', // use default value provided ''
required: '{!{["not.there"]}}', // should be set to null
required1: '{!={["not.there"]}}', // should be set to null
required2: '{!=altSource1{["not.there"]}}', // lookup value in alt-source, else null
required3: '{!=altSource1{["not.there.in.alt.source"]}}', // lookup value in alt-source, else null
required4: '{!=altSource1:ALT_VALUE_DEFAULT{["not.there.in.alt.source"]}}', // lookup value in alt-source, else default
required5: '{!=altSourceMissing{["not.there"]}}', // should be set to null
required6: '{!=altSourceMissing{["not.there.for.sure"]}}', // should be set to null
required7: '{!=altSourceMissing:ALT_VALUE_DEFAULT{["not.there.for.sure"]}}' // should be set to default
};
const expectedResult = {
name: 'Bicycle 123 [Bicycle 123 is a fabulous item that you have to spend all your money on] http://items/Bicycle 123',
oneScore: 1,
optional1: '@default::default-value',
optional2: 'OPTIONAL DEFAULT VALUE 2',
optional2Quoted: '"OPTIONAL DEFAULT VALUE 2"',
optional3: 'optional-default-value-3',
optionalInContext1: 'Value for not.there = @default::default-value',
optionalInContext2: 'Value for not.there = ""',
optionalInContext3: 'Value for not.there = default value',
optionalInContext3Quoted: 'Value for not.there = "default value in quotes"',
required: null,
required1: null,
required2: '@alt-source::alt-value',
required3: null,
required4: 'ALT_VALUE_DEFAULT',
required5: null,
required6: null,
required7: 'ALT_VALUE_DEFAULT',
reviews: {
disclaimer: 'Ad: /HOME/This product sells out quickly during the summer',
eula: 'read and agree and let us get on with it',
high: 'Excellent! Can\'t recommend it highly enough! Buy it!',
low: 'Terrible product! Do no buy this.'
},
safety: 'Always wear a helmet',
scores: [5, 5],
skip2users: ['memberUser3', 'memberUser4', 'memberUser5', 'memberUser6', '[email protected]', '[email protected]', '[email protected]'],
top5users: ['anonymousUser1', 'anonymousUser2', 'memberUser3', 'memberUser4', 'memberUser5'],
topRaters: 'user1 - user2 - user3',
topTaggers: 'memberUser4 - memberUser5 - memberUser6',
users: ['anonymousUser1', 'anonymousUser2', 'memberUser3', 'memberUser4', 'memberUser5', 'memberUser6', '[email protected]', '[email protected]', '[email protected]']
};
let result;
const templateClone = traverse(template).clone();
const documentClone = traverse(document).clone();
beforeEach(() => {
result = transform(templateClone, {
sources: {
default: {'not.there': '@default::default-value'},
altSource1: {'not.there': '@alt-source::alt-value'}
}
})(documentClone);
});
it('handles 1..* levels of nesting, and special characters in attribute names', () => {
expect(result).toEqual(expectedResult);
});
it('does not mutate the template', () => {
expect(templateClone).toEqual(template);
});
it('does not mutate the source', () => {
expect(documentClone).toEqual(document);
});
});
describe('simple interpolation with query modifiers [+] with arguments + constrains [!?] with arguments | functions pipeline', () => {
const template = {
name: '{{title} | toUpperCase } [{{description}}] http://items/{{title}}',
// updatedAt: '{!asDate{updatedAt}}',
// inStockCount: '{!asInt{inStockCount}}',
reviews: {
eula: 'read and agree and let us get on with it',
high: '{{productReview.fiveStar[0].comment}}',
low: '{{productReview.oneStar[0].comment}}',
disclaimer: 'Ad: {{comment}}'
},
expensive: '{{price} | gte:500:__}',
safety: '{{["Safety.Warning.On.Root"]} | ellipsis:15 }',
topRaters: '{{productReview.fiveStar[0]["first.name"]}} - {{productReview.fiveStar[1]["first.name"]}} - {{productReview.oneStar[0]["first.name"]}}',
topTaggers: '{{tags["tag-name-with-dash"].author}} - {{tags["tag name with spaces"].author}} - {{tags["tag.name.with.dots"].author}}',
scores: '{+2{productReview..score}}', // <- * means get one or more search results. The value is substituted as is unless the place holder is a part of a bigger string, in that case it is replaced into the string template
oneScore: '{{productReview..score}}', // <- * means get exactly one search results. The value is substituted as is unless the place holder is a part of a bigger string, in that case it is replaced into the string template
users: '{+100{..author} | take:2 }', // take all matches
top5users: '{+5{..author}}', // take n matches
optional1: '{?=default {["not.there"]}}', // lookup from sources['default']
optional2: '{?=default:OPTIONAL DEFAULT VALUE 2 {["not.there"]}}', // use default value provided
optional2Quoted: '{?=default:"OPTIONAL DEFAULT VALUE 2" {["not.there"]}}', // use default value provided
optional3: '{?=default:optional-default-value-3 {["not.there"]}}', // use default value provided
optionalInContext1: 'Value for not.there = {?=default: {["not.there"]}}', // use default value provided ''
optionalInContext2: 'Value for not.there = {?=default:"" {["not.there"]}}', // use default value provided ''
optionalInContext3: 'Value for not.there = {?=default:default value {["not.there"]}}', // use default value provided ''
optionalInContext3Quoted: 'Value for not.there = {?=default:"default value in quotes" {["not.there"]}}', // use default value provided ''
required: '{!{["not.there"]}}', // should be set to null
required1: '{!={["not.there"]}}', // should be set to null
required2: '{!=altSource1{["not.there"]}}', // lookup value in alt-source, else null
required3: '{!=altSource1{["not.there.in.alt.source"]}}', // lookup value in alt-source, else null
required4: '{!=altSource1:ALT_VALUE_DEFAULT{["not.there.in.alt.source"]}}', // lookup value in alt-source, else default
required5: '{!=altSourceMissing{["not.there"]}}', // should be set to null
required6: '{!=altSourceMissing{["not.there.for.sure"]}}', // should be set to null
required7: '{!=altSourceMissing:ALT_VALUE_DEFAULT{["not.there.for.sure"]}}' // should be set to default
};
const expectedResult = {
expensive: true,
name: 'BICYCLE 123 [Bicycle 123 is a fabulous item that you have to spend all your money on] http://items/Bicycle 123',
oneScore: 1,
optional1: '@default::default-value',
optional2: 'OPTIONAL DEFAULT VALUE 2',
optional2Quoted: '"OPTIONAL DEFAULT VALUE 2"',
optional3: 'optional-default-value-3',
optionalInContext1: 'Value for not.there = @default::default-value',
optionalInContext2: 'Value for not.there = ""',
optionalInContext3: 'Value for not.there = default value',
optionalInContext3Quoted: 'Value for not.there = "default value in quotes"',
required: null,
required1: null,
required2: '@alt-source::alt-value',
required3: null,
required4: 'ALT_VALUE_DEFAULT',
required5: null,
required6: null,
required7: 'ALT_VALUE_DEFAULT',
reviews: {
disclaimer: 'Ad: /HOME/This product sells out quickly during the summer',
eula: 'read and agree and let us get on with it',
high: 'Excellent! Can\'t recommend it highly enough! Buy it!',
low: 'Terrible product! Do no buy this.'
},
safety: 'Always wear ...',
scores: [5, 5],
top5users: ['anonymousUser1', 'anonymousUser2', 'memberUser3', 'memberUser4', 'memberUser5'],
topRaters: 'user1 - user2 - user3',
topTaggers: 'memberUser4 - memberUser5 - memberUser6',
users: ['anonymousUser1', 'anonymousUser2']
};
let result;
const templateClone = traverse(template).clone();
const documentClone = traverse(document).clone();
beforeEach(() => {
result = transform(templateClone, {
sources: {
default: {'not.there': '@default::default-value'},
altSource1: {'not.there': '@alt-source::alt-value'}
},
functions: {
gte: (source, target) => target >= source // gte is in bins, overriding to illustrate __ placeholder
}
})(documentClone);
});
it('handles 1..* levels of nesting, and special characters in attribute names', () => {
expect(result).toEqual(expectedResult);
});
it('does not mutate the template', () => {
expect(templateClone).toEqual(template);
});
it('does not mutate the source', () => {
expect(documentClone).toEqual(document);
});
});
describe('inception, apply first element as `document` to n successor array elements as template, the opposite and zip-align', () => {
const template = {
name: '{{title}}',
reviews: {
high: [1, 2, 'prelude', {keyBefore: 'literal value before'}, ['a', 'b', 'c'], '{{productReview.fiveStar.length}}', '{>> {productReview.fiveStar[0]}}', {
praise: '{+{["author","comment"]}}',
stars: '{{viewAs}}'
}, {keyAfter: 'literal value after'}
],
low: ['{>* {productReview.oneStar}}',
{
criticism: '{{[(@.length - 1)].comment}}'
},
{count: '{{length}}'}
],
disclaimer: 'Ad: {{comment}}'
},
reviewsSummary: [
'{>>>{productReview}}', // use this after rendering as a scoped-document, render next n templates with it
'{+{$..score}}',
{summary: {fiveStar: '{{fiveStar.length}}', oneStar: '{{oneStar.length}}'}}
], // render next n nodes with leading rendered item as scoped-document
views: ['{%% {pictures}}', '[{{view}}]({{images.length}})'], // for-each item in enumerable scoped-document, render with next node
twoimages: ['{%2 | + {pictures..images}}', 'front -> {{[1].thumbnail}}', 'rear -> {{[1].thumbnail}}', 'side -> {?=default:Not Available{[1].thumbnail}}'], // zip-align
images: ['{%* | + {pictures..images}}', 'front -> {{[1].thumbnail}}', 'rear -> {{[1].thumbnail}}', 'side -> {{[1].thumbnail}}'], // zip-align
recursive3: ['{.3{productReview}}', '{{fiveStar}}', '{{[(@.length - 1)]}}', '{{comment}}', '{{description}}'],
recursive2: ['{.2{productReview}}', '{{fiveStar}}', '{{[(@.length - 1)]}}', '{{comment}}', '{{description}}']
};
const expectedResult = {
name: 'Bicycle 123',
reviews: {
high: [1, 2, 'prelude', {keyBefore: 'literal value before'}, ['a', 'b', 'c'], 2, {
praise: ['[email protected]', 'Excellent! Can\'t recommend it highly enough! Buy it!'],
stars: '*****'
}, {keyAfter: 'literal value after'}],
low: [{criticism: 'Terrible product! Do no buy this.'}, {count: 1}],
disclaimer: 'Ad: /HOME/This product sells out quickly during the summer'
},
reviewsSummary: [[5, 5, 1], {summary: {fiveStar: 2, oneStar: 1}}],
views: ['[front](2)', '[rear](2)', '[side](2)'],
twoimages: ['front -> http://example.com/products/123_front_small.jpg', 'rear -> http://example.com/products/123_rear_small.jpg', 'side -> Not Available'],
images: ['front -> http://example.com/products/123_front_small.jpg', 'rear -> http://example.com/products/123_rear_small.jpg', 'side -> http://example.com/products/123_left_side_small.jpg'],
recursive3: [document.productReview.fiveStar[1].comment, document.description],
recursive2: [document.productReview.fiveStar[1], document.comment, document.description]
};
let result;
const templateClone = traverse(template).clone();
beforeEach(() => {
result = transform(templateClone)(document);
});
it('renders each array elements using the nested template, supporting straightforward enumeration', () => {
expect(result).toEqual(expectedResult);
});
it('does not mutate the template', () => {
expect(templateClone).toEqual(template);
});
});
describe('flatten and doubleFlatten pipes`} | * | ** }`', () => {
const template = {
allReviews: '{+{..productReview["fiveStar","oneStar"]}|*}',
fiveStar: '{+{..fiveStar}|*}' // example common use-case: multiple results for jsonpath.query are in an array, fiveStar item is also an array
};
const expectedResult = {
allReviews: [{
author: '[email protected]',
comment: 'Excellent! Can\'t recommend it highly enough! Buy it!',
'first.name': 'user1',
score: 5,
viewAs: '*****'
}, {
author: '[email protected]',
comment: 'Do yourself a favor and buy this.',
'first.name': 'user2',
score: 5,
viewAs: '*****'
}, {
author: '[email protected]',
comment: 'Terrible product! Do no buy this.',
'first.name': 'user3',
score: 1,
viewAs: '*----'
}],
fiveStar: [{
author: '[email protected]',
comment: 'Excellent! Can\'t recommend it highly enough! Buy it!',
'first.name': 'user1',
score: 5,
viewAs: '*****'
}, {
author: '[email protected]',
comment: 'Do yourself a favor and buy this.',
'first.name': 'user2',
score: 5,
viewAs: '*****'
}]
};
let result;
const templateClone = traverse(template).clone();
beforeEach(() => {
result = transform(templateClone)(document);
});
it('renders each array elements using the nested template, supporting straightforward enumeration', () => {
expect(result).toEqual(expectedResult);
});
it('does not mutate the template', () => {
expect(templateClone).toEqual(template);
});
});
describe('tagging with or without label', () => {
const template = {
a: {
b: {
c: '{#{title}}', // select this node's path, update value into tags under $.a.b.c
d: '{#id{id}}'
}
}
};
const expectedResult = {a: {b: {c: 'Bicycle 123', d: 123}}};
let result;
const templateClone = traverse(template).clone();
const tags = {};
beforeEach(() => {
result = transform(templateClone, {tags})(document);
});
it('renders each array elements using the nested template, supporting straightforward enumeration', () => {
expect(result).toEqual(expectedResult);
});
it('sets the tagged values into tags either by label or path', () => {
expect(tags).toEqual({title: 'Bicycle 123', id: 123}); // tag with no name uses the tagged node's path
});
it('does not mutate the template', () => {
expect(templateClone).toEqual(template);
});
});
describe('@function expression node, with pipes and args node', () => {
const helloWorld = () => 'hello world';
const template = {
updateAt: '@now',
age: '@since',
stockSummary: '@stock',
id: '@uuid | ellipsis:10',
expensive: '{{price} | gte:500:__}',
pictures: '{+{pictures..thumbnail} | stringify}',
LDPictures: '{{pictures} | stringify:__:null:0}',
injectedFunction: helloWorld,
echo: '{{id} | echoArgs:1:1000:0.5:100.99:true:false:null:undefined:__}' // literal args are parsed for you
};
// args keys are either functionName (if used only once), functionKey (if globally unique) or functionPath which is unique but ugliest option to write
const args = {
age: [{path: '$.updatedAt'}],
stockSummary: [
{path: '$.inStock'},
{path: '$.inStockCount'},
{path: '$.quantityOnHand'},
{value: 100},
1000
]
};
const gte = (source, target) => target >= source; // builtin gte is higher-order function, overriding with arity-2 function to illustrate __ placeholder
const now = () => '2018-09-11T00:20:08.411Z';
const since = previous => `Now: [2018-09-11T00:20:08.411Z], last update: ${previous}`;
const stock = (...args) => args.join('--');
const uuid = () => '4213ad4f-a2b3-4c02-8133-f89019eb6093'; // override for mocking/testing
const echoArgs = (...args) => F.reduced(args);
const expectedResult = {
age: 'Now: [2018-09-11T00:20:08.411Z], last update: 2017-10-13T10:37:47',
id: '4213ad4...',
stockSummary: 'true--100----100--1000',
updateAt: '2018-09-11T00:20:08.411Z',
expensive: true,
pictures: '[\n "http://example.com/products/123_front_small.jpg",\n "http://example.com/products/123_rear_small.jpg",\n "http://example.com/products/123_left_side_small.jpg"\n]',
LDPictures: '[{"view":"front","images":[{"big":"http://example.com/products/123_front.jpg"},{"thumbnail":"http://example.com/products/123_front_small.jpg"}]},{"view":"rear","images":[{"big":"http://example.com/products/123_rear.jpg"},{"thumbnail":"http://example.com/products/123_rear_small.jpg"}]},{"view":"side","images":[{"big":"http://example.com/products/123_left_side.jpg"},{"thumbnail":"http://example.com/products/123_left_side_small.jpg"}]}]',
injectedFunction: 'hello world',
echo: [1, 1000, 0.5, 100.99, true, false, null, undefined, 123]
};
let result;
const templateClone = traverse(template).clone();
beforeEach(() => {
result = transform(templateClone, {functions: {now, since, stock, uuid, gte, echoArgs}, args})(document);
});
it('renders each array elements using the nested template, supporting straightforward enumeration', () => {
expect(result).toEqual(expectedResult);
});
it('does not mutate the template', () => {
expect(templateClone).toEqual(template);
});
});
describe('scenario: tags string-templates into tags mapping', () => {
const template = {
a: '{ #$ {id}} { # {title}}',
b: {c: '{ #updatedAt {updatedAt}} is equivalent to', d: '{ # {updatedAt}}', e: '{ #$ {brand}}'},
f: ['{ # {price}}']
};
const tags = {};
const expectedTags = {
'$.a': 123,
'$.b.e': 'Brand-Company C',
price: 500,
title: 'Bicycle 123',
updatedAt: '2017-10-13T10:37:47'
};
it('works: inserts tags as keys into the tags mapping with dereferences value as value and contextRef as ctx', () => {
const result = transform(template, {tags})(document);
const expectedResult = {
a: '123 Bicycle 123',
b: {c: '2017-10-13T10:37:47 is equivalent to', d: '2017-10-13T10:37:47', e: 'Brand-Company C'},
f: [500]
};
expect(result).toEqual(expectedResult);
expect(tags).toEqual(expectedTags);
});
});
describe('scenario: self reference staged transform 1', () => {
const template = {
a: '{ #$ {id}} { # {title}}',
f: ['{ # {price}}'],
h: '{@price{$}}',
b: {c: '{ #updatedAt {updatedAt}} is equivalent to', d: '{ # {updatedAt}}', e: '{ #$ {brand}}'},
g: '{#{color[0]}}',
i: '{@color[0]{$}} from {{id}}'
};
const tags = {};
const expectedTags = {
'$.a': 123,
'$.b.e': 'Brand-Company C',
price: 500,
title: 'Bicycle 123',
updatedAt: '2017-10-13T10:37:47',
'color[0]': 'Red'
};
it('works: 1', () => {
const result = transform(template, {tags})(document);
const expectedResult = {
a: '123 Bicycle 123',
b: {c: '2017-10-13T10:37:47 is equivalent to', d: '2017-10-13T10:37:47', e: 'Brand-Company C'},
f: [500],
g: 'Red',
h: 500,
i: 'Red from 123'
};
expect(result).toEqual(expectedResult);
expect(tags).toEqual(expectedTags);
});
});
describe('scenario: self reference staged transform 2', () => {
const template = {
a: '{ #$ {id}} { # {title}}',
f: ['{ # {price}}'],
h: '{@price{$}}',
b: {c: '{ #updatedAt {updatedAt}} is equivalent to', d: '{ # {updatedAt}}', e: '{ #$ {brand}}'},
i: '{@color[0]{$}} from {{id}}',
g: '{#{color[0]}}'
};
const tags = {};
const expectedTags = {
'$.a': 123,
'$.b.e': 'Brand-Company C',
price: 500,
title: 'Bicycle 123',
updatedAt: '2017-10-13T10:37:47',
'color[0]': 'Red'
};
it('works: 2', () => {
const result = transform(template, {tags})(document);
const expectedResult = {
a: '123 Bicycle 123',
b: {c: '2017-10-13T10:37:47 is equivalent to', d: '2017-10-13T10:37:47', e: 'Brand-Company C'},
f: [500],
g: 'Red',
h: 500,
i: '{@color[0]{$}} from 123'
};
expect(result).toEqual(expectedResult);
expect(tags).toEqual(expectedTags);
});
});
describe('scenario: self reference staged transform 3', () => {
const template = {
a: '123 Bicycle 123',
b: {c: '2017-10-13T10:37:47 is equivalent to', d: '2017-10-13T10:37:47', e: 'Brand-Company C'},
f: [500],
g: 'Red',
h: '{@price{$}}',
i: '{@color[0]{$}} from 123',
x: {author: 'anonymousUser1', timestamp: '2016MMDDHHmmssSSS'},
z: '2016MMDDHHmmssSSS'
};
const sources = {'@@next': []};
const tags = {
'$.a': 123,
'$.b.e': 'Brand-Company C',
'color[0]': 'Red',
hotTags: {author: 'anonymousUser1', timestamp: '2016MMDDHHmmssSSS'},
price: 500,
title: 'Bicycle 123',
updatedAt: '2017-10-13T10:37:47'
};
it('works: 3', () => {
const result = transform(template, {sources, tags})(document);
const expectedResult = {
a: '123 Bicycle 123',
b: {c: '2017-10-13T10:37:47 is equivalent to', d: '2017-10-13T10:37:47', e: 'Brand-Company C'},
f: [500],
g: 'Red',
h: 500,
i: 'Red from 123',
x: {author: 'anonymousUser1', timestamp: '2016MMDDHHmmssSSS'},
z: '2016MMDDHHmmssSSS'
};
expect(result).toEqual(expectedResult);
});
});
describe('scenario: self reference staged transform 4', () => {
const template = {
a: '{ #$ {id}} { # {title}}',
h: '{@price{$}}',
b: {c: '{ #updatedAt {updatedAt}} is equivalent to', d: '{ # {updatedAt}}', e: '{ #$ {brand}}'},
i: '{@color[0]{$}} from {{id}}',
f: ['{ # {price}}'],
g: '{#{color[0]}}',
x: '{#hotTags{tags.hot}}',
z: '{@hotTags{timestamp}}'
};
const tags = {};
const sources = {'@@next': []};
const expectedTags = {
'$.a': 123,
'$.b.e': 'Brand-Company C',
'color[0]': 'Red',
hotTags: {author: 'anonymousUser1', timestamp: '2016MMDDHHmmssSSS'},
price: 500,
title: 'Bicycle 123',
updatedAt: '2017-10-13T10:37:47'
};
it('works: 4', () => {
const result = transform(template, {sources, tags})(document);
const expectedResult = {
a: '123 Bicycle 123',
b: {c: '2017-10-13T10:37:47 is equivalent to', d: '2017-10-13T10:37:47', e: 'Brand-Company C'},
f: [500],
g: 'Red',
h: '{@price{$}}',
i: '{@color[0]{$}} from 123',
x: {author: 'anonymousUser1', timestamp: '2016MMDDHHmmssSSS'},
z: '2016MMDDHHmmssSSS'
};
expect(result).toEqual(expectedResult);
expect(tags).toEqual(expectedTags);
expect(sources['@@next']).toEqual([
{
path: '$.i',
source: '{@color[0]{$}}',
tag: 'color[0]',
tagPath: 'color[0]',
templatePath: '$',
type: '@@tag'
}, {
path: '$.h',
source: '{@price{$}}',
tag: 'price',
tagPath: 'price',
templatePath: '$',
type: '@@tag'
}]
);
const final = reRenderTags(result, {tags, sources})(document);
expect(final).toEqual({
a: '123 Bicycle 123',
b: {c: '2017-10-13T10:37:47 is equivalent to', d: '2017-10-13T10:37:47', e: 'Brand-Company C'},
f: [500],
g: 'Red',
h: '500',
i: 'Red from 123',
x: {author: 'anonymousUser1', timestamp: '2016MMDDHHmmssSSS'},
z: '2016MMDDHHmmssSSS'
});
});
});
describe('scenario: key policies', () => {
const template = {
TAGS: {
hot: '{:policy.collapse_snake_case {tags.hot.author}}', // transplanted under new parent and child keys by policy
seasonal: '{{tags.seasonal.author}}}', // stays under the original key
personalTransportation: '{:policy.collapse_snake_case {tags.personalTransportation.author}}' // transplanted under new parent and child keys by policy
}
};
const tags = {};
const sources = {'@@next': [], policy: {collapse_snake_case: require('./extension/policy/collapse_snake_case')}};
it('works: 5', () => {
const result = transform(template, {sources, tags})(document);
const expectedResult = {
TAGS: {
hot: 'anonymousUser1',
personalTransportation: 'memberUser3',
seasonal: 'anonymousUser2}'
}
};
expect(result).toEqual(expectedResult);
expect(sources['@@next']).toEqual([
{
path: '$.TAGS.hot',
source: '{:policy.collapse_snake_case {tags.hot.author}}',
tag: 'policy.collapse_snake_case',
tagPath: 'tags.hot.author',
templatePath: '',
type: '@@policy'
}, {
path: '$.TAGS.personalTransportation',
source: '{:policy.collapse_snake_case {tags.personalTransportation.author}}',
tag: 'policy.collapse_snake_case',
tagPath: 'tags.personalTransportation.author',
templatePath: '',
type: '@@policy'
}
]);
const policyApplied = applyPolicies(result, {tags, sources})(document);
expect(policyApplied).toEqual({
TAGS: {
hot: undefined,
personalTransportation: undefined,
seasonal: 'anonymousUser2}'
},
tags_hot_author: 'anonymousUser1',
tags_personal_transportation_author: 'memberUser3'
});
});
});
describe('evaluates @foo within template "} pipes }" with extendedArgs', () => {
it('behaves exactly like an @foo within functionExpressionTemplate', () => {
const template = {
a: {
b: {
pipeWithExtendedArgs: '{{inStockCount} | @classify | stringify:__:null:0 }'
}
}
};
const functions = {
classify: (low, high, colors) => value => value <= low ? {level: 'low', colors} : value >= high ? {level: 'high', colors} : {level: 'ok', colors}
};
const argsPath = {
'$.a.b.pipeWithExtendedArgs': [
10,
80,
{path: '$.color'}
]
};
const argsKey = {
pipeWithExtendedArgs: [
10,
80,
{path: '$.color'}
]
};
const argsName = {
classify: [
10,
80,
{path: '$.color'}
]
};
const expectedResult = {a: {b: {pipeWithExtendedArgs: '{"level":"high","colors":["Red","Black","White"]}'}}};
expect(transform(template, {functions, args: argsPath})(document)).toEqual(expectedResult);
expect(transform(template, {functions, args: argsKey})(document)).toEqual(expectedResult);
expect(transform(template, {functions, args: argsName})(document)).toEqual(expectedResult);
});
});