-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
1066 lines (926 loc) · 39.5 KB
/
Copy pathtest.js
File metadata and controls
1066 lines (926 loc) · 39.5 KB
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
import {describe, it} from "node:test";
import assert from "node:assert";
import m from "./index.js";
describe("templating", () => {
describe("{{ xyz }}", () => {
it("should replace variables", () => {
const data = {name: "Bob"};
assert.equal(m("Hello {{ name }}!", data), "Hello Bob!");
});
it("should escape variables", () => {
const data = {tag: "<div>"};
assert.equal(m("Tag is {{ tag }}", data), "Tag is <div>");
});
it("should allow fallback values", () => {
assert.equal(m(`Hello {{name || "world"}}!`, {}), "Hello world!");
});
it("should allow to use 'this' or '.' to access current data", () => {
assert.equal(m(`Hello {{.}}`, "Bob"), "Hello Bob");
assert.equal(m(`Hello {{this}}`, "Bob"), "Hello Bob");
});
it("should support accessing to nested properties", () => {
const data = {
user: {
name: "Bob",
},
};
assert.equal(m(`Hello {{user.name}}`, data), "Hello Bob");
assert.equal(m(`Hello {{this.user.name}}`, data), "Hello Bob");
});
it("should support accessing items in an array", () => {
const data = {
users: [
{ name: "Bob" },
{ name: "Susan" },
],
};
assert.equal(m(`Hello {{this.users.0.name}}`, data), "Hello Bob");
assert.equal(m(`Hello {{this.users.2.name}}`, data), "Hello ");
});
});
describe("{{! xyz }}", () => {
it("should not escape variables", () => {
const data = {tag: "<div>"};
assert.equal(m("Tag is {{! tag }}", data), "Tag is <div>");
});
it("should allow fallback values", () => {
assert.equal(m(`Hello {{!name || !"<world>"}}!`, {}), "Hello <world>!");
});
});
describe("{{# xyz }}", () => {
it("should be displayed when truthy", () => {
const data = {visible: true};
assert.equal(m("{{# visible}}Yes!{{/ visible}}", data), "Yes!");
});
it("should not be visible when falsy", () => {
const data = {visible: false};
assert.equal(m("{{# visible}}Yes!{{/ visible}}", data), "");
});
it("should not be visible when null", () => {
const data = {name: null};
assert.equal(m("{{#name}}Hello {{name}}{{/name}}", data), "");
});
it("should not be visible when undefined", () => {
const data = {};
assert.equal(m("{{#name}}Hello {{name}}{{/name}}", data), "");
});
it("should parse variables inside", () => {
const data = {visible: true, name: "Bob"};
assert.equal(m("{{#visible}}Hello {{name}}!{{/visible}}", data), "Hello Bob!");
});
it("should support nested conditionals", () => {
const data = {visible1: true, visible2: true, name: "Bob"};
assert.equal(m("{{#visible1}}{{#visible2}}{{name}}{{/visible2}}{{/visible1}}", data), "Bob");
});
it("should iterate simple arrays", () => {
const data = {
items: ["1", "2", "3"],
};
assert.equal(m("{{#items}}{{.}}{{/items}}", data), "123");
assert.equal(m("{{#items}}{{this}}{{/items}}", data), "123");
});
it("should iterate arrays of objects", () => {
const data = {
items: [{name: "Susan"},{name: "Bob"}],
name: "Phil",
};
assert.equal(m("{{#items}}{{name}}-{{/items}}", data), "Susan-Bob-");
});
it("should not iterate over an empty array", () => {
const data = {
items: [],
};
assert.equal(m("List of items: {{#items}}{{.}},{{/items}}", data), "List of items: ");
});
it("should throw an error for unmatched end of section", () => {
const data = {name: "Bob"};
try {
m("{{#name}}Hello {{name}}!{{/foo}}", data);
}
catch (error) {
assert.equal(error.message, "Unmatched section end: {{/foo}}");
}
});
});
describe("{{^ xyz }}", () => {
it("should be rendered if variable is falsy", () => {
const data = {visible: false};
assert.equal(m("{{^visible}}Yeah!{{/visible}}", data), "Yeah!");
});
it("should not be rendered if variable is truthy", () => {
const data = {visible: true};
assert.equal(m("{{^visible}}Yeah!{{/visible}}", data), "");
});
});
describe("{{> xyz }}", () => {
it("should render provided partials", () => {
const partials = {
foo: "Hello World!",
};
assert.equal(m("Message: '{{> foo}}'", {}, {partials}), "Message: 'Hello World!'");
});
it("should forward context to partials", () => {
const data = {name: "Bob"};
const partials = {
foo: "Hello {{name}}!",
};
assert.equal(m("Message: '{{> foo}}'", data, {partials}), "Message: 'Hello Bob!'");
});
it("should ignore partial section if partial is not defined", () => {
assert.equal(m("Hello {{> foo}}", {}, {}), "Hello ");
});
it("should allow to provide custom context to partial", () => {
const data = {
author: {
name: "Bob",
},
};
const partials = {
foo: "Hello {{name}}!",
};
assert.equal(m("Message: '{{> foo author}}'", data, {partials}), "Message: 'Hello Bob!'");
});
it("should allow to provide keyword arguments to partials", () => {
const data = {
name: "Bob"
};
const partials = {
foo: "Hello {{userName}}!",
};
assert.equal(m("{{>foo userName=name}}", data, {partials}), "Hello Bob!");
});
it("should allow partial variables", () => {
const partials = {
foo: {
body: "Hello {{@partial.attributes.name}}!",
attributes: {
name: "Bob",
},
},
};
assert.equal(m("{{>foo}}", {}, {partials}), "Hello Bob!");
});
it("should support spread operator in partial keyword arguments", () => {
const data = {
partialArgs: {
name: "Bob",
age: 30,
},
};
const partials = {
foo: "Hello {{name}}! You are {{age}} years old.",
};
assert.equal(m("{{>foo ...partialArgs}}", data, {partials}), "Hello Bob! You are 30 years old.");
});
it("should support accessing to partial options from @partial.options variable", () => {
const partials = {
foo: "Hello {{@partial.options.name}}!",
};
assert.equal(m(`{{>foo name="Bob"}}`, {}, {partials}), "Hello Bob!");
});
it("should support accessing to partial arguments from @partial.args variable", () => {
const partials = {
foo: "Tags: {{#each @partial.args}}{{this}},{{/each}}",
};
assert.equal(m(`{{>foo "tag1" "tag2" "tag3"}}`, {}, {partials}), "Tags: tag1,tag2,tag3,");
});
it("should support subexpressions in positional arguments", () => {
const options = {
functions: {
concat: params => params.args.join(" "),
},
partials: {
foo: "Hello {{this}}!",
},
};
assert.equal(m(`{{>foo (concat "John" "Doe")}}`, {}, options), "Hello John Doe!");
});
it("should support subexpressions in key-value arguments", () => {
const options = {
functions: {
concat: params => params.args.join(" "),
},
partials: {
foo: "Hello {{this.name}}!",
},
};
assert.equal(m(`{{>foo name=(concat "John" "Doe")}}`, {}, options), "Hello John Doe!");
});
});
describe("{{>> xyz}}", () => {
it("should allow to prove a block of content to the partial", () => {
const partials = {
foo: "Hello {{@content}}!",
};
assert.equal(m("{{>>foo}}Bob{{/foo}}", {}, {partials}), "Hello Bob!");
});
});
describe("{{#each }}", () => {
it("should do nothing if value is not an array or object", () => {
assert.equal(m("x{{#each values}}{{.}}{{/each}}x", {values: null}), "xx");
assert.equal(m("x{{#each values}}{{.}}{{/each}}x", {values: []}), "xx");
assert.equal(m("x{{#each values}}{{.}}{{/each}}x", {values: {}}), "xx");
assert.equal(m("x{{#each values}}{{.}}{{/each}}x", {values: "aa"}), "xx");
});
it("should iterate over an array of items", () => {
assert.equal(m("{{#each values}}{{.}}{{/each}}", {values: ["a", "b"]}), "ab");
assert.equal(m("{{#each values}}{{this}}{{/each}}", {values: ["a", "b"]}), "ab");
assert.equal(m("{{#each values}}{{@index}}:{{.}},{{/each}}", {values: ["a", "b"]}), "0:a,1:b,");
assert.equal(m("{{#each values}}{{@index}}:{{this}},{{/each}}", {values: ["a", "b"]}), "0:a,1:b,");
});
it("should iterate over an object", () => {
assert.equal(m("{{#each values}}{{.}},{{/each}}", {values: {foo: "bar"}}), "bar,");
assert.equal(m("{{#each values}}{{@key}}:{{@value}},{{/each}}", {values: {foo: "bar"}}), "foo:bar,");
});
it("should register @first variable", () => {
assert.equal(m("{{#each values}}{{.}}:{{@first}};{{/each}}", {values: [0, 1, 2]}), "0:true;1:false;2:false;");
});
it("should register @last variable", () => {
assert.equal(m("{{#each values}}{{.}}:{{@last}};{{/each}}", {values: [0, 1, 2]}), "0:false;1:false;2:true;");
});
it("should allow to limit the number of iterations using the limit option", () => {
assert.equal(m("{{#each values limit=2}}{{.}}{{/each}}", {values: [0, 1, 2]}), "01");
});
it("should allow to change the start index using the skip option", () => {
assert.equal(m("{{#each values skip=2}}{{.}}{{/each}}", {values: [0, 1, 2, 3]}), "23");
});
it("should allow to change the start index and limit the number of iterations", () => {
assert.equal(m("{{#each values skip=1 limit=2}}{{.}}{{/each}}", {values: [0, 1, 2, 3]}), "12");
});
});
describe("{{#if }}", () => {
it("should include content if value is true", () => {
assert.equal(m("_{{#if value}}Yes!{{/if}}_", {value: true}), "_Yes!_");
});
it("should not include content if value is false", () => {
assert.equal(m("_{{#if value}}Yes!{{/if}}_", {value: false}), "__");
});
it("should read value from state variables", () => {
assert.equal(m("{{#each items}}{{#if @first}}.{{/if}}{{.}}{{/each}}", {items: [0, 1, 2]}), ".012");
});
});
describe("{{#unless }}", () => {
it("should not include content if value is true", () => {
assert.equal(m("_{{#unless value}}Yes!{{/unless}}_", {value: true}), "__");
});
it("should include content if value is false", () => {
assert.equal(m("_{{#unless value}}Yes!{{/unless}}_", {value: false}), "_Yes!_");
});
it("should read value from state variables", () => {
assert.equal(m("{{#each items}}{{.}}{{#unless @last}},{{/unless}}{{/each}}", {items: [0, 1, 2]}), "0,1,2");
});
});
describe("{{#eq }}", () => {
it("should render block if two values are equal", () => {
assert.equal(m(`{{#eq value "a"}}yes!{{/eq}}`, {value: "a"}), "yes!");
assert.equal(m(`{{#eq value true}}yes!{{/eq}}`, {value: true}), "yes!");
assert.equal(m(`{{#eq value 0}}yes!{{/eq}}`, {value: 0}), "yes!");
});
it("should not render block if two values are not equal", () => {
assert.equal(m(`{{#eq value "a"}}yes!{{/eq}}`, {value: "b"}), "");
assert.equal(m(`{{#eq value true}}yes!{{/eq}}`, {value: false}), "");
assert.equal(m(`{{#eq value 0}}yes!{{/eq}}`, {value: 1}), "");
});
});
describe("{{#ne }}", () => {
it("should not render block if two values are equal", () => {
assert.equal(m(`{{#ne value "a"}}yes!{{/ne}}`, {value: "a"}), "");
assert.equal(m(`{{#ne value true}}yes!{{/ne}}`, {value: true}), "");
assert.equal(m(`{{#ne value 0}}yes!{{/ne}}`, {value: 0}), "");
});
it("should render block if two values are not equal", () => {
assert.equal(m(`{{#ne value "a"}}yes!{{/ne}}`, {value: "b"}), "yes!");
assert.equal(m(`{{#ne value true}}yes!{{/ne}}`, {value: false}), "yes!");
assert.equal(m(`{{#ne value 0}}yes!{{/ne}}`, {value: 1}), "yes!");
});
});
describe("{{#with }}", () => {
it("should change the context of the block", () => {
const data = {
value: "no",
subdata: {
value: "yes",
},
};
assert.equal(m("result: {{#with subdata}}{{value}}{{/with}}", data), "result: yes");
});
});
describe("{{#escape}}", () => {
it("should escape provided content", () => {
assert.equal(m("{{#escape}}<b>Hello</b>{{/escape}}"), "<b>Hello</b>");
});
});
describe("{{#raw}}", () => {
it("should return raw content", () => {
assert.equal(m("{{#raw}}{{foo}}{{/raw}}", {foo: "bar"}), "{{foo}}");
});
it("should return raw content with nested sections", () => {
assert.equal(m("{{#raw}}{{#if condition}}Hello{{/if}}{{/raw}}", {}), "{{#if condition}}Hello{{/if}}");
});
it("should return raw content with nested sections and variables", () => {
assert.equal(m("{{#raw}}Hello {{name}}!{{/raw}}", {name: "Bob"}), "Hello {{name}}!");
});
it("should return raw content with nested raw sections", () => {
assert.equal(m("{{#raw}}{{#raw}}Hello {{name}}!{{/raw}}{{/raw}}", {name: "Bob"}), "{{#raw}}Hello {{name}}!{{/raw}}");
assert.equal(m("{{#raw}}#raw{{name}}/raw{{/raw}}", {name: "Bob"}), "#raw{{name}}/raw");
});
});
describe("{{#slot }}", () => {
it("should allow to register blocks of template contents", () => {
assert.equal(m(`{{#slot "name"}}Hello {{name}}!{{/slot}}{{@slot.name}}`, { name: "Bob"}), "Hello Bob!");
});
it("should overwrite existing blocks", () => {
assert.equal(m(`{{#slot "name"}}Bob{{/slot}}{{#slot "name"}}Susan{{/slot}}{{@slot.name}}`, {}), "Susan");
});
});
describe("{{#macro }}", () => {
it("should allow to register reusable chunks of content", () => {
const template = `{{#macro "foo"}}Hello {{this.name}}!{{/macro}}{{#call "foo" name="Bob"}}{{/call}}`;
assert.equal(m(template, {}), "Hello Bob!");
});
});
describe("{{#call}}", () => {
it("should allow to call registered chunks of content", () => {
const template = `{{#macro "foo"}}{{this.name}}{{/macro}}{{#call "foo" name="Bob" /}}`;
assert.equal(m(template, {}), "Bob");
});
it("should allow to pass keyword attributes to a macro", () => {
const template = `{{#macro "foo"}}{{this.key}}:{{this.value}}{{/macro}}{{#call "foo" key="foo" value="bar" /}}`;
assert.equal(m(template, {}), "foo:bar");
});
it("should allow to pass internal content as a @content state variable", () => {
const template = `{{#macro "foo"}}Hello {{@content}}{{/macro}}{{#call "foo"}}Bob{{/call}}`;
assert.equal(m(template, {}), "Hello Bob");
});
it("should allow to access to helpers/partials from parent execution", () => {
const template = `{{#macro "foo"}}{{#bar}}{{/bar}}{{/macro}}{{#call "foo" /}}`;
const options = {
helpers: {
bar: () => "Hello world!",
}
};
assert.equal(m(template, {}, options), "Hello world!");
});
});
describe("{{#customHelper }}", () => {
it("should allow to execute a simple custom helper", () => {
const options = {
helpers: {
hello: params => `Hello ${params.args[0]}!!`,
},
};
assert.equal(m("{{#hello name}}{{/hello}}", {name: "Bob"}, options), "Hello Bob!!");
});
it("should allow to provide multiple values to custom helper", () => {
const options = {
helpers: {
concat: params => params.args.join(" "),
},
};
assert.equal(m("{{#concat a b}}{{/concat}}!", {a: "hello", b: "world"}, options), "hello world!");
});
it("should allow to provide fixed arguments values", () => {
const options = {
helpers: {
customEqual: params => params.args[0] === params.args[1] ? params.fn(params.context) : "",
customEval: params => {
const values = params.args[0].split(" == ");
return values[0] === values[1] ? params.fn(params.context) : "";
},
},
};
assert.equal(m(`{{#customEqual value "yes"}}Yes!!{{/customEqual}}`, {value: "yes"}, options), "Yes!!");
assert.equal(m(`{{#customEqual value "no"}}Yes!!{{/customEqual}}`, {value: "yes"}, options), "");
assert.equal(m(`{{#customEqual value false}}Yes!!{{/customEqual}}`, {value: "yes"}, options), "");
assert.equal(m(`{{#customEval "1 == 1"}}Equal!!{{/customEval}}`, {}, options), "Equal!!");
assert.equal(m(`{{#customEval "1 == 2"}}Equal!!{{/customEval}}`, {}, options), "");
});
it("should allow to provide keyword arguments", () => {
const options = {
helpers: {
concat: params => params.args.join(params.options.delimiter || " "),
},
};
assert.equal(m(`{{#concat a b delimiter=","}}{{/concat}}`, {a: "hello", b: "world"}, options), "hello,world");
});
it("should support spread operator in helper arguments", () => {
const data = {
values: ["Hello", "World"],
};
const options = {
helpers: {
concat: params => params.args.join(" "),
},
};
assert.equal(m("{{#concat ...values}}{{/concat}}", data, options), "Hello World");
});
it("should support spread operator in helper arguments with keywords", () => {
const data = {
helperArgs: {
values: ["Hello", "World"],
delimiter: ",",
},
};
const options = {
helpers: {
concat: params => params.options.values.join(params.options.delimiter || " "),
},
};
assert.equal(m("{{#concat ...helperArgs}}{{/concat}}", data, options), "Hello,World");
});
it("should support using the spread operator for both arguments and keyword arguments", () => {
const data = {
values: ["Hello", "World"],
options: {
delimiter: ",",
},
};
const options = {
helpers: {
concat: params => params.args.join(params.options.delimiter || " "),
},
};
assert.equal(m("{{#concat ...values ...options}}{{/concat}}", data, options), "Hello,World");
});
it("should support accessing to state variables in helper params", () => {
const data = {
name: "Bob",
};
const options = {
helpers: {
greet: params => `Hello ${params.state.root.name}!`,
},
};
assert.equal(m("{{#greet}}{{/greet}}", data, options), "Hello Bob!");
});
it("should support accessing helper content using the @helper variable", () => {
const options = {
helpers: {
foo: params => params.fn({value: "bar"}),
},
};
assert.equal(m("{{#foo}}Helper name: {{@helper.name}}{{/foo}}", {}, options), "Helper name: foo");
assert.equal(m("{{#foo value=2}}Helper option: {{@helper.options.value}}{{/foo}}", {}, options), "Helper option: 2");
assert.equal(m("{{#foo}}Helper context: {{@helper.context.value}}{{/foo}}", {}, options), "Helper context: bar");
});
it("should support subexpressions as argument values", () => {
const options = {
helpers: {
foo: params => "Result is: " + params.args[0],
},
functions: {
concat: params => params.args.join(","),
},
};
assert.equal(m(`{{#foo (concat "Hello" "World")}}{{/foo}}`, {}, options), "Result is: Hello,World");
});
it("should support subexpressions as options", () => {
const options = {
helpers: {
foo: params => "Result is: " + params.options.value,
},
functions: {
concat: params => params.args.join(","),
},
};
assert.equal(m(`{{#foo value=(concat "Hello" "World")}}{{/foo}}`, {}, options), "Result is: Hello,World");
});
it("should support self-closing helpers", () => {
const options = {
helpers: {
touppercase: params => {
return params.options.value.toUpperCase();
},
},
};
assert.equal(m(`{{#touppercase value="foo" /}}`, {}, options), "FOO");
});
});
describe("{{@root}}", () => {
it("should reference the global context", () => {
assert.equal(m("{{#each values}}{{@root.key}}{{/each}}", {values: ["a", "b"], key: "c"}), "cc");
});
});
describe("{{@index}}", () => {
it("should reference current index in the array", () => {
assert.equal(m("{{#each values}}{{@index}}{{/each}}", {values: ["a", "b", "c"]}), "012");
});
});
describe("{{@key}}", () => {
it("should reference current key when looping throug an object", () => {
assert.equal(m("{{#each values}}{{@key}},{{/each}}", {values: {foo: 1, bar: 2}}), "foo,bar,");
});
});
describe("{{@value}}", () => {
it("should reference current value when looping throug an object", () => {
assert.equal(m("{{#each values}}{{@value}},{{/each}}", {values: {foo: 1, bar: 2}}), "1,2,");
});
});
describe("{{@partial}}", () => {
it("should allow accessing to rawContent passed to the partial", () => {
const options = {
partials: {
foo: `{{!@partial.rawContent}}`,
},
};
assert.equal(m("{{>>foo}}{{bar}}{{/foo}}", {}, options), "{{bar}}");
});
it("should pass an empty rawContent for non block partials", () => {
const options = {
partials: {
foo: "{{!@partial.rawContent}}",
},
};
assert.equal(m("{{>foo}}", {}, options), "");
});
});
describe("{{=function }}", () => {
const options = {
functions: {
toUpperCase: params => params.args[0].toUpperCase(),
concat: params => params.args.join(params.options.delimiter || " "),
equal: params => {
const values = params.args[0].split(" == ");
return values[0] === values[1] ? "YES" : "NO";
},
},
};
it("should allow executing a function to return a value", () => {
assert.equal(m("{{=toUpperCase value}}!!", {value: "Bob"}, options), "BOB!!");
});
it("should support multiple arguments", () => {
assert.equal(m("{{=concat a b}}", {a: "Hello", b: "World"}, options), "Hello World");
});
it("should render nothing if no function is provided", () => {
assert.equal(m("Result: {{=noop value}}", {value: "a"}), "Result: ");
});
it("should allow to provide fixed arguments values", () => {
assert.equal(m(`{{=concat "Hello" "World"}}!`, {}, options), "Hello World!");
assert.equal(m(`{{=equal "1 == 1"}}`, {}, options), "YES");
assert.equal(m(`{{=equal "1 == 2"}}`, {}, options), "NO");
});
it("should allow to execute funcions inside helpers blocks", () => {
assert.equal(m(`{{#each names}}{{=toUpperCase .}}, {{/each}}`, {names: ["bob", "susan"]}, options), "BOB, SUSAN, ");
});
it("should support keywods in function arguments", () => {
assert.equal(m(`{{=concat a b delimiter=","}}`, {a: "Hello", b: "World"}, options), "Hello,World");
});
it("should support context variables as keyword arguments", () => {
const data = {
name: "Bob",
surname: "Doe",
};
const options = {
functions: {
sayWelcome: params => {
return `Welcome, ${[params.args[0], params.options.surname || ""].filter(Boolean).join(" ")}`;
},
},
};
assert.equal(m("{{=sayWelcome name surname=surname}}", data, options), "Welcome, Bob Doe");
});
it("should support spread operator in function arguments", () => {
const data = {
values: [1, 2, 3],
};
const options = {
functions: {
sum: params => {
return params.args.reduce((a, b) => a + b, 0);
},
},
};
assert.equal(m("result={{=sum ...values}}", data, options), "result=6");
});
it("should support spread operator in function arguments with keywords", () => {
const data = {
functionArgs: {
values: [1, 2, 3],
delimiter: ",",
},
};
const options = {
functions: {
concat: params => {
return (params.options.values || []).join(params.options.delimiter || " ");
},
},
};
assert.equal(m("result={{=concat ...functionArgs}}", data, options), "result=1,2,3");
});
it("should support accessing to state variables in function params", () => {
const data = {
name: "Bob",
};
const options = {
functions: {
greet: params => `Hello ${params.state.root.name}!`,
},
};
assert.equal(m("{{=greet}}", data, options), "Hello Bob!");
});
it("should support subexpressions", () => {
const options = {
functions: {
sum: params => params.args[0] + params.args[1],
},
};
assert.equal(m("{{=sum (sum 1 1) 2}}", {}, options), "4");
});
});
describe("argument values", () => {
const options = {
functions: {
concat: params => params.args.join(params.options.delimiter || " "),
},
};
it("should support single quotes in arguments", () => {
assert.equal(m(`{{=concat 'Hello' 'World'}}`, {}, options), "Hello World");
});
});
describe("subexpressions", () => {
const options = {
functions: {
sum: params => params.args.reduce((a, b) => a + b, 0),
upper: params => params.args[0].toUpperCase(),
concat: params => params.args.join(params.options.delimiter || " "),
},
};
it("should support simple subexpressions", () => {
assert.equal(m("{{=sum (sum 3 4) 3}}", {}, options), "10");
});
it("should support nested subexpressions", () => {
assert.equal(m("{{=sum (sum 1 (sum 2 3)) 4}}", {}, options), "10");
});
it("should support subexpressions with variables", () => {
const data = {
price: 10,
tax: 2,
shipping: 3,
};
assert.equal(m("{{=sum (sum price tax) shipping}}", data, options), "15");
});
it("should support subexpressions with strings", () => {
const data = {
name: "world",
};
assert.equal(m(`{{=concat "Hello" (upper name)}}`, data, options), "Hello WORLD");
});
it("should support ubexpression with quoted strings containing spaces", () => {
assert.equal(m(`{{=concat (concat "Hello" "dear") "world"}}`, {}, options), "Hello dear world");
});
it("should support multiple subexpressions in the same call", () => {
assert.equal(m("{{=sum (sum 1 2) (sum 3 4)}}", {}, options), "10");
});
it("should support deeply nested subexpressions", () => {
assert.equal(m("{{=sum (sum (sum 1 1) (sum (sum 1 1) 2)) 1}}", {}, options), "7");
});
it("should support subexpressions inside a string argument should not be evaluated", () => {
assert.equal(m(`{{=concat "(sum 1 2)" "test"}}`, {}, options), "(sum 1 2) test");
});
});
describe("{{!-- --}}", () => {
it("should remove single-line comments", () => {
assert.equal(m("Hello {{!-- Susan --}}Bob"), "Hello Bob");
});
it("should remove multi-line comments", () => {
const template = [
"Start{{!--",
"content",
"--}}End",
];
assert.equal(m(template.join("\n")), "StartEnd");
});
it("should remove multiple comments", () => {
assert.equal(m("A{{!-- first --}}B{{!-- second --}}C"), "ABC");
});
it("should ignore all stuff between the comments tokens", () => {
assert.equal(m("Hello {{!-- {{name}} --}}Bob!"), "Hello Bob!");
});
});
});
describe("mikel", () => {
it("should be a function", () => {
assert.equal(typeof m, "function");
});
it("should return a string", () => {
const result = m("Hello {{ name }}!", {name: "Alice"});
assert.equal(typeof result, "string");
});
it("should return the same result for the same input", () => {
const template = "Hello {{ name }}!";
const data = {name: "Alice"};
const result1 = m(template, data);
const result2 = m(template, data);
assert.equal(result1, result2);
});
it("should allow to pass custom options", () => {
const options = {
helpers: {
greet: () => "Hello World!",
},
};
const result = m("{{#greet}}{{/greet}}", {}, options);
assert.equal(result, "Hello World!");
});
});
describe("mikel.create", () => {
it("should create a new instance of mikel", () => {
const mk = m.create();
assert.equal(mk("Hello {{ name }}!", {name: "Alice"}), "Hello Alice!");
});
it("should allow providing custom helpers", () => {
const mk = m.create({
helpers: {
name: () => "Bob",
},
});
assert.equal(mk("Hello {{#name}}{{/name}}!", {}), "Hello Bob!");
});
it("should allow providing custom functions", () => {
const mk = m.create({
functions: {
greet: () => "Hello World!",
},
});
assert.equal(mk("{{=greet}}", {}), "Hello World!");
});
it("should allow providing custom partials", () => {
const mk = m.create({
partials: {
foo: "Bar",
},
});
assert.equal(mk("{{>foo}}", {}), "Bar");
});
});
describe("mikel.use", () => {
it("should allow registering new helpers", () => {
const mk = m.create();
mk.use({
helpers: {
foo: params => params.args[0],
},
});
assert.equal(mk(`Hello {{#foo "bar"}}{{/foo}}`, {}), "Hello bar");
});
it("should allow registering new functions", () => {
const mk = m.create();
mk.use({
functions: {
foo: () => "bar",
},
});
assert.equal(mk("Hello {{=foo}}", {}), "Hello bar");
});
it("should allow registering new partials", () => {
const mk = m.create();
mk.use({
partials: {
foo: "bar",
},
});
assert.equal(mk("Hello {{>foo}}", {}), "Hello bar");
});
it("should allow to register initial state", () => {
const mk = m.create();
mk.use({
initialState: {
foo: "bar",
},
});
assert.equal(mk("Hello {{@foo}}", {}), "Hello bar");
});
it("should accept a function to extend mikel", () => {
const mk = m.create();
mk.use((ctx) => {
ctx.functions["foo"] = () => "bar";
});
assert.equal(mk("{{=foo}}", {}), "bar");
});
it("should allow to register pre-transforms to the template", () => {
const mk = m.create();
mk.use((ctx) => {
ctx.preTransforms.push(template => {
return "Hello " + template + "!";
});
});
assert.equal(mk("{{name}}", { name: "Bob" }), "Hello Bob!");
});
it("should allow to register post-transforms to the template", () => {
const mk = m.create();
mk.use((ctx) => {
ctx.postTransforms.push(result => {
return result.replace("Bob", "Susan");
});
});
assert.equal(mk("Hello {{name}}!", { name: "Bob" }), "Hello Susan!");
});
});
describe("mikel.tokenize", () => {
it("should tokenize a simple template", () => {
const template = "Hello {{ name }}!";
const tokens = m.tokenize(template);
assert.deepEqual(tokens, ["Hello ", " name ", "!"]);
});
it("should tokenize a template with sections", () => {
const template = "{{#if condition}}Yes{{/if}} {{name}}!";
const tokens = m.tokenize(template);
assert.deepEqual(tokens, ["", "#if condition", "Yes", "/if", " ", "name", "!"]);
});
it("should tokenize a template with partials", () => {
const template = "Hello {{>partial}}!";
const tokens = m.tokenize(template);
assert.deepEqual(tokens, ["Hello ", ">partial", "!"]);
});
it("should tokenize a template with functions", () => {
const template = "Result: {{=function arg1 arg2}}";
const tokens = m.tokenize(template);
assert.deepEqual(tokens, ["Result: ", "=function arg1 arg2", ""]);
});
it("should tokenize a template with nested sections", () => {
const template = "{{#if condition}}{{#each items}}{{.}}{{/each}}{{/if}}";
const tokens = m.tokenize(template);
assert.deepEqual(tokens, ["", "#if condition", "", "#each items", "", ".", "", "/each", "", "/if", ""]);
});
it("should tokenize a template with raw sections", () => {
const template = "{{#raw}}Hello {{name}}!{{/raw}}";
const tokens = m.tokenize(template);
assert.deepEqual(tokens, ["", "#raw", "Hello ", "name", "!", "/raw", ""]);
});
it("should tokenize a template with escape sections", () => {
const template = "{{#escape}}<b>Hello</b>{{/escape}}";
const tokens = m.tokenize(template);
assert.deepEqual(tokens, ["", "#escape", "<b>Hello</b>", "/escape", ""]);
});
it("should tokenize a template with custom helpers", () => {
const template = "{{#customHelper arg1 arg2}}Content{{/customHelper}}";
const tokens = m.tokenize(template);
assert.deepEqual(tokens, ["", "#customHelper arg1 arg2", "Content", "/customHelper", ""]);
});
});
describe("mikel.untokenize", () => {
it("should untokenize a simple array of tokens", () => {
const tokens = ["Hello ", " name ", "!"];
const template = m.untokenize(tokens);
assert.equal(template, "Hello {{ name }}!");
});