forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 5
/
comparison.js
615 lines (525 loc) · 23.4 KB
/
comparison.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
const assert = require('assert');
const hbs = require('handlebars').create();
const helpers = require('..');
helpers.comparison({ handlebars: hbs });
describe('comparison', function() {
describe('and', function() {
describe('block', function() {
it('should render a block if both values are truthy.', function() {
const fn = hbs.compile('{{#and great magnificent}}A{{else}}B{{/and}}');
assert.equal(fn({great: true, magnificent: true}), 'A');
});
it('should render the inverse block if both values are not truthy.', function() {
const fn = hbs.compile('{{#and great magnificent}}A{{else}}B{{/and}}');
assert.equal(fn({great: true, magnificent: false}), 'B');
});
});
describe('inline or subexpression', function() {
it('should render a block if both values are truthy.', function() {
const fn = hbs.compile('{{and great magnificent}}');
assert.equal(fn({great: true, magnificent: true}), 'true');
});
it('should render the inverse block if both values are not truthy.', function() {
const fn = hbs.compile('{{and great magnificent}}');
assert.equal(fn({great: true, magnificent: false}), 'false');
});
it('should work as subexpressions', function() {
const fn = hbs.compile('{{and (and a b) (and great magnificent)}}');
assert.equal(fn({great: true, magnificent: false}), 'false');
});
});
});
describe('compare', function() {
describe('errors', function() {
it('should throw an error when args are invalid', function() {
assert.throws(function() {
hbs.compile('{{#compare}}{{/compare}}')();
}, /expects 4 arguments/);
assert.throws(function() {
hbs.compile('{{#compare a b}}{{/compare}}')();
}, /expects 4 arguments/);
});
it('should throw an error when the operator is invalid', function() {
assert.throws(function() {
hbs.compile('{{#compare a "~" b}}{{/compare}}')();
});
});
});
describe('operators', function() {
describe('==', function() {
const fn = hbs.compile('{{#compare a "==" b}}A{{else}}B{{/compare}}');
it('should render the first block if `a` equals `b`', function() {
assert(fn({a: '0', b: 0}), 'A');
});
it('should render the second block if false', function() {
assert(fn({a: 'foo', b: 0}), 'B');
});
});
describe('===', function() {
const fn = hbs.compile('{{#compare a "===" b}}A{{else}}B{{/compare}}');
it('should render the first block if `a` strictly equals `b`', function() {
assert(fn({a: '1', b: '1'}), 'A');
});
it('should render the second block if false', function() {
assert(fn({a: '1', b: 1}), 'B');
});
});
describe('!=', function() {
const fn = hbs.compile('{{#compare a "!=" b}}A{{else}}B{{/compare}}');
it('should render the first block if `a` does not equal `b`', function() {
assert(fn({a: 10, b: '11'}), 'A');
});
it('should render the second block if false', function() {
assert(fn({a: 10, b: '10'}), 'B');
});
});
describe('!==', function() {
const fn = hbs.compile('{{#compare a "!==" b}}A{{else}}B{{/compare}}');
it('should render the first block if `a` does not strictly equal `b`', function() {
assert(fn({a: 10, b: 11}), 'A');
});
it('should render the second block if false', function() {
assert(fn({a: 10, b: 10}), 'B');
});
});
describe('>', function() {
const fn = hbs.compile('{{#compare a ">" b}}greater than or equal to 15{{else}}less than 15{{/compare}}');
it('should render the first block if true.', function() {
assert(fn({a: 20, b: 15}), 'greater than or equal to 15');
});
it('should render the second block if false.', function() {
assert(fn({a: 14, b: 15}), 'less than 15');
});
});
describe('<', function() {
const fn = hbs.compile('I knew it, {{#compare unicorns "<" ponies}}unicorns are just low-quality ponies!{{else}}unicorns are special!{{/compare}}');
it('should render the first block if true.', function() {
const res = fn({unicorns: 5, ponies: 6});
assert(res, 'I knew it, unicorns are just low-quality ponies!');
});
it('should render the second block if false.', function() {
const res = fn({unicorns: 7, ponies: 6});
assert(res, 'I knew it, unicorns are special!');
});
});
describe('>=', function() {
const fn = hbs.compile('{{#compare a ">=" b}}greater than or equal to 15{{else}}less than 15{{/compare}}');
it('should render the first block if true.', function() {
assert(fn({a: 20, b: 15}), 'greater than or equal to 15');
});
it('should render the first block if equal.', function() {
assert(fn({a: 15, b: 15}), 'greater than or equal to 15');
});
it('should render the second block if false.', function() {
assert(fn({a: 14, b: 15}), 'less than 15');
});
});
describe('<=', function() {
const fn = hbs.compile('{{#compare a "<=" b}}less than or equal to 10{{else}}greater than 10{{/compare}}');
it('should render the first block if true.', function() {
assert(fn({a: 10, b: 15}), 'less than or equal to 10');
});
it('should render the second block if false.', function() {
assert(fn({a: 20, b: 15}), 'greater than 10');
});
});
describe('typeof', function() {
it('should render the first block if true', function() {
const fn = hbs.compile('{{#compare obj "typeof" "object"}}A{{else}}B{{/compare}}');
assert(fn({obj: {}}), 'A');
});
});
});
});
describe('contains', function() {
it('should render a block if the condition is true.', function() {
const fn = hbs.compile('{{#contains context "C"}}A{{else}}B{{/contains}}');
assert.equal(fn({context: 'CCC'}), 'A');
});
it('should render the inverse block if false.', function() {
const fn = hbs.compile('{{#contains context "zzz"}}A{{else}}B{{/contains}}');
assert.equal(fn({context: 'CCC'}), 'B');
});
it('should work with arrays', function() {
const fn = hbs.compile('{{#contains array "a"}}A{{else}}B{{/contains}}');
assert.equal(fn({array: ['a', 'b', 'c']}), 'A');
});
it('should render the block when an index is passed:', function() {
const fn = hbs.compile('{{#contains array "a" 0}}A{{else}}B{{/contains}}');
assert.equal(fn({array: ['a', 'b', 'c']}), 'A');
});
it('should render the inverse block when false with index:', function() {
const fn = hbs.compile('{{#contains array "a" 1}}A{{else}}B{{/contains}}');
assert.equal(fn({array: ['a', 'b', 'c']}), 'B');
});
it('should not render the block when an undefined argument is passed:', function() {
const fn = hbs.compile('{{#contains array nothing}}A{{/contains}}');
assert.equal(fn({array: ['a', 'b', 'c']}), '');
});
it('should render the inverse block when an undefined argument is passed:', function() {
const fn = hbs.compile('{{#contains array nothing}}A{{else}}B{{/contains}}');
assert.equal(fn({array: ['a', 'b', 'c']}), 'B');
});
});
describe('default', function() {
it('should use the given value:', function() {
assert.equal(hbs.compile('{{default title "A"}}')({title: 'B'}), 'B');
});
it('should fallback to the default value when no value exists', function() {
assert.equal(hbs.compile('{{default title "A"}}')({title: null}), 'A');
assert.equal(hbs.compile('{{default title "A"}}')(), 'A');
});
});
describe('gt', function() {
const fn = hbs.compile('{{#gt a b}}A{{else}}B{{/gt}}');
describe('second arg', function() {
it('should render the first block if true.', function() {
assert(fn({a: 20, b: 15}), 'A');
});
it('should render the second block if equal.', function() {
assert(fn({a: 15, b: 15}), 'B');
});
it('should render the second block if false.', function() {
assert(fn({a: 14, b: 15}), 'B');
});
});
describe('compare hash', function() {
it('should not render a block if the value is not equal to a given number.', function() {
const fn = hbs.compile('{{#gt number compare=8}}A{{/gt}}');
assert.equal(fn({number: 5}), '');
});
it('should render a block if the value is greater than a given number.', function() {
const fn = hbs.compile('{{#gt number compare=8}}A{{/gt}}');
assert.equal(fn({number: 10}), 'A');
});
it('should not render a block if the value is less than a given number.', function() {
const fn = hbs.compile('{{#gt number compare=8}}A{{/gt}}');
assert.equal(fn({number: 5}), '');
});
});
});
describe('gte', function() {
describe('second argument', function() {
const fn = hbs.compile('{{#gte a b}}A{{else}}B{{/gte}}');
it('should render the first block if true.', function() {
assert.equal(fn({a: 20, b: 15}), 'A');
});
it('should render the first block if equal.', function() {
assert.equal(fn({a: 15, b: 15}), 'A');
});
it('should render the second block if false.', function() {
assert.equal(fn({a: 14, b: 15}), 'B');
});
});
describe('hash compare', function() {
it('should render a block if the value is greater than a given number.', function() {
const fn = hbs.compile('{{#gte number compare=8}}A{{/gte}}');
assert.equal(fn({number: 12}), 'A');
});
it('should render a block if the value is equal to a given number.', function() {
const fn = hbs.compile('{{#gte number compare=8}}A{{/gte}}');
assert.equal(fn({number: 8}), 'A');
});
it('should not render a block if the value is less than a given number.', function() {
const fn = hbs.compile('{{#gte number compare=8}}A{{/gte}}');
assert.equal(fn({number: 5}), '');
});
});
});
describe('has', function() {
describe('inline', function() {
it('should return true when the property exists', function() {
const fn = hbs.compile('{{has "foo"}}');
assert.equal(fn({foo: 'bar'}), 'true');
});
});
describe('block', function() {
it('should render a block if the condition is true.', function() {
const fn = hbs.compile('{{#has context "C"}}A{{else}}B{{/has}}');
assert.equal(fn({context: 'CCC'}), 'A');
});
it('should render the inverse block if false.', function() {
const fn = hbs.compile('{{#has context "zzz"}}A{{else}}B{{/has}}');
assert.equal(fn({context: 'CCC'}), 'B');
});
it('should render the inverse block if value is undefined.', function() {
const fn = hbs.compile('{{#has context}}A{{else}}B{{/has}}');
assert.equal(fn({context: 'CCC'}), 'B');
});
it('should render the inverse block if context is undefined.', function() {
const fn = hbs.compile('{{#has}}A{{else}}B{{/has}}');
assert.equal(fn({context: 'CCC'}), 'B');
});
it('should work with arrays', function() {
const fn = hbs.compile('{{#has array "a"}}A{{else}}B{{/has}}');
assert.equal(fn({array: ['a', 'b', 'c']}), 'A');
});
it('should work with two strings', function() {
const fn = hbs.compile('{{#has "abc" "a"}}A{{else}}B{{/has}}');
assert.equal(fn(), 'A');
});
it('should return the inverse when the second string is not found', function() {
const fn = hbs.compile('{{#has "abc" "z"}}A{{else}}B{{/has}}');
assert.equal(fn(), 'B');
});
it('should work with object keys', function() {
const fn = hbs.compile('{{#has object "a"}}A{{else}}B{{/has}}');
assert.equal(fn({object: {a: 'b'}}), 'A');
});
});
});
describe('isFalsey', function() {
it('should render block if given value is falsey.', function() {
const fn = hbs.compile('{{#if (isFalsey value)}}A{{else}}B{{/if}}');
assert.equal(fn({value: 'nope'}), 'A');
});
it('should render inverse if given value is truthy', function() {
const fn = hbs.compile('{{#if (isFalsey value)}}A{{else}}B{{/if}}');
assert.equal(fn({value: 'CCC'}), 'B');
});
});
describe('isTruthy', function() {
it('should render block if given value is truthy.', function() {
const fn = hbs.compile('{{#if (isTruthy value)}}A{{else}}B{{/if}}');
assert.equal(fn({value: 'CCC'}), 'A');
});
it('should render inverse if given value is not truthy', function() {
const fn = hbs.compile('{{#if (isTruthy value)}}A{{else}}B{{/if}}');
assert.equal(fn({value: 'nope'}), 'B');
});
});
describe('eq', function() {
it('should render a block if the value is equal to a given number.', function() {
const fn = hbs.compile('{{#eq number compare=8}}A{{/eq}}');
assert.equal(fn({number: 8}), 'A');
});
it('should render the inverse block if falsey.', function() {
const fn = hbs.compile('{{#eq number compare=8}}A{{else}}B{{/eq}}');
assert.equal(fn({number: 9}), 'B');
});
it('should compare first and second args', function() {
const fn = hbs.compile('{{#eq number 8}}A{{else}}B{{/eq}}');
assert.equal(fn({number: 9}), 'B');
});
});
describe('ifEven', function() {
it('should render the block if the given value is an even number', function() {
const fn = hbs.compile('{{#ifEven number}}A{{else}}B{{/ifEven}}');
assert(fn({number: 8}), 'A');
});
it('should render the inverse block if the number is odd', function() {
const fn = hbs.compile('{{#ifEven number}}A{{else}}B{{/ifEven}}');
assert.equal(fn({number: 9}), 'B');
});
});
describe('ifNth', function() {
it('should render a custom class on even rows', function() {
const source = '{{#each items}}<div{{#ifNth 2 @index}}{{else}} class="row-alternate"{{/ifNth}}>{{name}}</div>{{/each}}';
const fn = hbs.compile(source);
const context = {
items: [
{ name: 'Philip J. Fry' },
{ name: 'Turanga Leela' },
{ name: 'Bender Bending Rodriguez' },
{ name: 'Amy Wong' },
{ name: 'Hermes Conrad' }
]
};
assert(fn(context), [
'<div>Philip J. Fry</div>',
'<div class="row-alternate">Turanga Leela</div>',
'<div>Bender Bending Rodriguez</div>',
'<div class="row-alternate">Amy Wong</div>',
'<div>Hermes Conrad</div>'
].join(''));
});
});
describe('ifOdd', function() {
it('should render the block if the given value is an even number', function() {
const fn = hbs.compile('{{#ifOdd number}}A{{else}}B{{/ifOdd}}');
assert.equal(fn({number: 9}), 'A');
});
it('should render the inverse block if the number is odd', function() {
const fn = hbs.compile('{{#ifOdd number}}A{{else}}B{{/ifOdd}}');
assert.equal(fn({number: 8}), 'B');
});
});
describe('is', function() {
it('should render a block if the condition is true.', function() {
const fn = hbs.compile('{{#is value "CCC"}}A{{else}}B{{/is}}');
assert.equal(fn({value: 'CCC'}), 'A');
});
it('should use the `compare` arg on the options hash', function() {
const fn = hbs.compile('{{#is value compare="CCC"}}A{{else}}B{{/is}}');
assert.equal(fn({value: 'CCC'}), 'A');
});
it('should render the inverse if the condition is false', function() {
const fn = hbs.compile('{{#is value "FOO"}}A{{else}}B{{/is}}');
assert.equal(fn({value: 'CCC'}), 'B');
});
});
describe('isnt', function() {
it('should render a block if the condition is not true.', function() {
const fn = hbs.compile('{{#isnt number 2}}A{{else}}B{{/isnt}}');
assert.equal(fn({number: 3}), 'A');
});
it('should use the `compare` arg on the options hash', function() {
const fn = hbs.compile('{{#isnt value compare="CCC"}}A{{else}}B{{/isnt}}');
assert.equal(fn({value: 'CCC'}), 'B');
});
it('should render the inverse if the condition is false', function() {
const fn = hbs.compile('{{#isnt value "FOO"}}A{{else}}B{{/isnt}}');
assert.equal(fn({value: 'CCC'}), 'A');
});
});
describe('lt', function() {
describe('second arg', function() {
const fn = hbs.compile('{{#lt a b}}A{{else}}B{{/lt}}');
it('should render the first block if true.', function() {
assert.equal(fn({a: 14, b: 15}), 'A');
});
it('should render the second block if equal.', function() {
assert.equal(fn({a: 15, b: 15}), 'B');
});
it('should render the second block if false.', function() {
assert.equal(fn({a: 20, b: 15}), 'B');
});
});
describe('compare hash', function() {
it('should render a block if the value is less than a given number.', function() {
const fn = hbs.compile('{{#lt number compare=8}}A{{/lt}}');
assert.equal(fn({number: 5}), 'A');
});
it('should not render a block if the value is greater than a given number.', function() {
const fn = hbs.compile('{{#lt number compare=8}}A{{/lt}}');
assert.equal(fn({number: 42}), '');
});
});
});
describe('lte', function() {
const fn = hbs.compile('{{#lte a b}}A{{else}}B{{/lte}}');
describe('second arg', function() {
it('should render the first block if true.', function() {
assert.equal(fn({a: 14, b: 15}), 'A');
});
it('should render the first block if equal.', function() {
assert.equal(fn({a: 15, b: 15}), 'A');
});
it('should render the second block if false.', function() {
assert.equal(fn({a: 20, b: 15}), 'B');
});
});
describe('compare hash', function() {
it('should render a block if the value is less than a given number.', function() {
const fn = hbs.compile('{{#lte number compare=8}}A{{/lte}}');
assert.equal(fn({number: 1}), 'A');
});
it('should render a block if the value is equal to a given number.', function() {
const fn = hbs.compile('{{#lte number compare=8}}A{{/lte}}');
assert.equal(fn({number: 8}), 'A');
});
it('should not render a block if the value is greater than a given number.', function() {
const fn = hbs.compile('{{#lte number compare=8}}A{{/lte}}');
assert.equal(fn({number: 27}), '');
});
});
});
describe('neither', function() {
it('should render a block if one of the values is truthy.', function() {
const fn = hbs.compile('{{#neither great magnificent}}A{{else}}B{{/neither}}');
assert.equal(fn({great: false, magnificent: false}), 'A');
});
it('should render the inverse block if neither are true.', function() {
const fn = hbs.compile('{{#neither great magnificent}}A{{else}}B{{/neither}}');
assert.equal(fn({great: true, magnificent: false}), 'B');
});
});
describe('or', function() {
describe('block', function() {
it('should render a block if one of the values is truthy.', function() {
const fn = hbs.compile('{{#or great magnificent}}A{{else}}B{{/or}}');
assert.equal(fn({great: false, magnificent: true}), 'A');
});
it('should render a block if any of the values are truthy.', function() {
const fn = hbs.compile('{{#or great magnificent fantastic}}A{{else}}B{{/or}}');
assert.equal(fn({great: false, magnificent: false, fantastic: true}), 'A');
});
it('should render the inverse block if neither are true.', function() {
const fn = hbs.compile('{{#or great magnificent}}A{{else}}B{{/or}}');
assert.equal(fn({great: false, magnificent: false}), 'B');
});
it('should render the inverse block if none are true.', function() {
const fn = hbs.compile('{{#or great magnificent fantastic}}A{{else}}B{{/or}}');
assert.equal(fn({great: false, magnificent: false, fantastic: false}), 'B');
});
});
describe('inline', function() {
it('should return false none of the values is truthy.', function() {
const fn = hbs.compile('{{or great magnificent}}');
assert.equal(fn({great: false, magnificent: false}), 'false');
});
it('should return true if one of the values is truthy.', function() {
const fn = hbs.compile('{{or great magnificent}}');
assert.equal(fn({great: false, magnificent: true}), 'true');
});
});
});
describe('unlessEq', function() {
it('should render a block unless the value is equal to a given number.', function() {
const fn = hbs.compile('{{#unlessEq number compare=8}}A{{/unlessEq}}');
assert.equal(fn({number: 10}), 'A');
});
it('should render a block unless the value is equal to a given number.', function() {
const fn = hbs.compile('{{#unlessEq number compare=8}}A{{/unlessEq}}');
assert.equal(fn({number: 8}), '');
});
});
describe('unlessGt', function() {
it('should render a block unless the value is greater than a given number.', function() {
const fn = hbs.compile('{{#unlessGt number compare=8}}A{{/unlessGt}}');
assert.equal(fn({number: 5}), 'A');
});
it('should render a block unless the value is greater than a given number.', function() {
const fn = hbs.compile('{{#unlessGt number compare=8}}A{{/unlessGt}}');
assert.equal(fn({number: 10}), '');
});
});
describe('unlessLt', function() {
it('should render a block unless the value is less than a given number.', function() {
const fn = hbs.compile('{{#unlessLt number compare=8}}A{{/unlessLt}}');
assert.equal(fn({number: 10}), 'A');
});
it('should render a block unless the value is less than a given number.', function() {
const fn = hbs.compile('{{#unlessLt number compare=8}}A{{/unlessLt}}');
assert.equal(fn({number: 5}), '');
});
});
describe('unlessGteq', function() {
it('should render a block unless the value is greater than or equal to a given number.', function() {
const fn = hbs.compile('{{#unlessGteq number compare=8}}A{{/unlessGteq}}');
assert.equal(fn({number: 4}), 'A');
});
it('should render a block unless the value is greater than or equal to a given number.', function() {
const fn = hbs.compile('{{#unlessGteq number compare=8}}A{{/unlessGteq}}');
assert.equal(fn({number: 8}), '');
});
it('should not render a block unless the value is greater than or equal to a given number.', function() {
const fn = hbs.compile('{{#unlessGteq number compare=8}}A{{/unlessGteq}}');
assert.equal(fn({number: 34}), '');
});
});
describe('unlessLteq', function() {
it('should render a block unless the value is less than or equal to a given number.', function() {
const fn = hbs.compile('{{#unlessLteq number compare=8}}A{{/unlessLteq}}');
assert.equal(fn({number: 10}), 'A');
});
it('should render a block unless the value is less than or equal to a given number.', function() {
const fn = hbs.compile('{{#unlessLteq number compare=8}}A{{/unlessLteq}}');
assert.equal(fn({number: 8}), '');
});
it('should not render a block unless the value is less than or equal to a given number.', function() {
const fn = hbs.compile('{{#unlessLteq number compare=8}}A{{/unlessLteq}}');
assert.equal(fn({number: 4}), '');
});
});
});