-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
collection.js
137 lines (110 loc) · 4.53 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
136
137
'use strict';
require('mocha');
var assert = require('assert');
var hbs = require('handlebars').create();
var helpers = require('..');
helpers.array({handlebars: hbs});
helpers.collection({handlebars: hbs});
helpers.string({handlebars: hbs});
var 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() {
var 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() {
var fn = hbs.compile('{{#isEmpty}}AAA{{else}}BBB{{/isEmpty}}');
assert.equal(fn({array: []}), 'AAA');
});
it('should render the second block when an array is not empty', function() {
var 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() {
var 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() {
var 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() {
var fn = hbs.compile('{{isEmpty array}}');
assert.equal(fn({array: []}), 'true');
});
it('should render the first block when the value is null', function() {
var fn = hbs.compile('{{isEmpty}}');
assert.equal(fn({array: []}), 'true');
});
it('should render the second block when an array is not empty', function() {
var fn = hbs.compile('{{isEmpty array}}');
assert.equal(fn(context), 'false');
});
it('should render the second block when an object is not empty', function() {
var fn = hbs.compile('{{isEmpty object}}');
assert.equal(fn({object: {foo: 'bar'}}), 'false');
});
it('should render the first block when an object is empty', function() {
var fn = hbs.compile('{{isEmpty object}}');
assert.equal(fn({object: {}}), 'true');
});
});
describe('iterate', function() {
describe('object', function() {
it('should iterate over a plain object:', function() {
var obj = {a: 'aaa', b: 'bbb', c: 'ccc'};
var fn = hbs.compile('{{#iterate obj}}{{.}}{{/iterate}}');
assert.equal(fn({obj: obj}), 'aaabbbccc');
});
it('should expose `@key`:', function() {
var obj = {a: 'aaa', b: 'bbb', c: 'ccc'};
var fn = hbs.compile('{{#iterate obj}}{{@key}}{{/iterate}}');
assert.equal(fn({obj: obj}), 'abc');
});
it('should render the inverse block when falsey:', function() {
var fn = hbs.compile('{{#iterate obj}}A{{else}}B{{/iterate}}');
assert.equal(fn(), 'B');
});
});
describe('array', function() {
it('should iterate over an array:', function() {
var arr = [{name: 'a'}, {name: 'b'}, {name: 'c'}];
var fn = hbs.compile('{{#iterate arr}}{{name}}{{/iterate}}');
assert.equal(fn({arr: arr}), 'abc');
});
it('should expose `@index`:', function() {
var arr = [{name: 'a'}, {name: 'b'}, {name: 'c'}];
var fn = hbs.compile('{{#iterate arr}}{{@index}}{{/iterate}}');
assert.equal(fn({arr: arr}), '012');
});
});
});
describe('length', function() {
it('should return the length of the array', function() {
var 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() {
var fn = hbs.compile('{{length "foo"}}');
assert.equal(fn(context), '3');
});
it('should work with arrays passed via subexpression', function() {
var fn = hbs.compile('{{length (split "b,c,a")}}');
assert.equal(fn(context), '3');
});
it('should return 0 when the array is invalid:', function() {
var fn = hbs.compile('{{length foo}}');
assert.equal(fn(context), '0');
});
it('should return 0 when the value is not an array:', function() {
var fn = hbs.compile('{{length foo}}');
assert.equal(fn({foo: {}}), '0');
});
});
});