forked from oneflow/handlebars-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.js
135 lines (110 loc) · 4.94 KB
/
collection.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
const assert = require('assert');
const hbs = require('handlebars').create();
const helpers = require('..');
helpers.array({ handlebars: hbs });
helpers.collection({ handlebars: hbs });
helpers.string({ handlebars: hbs });
const context = {array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']};
describe('collection', function() {
describe('isEmpty block helper', function() {
it('should render the first block when an array is empty', function() {
const fn = hbs.compile('{{#isEmpty array}}AAA{{else}}BBB{{/isEmpty}}');
assert.equal(fn({array: []}), 'AAA');
});
it('should render the first block when the value is null', function() {
const fn = hbs.compile('{{#isEmpty}}AAA{{else}}BBB{{/isEmpty}}');
assert.equal(fn({array: []}), 'AAA');
});
it('should render the first block when the value is explizitly set to null', function() {
const fn = hbs.compile('{{#isEmpty null}}AAA{{else}}BBB{{/isEmpty}}');
assert.equal(fn({array: []}), 'AAA');
});
it('should render the first block when the value is explizitly set to undefined', function() {
const fn = hbs.compile('{{#isEmpty null}}AAA{{else}}BBB{{/isEmpty}}');
assert.equal(fn({array: []}), 'AAA');
});
it('should render the first block when the value is an empty string', function() {
const fn = hbs.compile('{{#isEmpty ""}}AAA{{else}}BBB{{/isEmpty}}');
assert.equal(fn({array: []}), 'AAA');
});
it('should render the second block when the value is an string', function() {
const fn = hbs.compile('{{#isEmpty "test"}}AAA{{else}}BBB{{/isEmpty}}');
assert.equal(fn({array: []}), 'BBB');
});
it('should render the second block when an array is not empty', function() {
const fn = hbs.compile('{{#isEmpty array}}AAA{{else}}BBB{{/isEmpty}}');
assert.equal(fn(context), 'BBB');
});
it('should render the second block when an object is not empty', function() {
const fn = hbs.compile('{{#isEmpty object}}AAA{{else}}BBB{{/isEmpty}}');
assert.equal(fn({object: {foo: 'bar'}}), 'BBB');
});
it('should render the first block when an object is empty', function() {
const fn = hbs.compile('{{#isEmpty object}}AAA{{else}}BBB{{/isEmpty}}');
assert.equal(fn({object: {}}), 'AAA');
});
});
describe('isEmpty inline helper', function() {
it('should render the first block when an array is empty', function() {
const fn = hbs.compile('{{isEmpty array}}');
assert.equal(fn({array: []}), 'true');
});
it('should render the first block when the value is null', function() {
const fn = hbs.compile('{{isEmpty}}');
assert.equal(fn({array: []}), 'true');
});
it('should render the first block when the value is explizitly set to null', function() {
const fn = hbs.compile('{{isEmpty null}}');
assert.equal(fn({array: []}), 'true');
});
it('should render the first block when the value is explizitly set to undefined', function() {
const fn = hbs.compile('{{isEmpty null}}');
assert.equal(fn({array: []}), 'true');
});
it('should render the first block when the value is an empty string', function() {
const fn = hbs.compile('{{isEmpty ""}}');
assert.equal(fn({array: []}), 'true');
});
it('should render the second block when the value is an string', function() {
const fn = hbs.compile('{{isEmpty "test"}}');
assert.equal(fn({array: []}), 'false');
});
it('should render the second block when an array is not empty', function() {
const fn = hbs.compile('{{isEmpty array}}');
assert.equal(fn(context), 'false');
});
it('should render the second block when an object is not empty', function() {
const fn = hbs.compile('{{isEmpty object}}');
assert.equal(fn({object: {foo: 'bar'}}), 'false');
});
it('should render the first block when an object is empty', function() {
const fn = hbs.compile('{{isEmpty object}}');
assert.equal(fn({object: {}}), 'true');
});
});
describe('length', function() {
it('should return the length of the array', function() {
const fn = hbs.compile('{{length array}}');
assert.equal(fn(context), '8');
});
it('should return zero when undefined', function() {
assert.equal(hbs.compile('{{length}}')(), '0');
});
it('should return the length of a string', function() {
const fn = hbs.compile('{{length "foo"}}');
assert.equal(fn(context), '3');
});
it('should work with arrays passed via subexpression', function() {
const fn = hbs.compile('{{length (split "b,c,a")}}');
assert.equal(fn(context), '3');
});
it('should return 0 when the array is invalid:', function() {
const fn = hbs.compile('{{length foo}}');
assert.equal(fn(context), '0');
});
it('should return 0 when the value is not an array:', function() {
const fn = hbs.compile('{{length foo}}');
assert.equal(fn({foo: {}}), '0');
});
});
});