forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 5
/
object.js
216 lines (181 loc) · 7.58 KB
/
object.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
const assert = require('assert');
const helpers = require('..');
const hbs = require('handlebars').create();
helpers.math({handlebars: hbs});
helpers.object({handlebars: hbs});
const context = {object: {a: 'b', c: 'd', e: 'f'}};
describe('object', function() {
describe('extend', function() {
it('should extend multiple objects into one', function() {
const fn = hbs.compile('{{{stringify (extend a d g)}}}');
const actual = fn({a: {b: 'c'}, d: {e: 'f'}, g: {h: 'i'}});
assert.equal(actual, '{"b":"c","e":"f","h":"i"}');
});
it('should skip over sparse objects', function() {
const fn = hbs.compile('{{{stringify (extend a d g)}}}');
const actual = fn({a: {b: 'c'}, d: undefined, g: {h: 'i'}});
assert.equal(actual, '{"b":"c","h":"i"}');
});
});
describe('forOwn', function() {
it('should iterate over each property in an object:', function() {
const fn = hbs.compile('{{#forOwn this}} {{@key}} {{.}} {{/forOwn}}');
assert.equal(fn(context.object), ' a b c d e f ');
});
it('should return the inverse block if no object is passed:', function() {
const fn = hbs.compile('{{#forOwn}} {{.}} {{else}} Nada. {{/forOwn}}');
assert.equal(fn(context.object), ' Nada. ');
});
it('should only expose "own" keys:', function() {
function Foo() {
this.a = 'b';
this.b = 'c';
}
Foo.prototype.c = 'd';
const fn = hbs.compile('{{#forOwn this}} {{.}} {{/forOwn}}');
assert.equal(fn(new Foo()), ' b c ');
});
it('should expose private variables:', function() {
const fn = hbs.compile('{{#forOwn this abc=object}} {{@abc.c}} {{/forOwn}}');
assert.equal(fn(context), ' d ');
});
});
describe('getObject', function() {
it('should get an object from the context', function() {
const one = hbs.compile('{{{stringify (getObject "a" this)}}}')({a: 'b'});
assert.equal(one, '{"a":"b"}');
const two = hbs.compile('{{{stringify (getObject "c" this)}}}')({c: 'd'});
assert.equal(two, '{"c":"d"}');
const three = hbs.compile('{{{stringify (getObject "c.d" this)}}}')({ c: { d: { e: 'f' }, g: 'h' } });
assert.equal(three, '{"d":{"e":"f"}}');
});
});
describe('toPath', function() {
it('should return a path from provided arguments', function() {
assert.equal(hbs.compile('{{toPath "a" "b" "c"}}')(), 'a.b.c');
});
it('should return a path from calculated arguments', function() {
const t = hbs.compile('{{toPath "a" (add 1 1) "b"}}')();
assert.equal(t, 'a.2.b');
});
it('should return a `get` compatible path', function() {
const fn = hbs.compile('{{get (toPath "a" (add 1 1) "j") this}}');
assert.equal(fn({a: [{b: 'c', d: 'e'}, {f: 'g', h: 'i'}, {j: 'k', l: 'm'}]}), 'k');
});
});
describe('get', function() {
it('should get a value from the context', function() {
assert.equal(hbs.compile('{{get "a" this}}')({a: 'b'}), 'b');
assert.equal(hbs.compile('{{get "c" this}}')({c: 'd'}), 'd');
});
it('should get a nested value from the context', function() {
const fn = hbs.compile('{{get "a.b.c.d" this}}');
assert.equal(fn({a: {b: {c: {d: 'e'}}}}), 'e');
});
it('should work as a block helper', function() {
const fn1 = hbs.compile('{{#get "a" this}} {{.}} {{/get}}');
assert.equal(fn1(context.object), ' b ');
const fn2 = hbs.compile('{{#get "c" this}} {{.}} {{/get}}');
assert.equal(fn2(context.object), ' d ');
});
it('should get the inverse block if not found', function() {
const fn = hbs.compile('{{#get "foo" this}} {{.}} {{else}}Nope.{{/get}}');
assert.equal(fn(context.object), 'Nope.');
});
});
describe('getProperty', function() {
it('returns the correct property', function() {
const fn = hbs.compile('{{getProperty "name"}}');
assert.equal(fn({ name: 'john' }), 'john');
});
});
describe('hasOwn', function() {
function Foo() {
this.a = 'b';
this.b = 'c';
}
Foo.prototype.c = 'd';
it('should return true if object has own property:', function() {
const fn = hbs.compile('{{hasOwn this "a"}}');
assert.equal(fn(new Foo()), 'true');
});
it('should return false if object does not have own property:', function() {
const fn = hbs.compile('{{hasOwn this "c"}}');
assert.equal(fn(new Foo()), 'false');
});
});
describe('isObject', function() {
it('should return true if value is an object:', function() {
const fn = hbs.compile('{{isObject this}}');
assert.equal(fn({a: 'b'}), 'true');
});
it('should return false if value is not an object:', function() {
const fn = hbs.compile('{{isObject this}}');
assert.equal(fn('foo'), 'false');
});
});
describe('merge', function() {
it('should deeply merge objects passed on the context:', function() {
const fn = hbs.compile('{{{stringify (merge a b c)}}}');
const actual = fn({a: {one: 'two'}, b: {one: 'three'}, c: {two: 'four'}});
assert.equal(actual, '{"one":"three","two":"four"}');
});
});
describe('JSONparse', function() {
it('should parse a JSON string:', function() {
const fn = hbs.compile('{{lookup (JSONparse string) "name"}}');
assert.equal(fn({string: '{"name": "Fry"}'}), 'Fry');
});
});
describe('JSONstringify', function() {
it('should stringify an object', function() {
const fn = hbs.compile('{{{JSONstringify value}}}');
assert.equal(fn({value: { foo: 'bar' }}), '{"foo":"bar"}');
});
});
describe('prettyJson', function() {
it('should stringify an object', function() {
const fn = hbs.compile('{{{prettyJson value}}}');
assert.equal(fn({value: { foo: 'bar' }}), '{\n "foo": "bar"\n}');
});
});
describe('pick', function() {
it('should pick a value from the context', function() {
const one = hbs.compile('{{{stringify (pick "a" this)}}}')({a: 'b'});
assert.equal(one, '{"a":"b"}');
const two = hbs.compile('{{{stringify (pick "c" this)}}}')({c: 'd'});
assert.equal(two, '{"c":"d"}');
});
it('should pick a nested value from the context', function() {
const fn = hbs.compile('{{{stringify (pick "a.b.c" this)}}}');
assert.equal(fn({a: {b: {c: {d: 'e'}}}}), '{"c":{"d":"e"}}');
});
it('should work as a block helper', function() {
const fn1 = hbs.compile('{{#pick "a" this}} {{{stringify .}}} {{/pick}}');
assert.equal(fn1(context.object), ' {"a":"b"} ');
const fn2 = hbs.compile('{{#pick "c" this}} {{{stringify .}}} {{/pick}}');
assert.equal(fn2(context.object), ' {"c":"d"} ');
});
it('should pick the inverse block if not found', function() {
const fn = hbs.compile('{{#pick "foo" this}} {{.}} {{else}}Nope.{{/pick}}');
assert.equal(fn(context.object), 'Nope.');
});
});
describe('stringify', function() {
it('should stringify an object:', function() {
const fn = hbs.compile('{{{stringify data}}}');
const res = fn({data: {name: 'Halle', age: 4, userid: 'Nicole'}});
assert.equal(res, '{"name":"Halle","age":4,"userid":"Nicole"}');
});
});
describe('checkExists', () => {
it('returns the contents if field exists', () => {
const fn = hbs.compile("{{#checkExists test 'field'}}assert{{/checkExists}}");
assert.equal(fn({ test: { field: 'here' } }), 'assert');
});
it('doesn\'t return the contents if field doesn\'t exist', () => {
const fn = hbs.compile("{{#checkExists test 'field'}}assert{{/checkExists}}");
assert.equal(fn({ test: { differentField: 'here' } }), '');
});
});
});