-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
object.js
206 lines (172 loc) · 7.15 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
'use strict';
require('mocha');
var assert = require('assert');
var support = require('./support');
var expected = support.expected('object');
var helpers = require('..');
var hbs = require('handlebars').create();
helpers.math({handlebars: hbs});
helpers.object({handlebars: hbs});
var context = {object: {a: 'b', c: 'd', e: 'f'}};
describe('object', function() {
describe('extend', function() {
it('should extend multiple objects into one:', function() {
var fn = hbs.compile('{{{stringify (extend a d g)}}}');
var actual = fn({a: {b: 'c'}, d: {e: 'f'}, g: {h: 'i'}});
assert.equal(actual, expected('extend.txt'));
});
it('should work as a non-helper util:', function() {
var actual = helpers().extend({a: {b: 'c'}}, {d: {e: 'f'}}, {g: {h: 'i'}});
assert.deepEqual(actual, { a: { b: 'c' }, d: { e: 'f' }, g: { h: 'i' } });
});
it('should skip over sparse objects', function() {
var actual = helpers().extend({a: {b: 'c'}}, null, {g: {h: 'i'}});
assert.deepEqual(actual, { a: { b: 'c' }, g: { h: 'i' } });
});
});
describe('forIn', function() {
it('should iterate over each property in an object:', function() {
var fn = hbs.compile('{{#forIn this}} {{@key}} {{.}} {{/forIn}}');
assert.equal(fn(context.object), ' a b c d e f ');
});
it('should return the inverse block if no object is passed:', function() {
var fn = hbs.compile('{{#forIn}} {{.}} {{else}} Nada. {{/forIn}}');
assert.equal(fn(context.object), ' Nada. ');
});
it('should expose private variables:', function() {
var fn = hbs.compile('{{#forIn this abc=object}} {{@abc.a}} {{/forIn}}');
assert.equal(fn(context), ' b ');
});
});
describe('forOwn', function() {
it('should iterate over each property in an object:', function() {
var 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() {
var 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';
var fn = hbs.compile('{{#forOwn this}} {{.}} {{/forOwn}}');
assert.equal(fn(new Foo()), ' b c ');
});
it('should expose private variables:', function() {
var 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() {
var one = hbs.compile('{{{stringify (getObject "a" this)}}}')({a: 'b'});
assert.equal(one, '{"a":"b"}');
var two = hbs.compile('{{{stringify (getObject "c" this)}}}')({c: 'd'});
assert.equal(two, '{"c":"d"}');
});
});
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() {
var t = hbs.compile('{{toPath "a" (add 1 1) "b"}}')();
assert.equal(t, 'a.2.b');
});
it('should return a `get` compatible path', function() {
var 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() {
var 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() {
var fn1 = hbs.compile('{{#get "a" this}} {{.}} {{/get}}');
assert.equal(fn1(context.object), ' b ');
var fn2 = hbs.compile('{{#get "c" this}} {{.}} {{/get}}');
assert.equal(fn2(context.object), ' d ');
});
it('should get the inverse block if not found', function() {
var fn = hbs.compile('{{#get "foo" this}} {{.}} {{else}}Nope.{{/get}}');
assert.equal(fn(context.object), 'Nope.');
});
});
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() {
var fn = hbs.compile('{{hasOwn this "a"}}');
assert.equal(fn(new Foo()), 'true');
});
it('should return false if object does not have own property:', function() {
var 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() {
var fn = hbs.compile('{{isObject this}}');
assert.equal(fn({a: 'b'}), 'true');
});
it('should return false if value is not an object:', function() {
var fn = hbs.compile('{{isObject this}}');
assert.equal(fn('foo'), 'false');
});
});
describe('merge', function() {
it('should deeply merge objects passed on the context:', function() {
var fn = hbs.compile('{{{stringify (merge a b c)}}}');
var 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() {
var fn = hbs.compile('{{lookup (JSONparse string) "name"}}');
assert.equal(fn({string: '{"name": "Fry"}'}), 'Fry');
});
});
describe('pick', function() {
it('should pick a value from the context', function() {
var one = hbs.compile('{{{stringify (pick "a" this)}}}')({a: 'b'});
assert.equal(one, '{"a":"b"}');
var two = hbs.compile('{{{stringify (pick "c" this)}}}')({c: 'd'});
assert.equal(two, '{"c":"d"}');
});
it('should pick a nested value from the context', function() {
var 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() {
var fn1 = hbs.compile('{{#pick "a" this}} {{{stringify .}}} {{/pick}}');
assert.equal(fn1(context.object), ' {"a":"b"} ');
var 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() {
var fn = hbs.compile('{{#pick "foo" this}} {{.}} {{else}}Nope.{{/pick}}');
assert.equal(fn(context.object), 'Nope.');
});
});
describe('stringify', function() {
it('should stringify an object:', function() {
var fn = hbs.compile('{{{stringify data}}}');
var res = fn({data: {name: 'Halle', age: 4, userid: 'Nicole'}});
assert.equal(res, '{"name":"Halle","age":4,"userid":"Nicole"}');
});
});
});