forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.js
290 lines (250 loc) · 6.68 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
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
'use strict';
var hasOwn = Object.hasOwnProperty;
var util = require('handlebars-utils');
var array = require('./array');
var utils = require('./utils/');
var helpers = module.exports;
/**
* Extend the context with the properties of other objects.
* A shallow merge is performed to avoid mutating the context.
*
* @param {Object} `objects` One or more objects to extend.
* @return {Object}
* @api public
*/
helpers.extend = function(/*objects*/) {
var args = [].slice.call(arguments);
var opts = {};
if (util.isOptions(args[args.length - 1])) {
// remove handlebars options object
opts = args.pop().hash;
// re-add handlebars options.hash object
args.push(opts);
}
var context = {};
for (var i = 0; i < args.length; i++) {
var obj = args[i];
if (util.isObject(obj)) {
var keys = Object.keys(obj);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
context[key] = obj[key];
}
}
}
return context;
};
/**
* Block helper that iterates over the properties of
* an object, exposing each key and value on the context.
*
* @param {Object} `context`
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.forIn = function(obj, options) {
if (!util.isOptions(options)) {
return obj.inverse(this);
}
var data = utils.createFrame(options, options.hash);
var result = '';
for (var key in obj) {
data.key = key;
result += options.fn(obj[key], {data: data});
}
return result;
};
/**
* Block helper that iterates over the **own** properties of
* an object, exposing each key and value on the context.
*
* @param {Object} `obj` The object to iterate over.
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.forOwn = function(obj, options) {
if (!util.isOptions(options)) {
return obj.inverse(this);
}
var data = utils.createFrame(options, options.hash);
var result = '';
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
data.key = key;
result += options.fn(obj[key], {data: data});
}
}
return result;
};
/**
* Take arguments and, if they are string or number, convert them to a dot-delineated object property path.
*
* @param {String|Number} `prop` The property segments to assemble (can be multiple).
* @return {String}
* @api public
*/
helpers.toPath = function(/*prop*/) {
var prop = [];
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'string' || typeof arguments[i] === 'number') {
prop.push(arguments[i]);
}
}
return prop.join('.');
};
/**
* Use property paths (`a.b.c`) to get a value or nested value from
* the context. Works as a regular helper or block helper.
*
* @param {String} `prop` The property to get, optionally using dot notation for nested properties.
* @param {Object} `context` The context object
* @param {Object} `options` The handlebars options object, if used as a block helper.
* @return {String}
* @block
* @api public
*/
helpers.get = function(prop, context, options) {
var val = utils.get(context, prop);
if (options && options.fn) {
return val ? options.fn(val) : options.inverse(context);
}
return val;
};
/**
* Use property paths (`a.b.c`) to get an object from
* the context. Differs from the `get` helper in that this
* helper will return the actual object, including the
* given property key. Also, this helper does not work as a
* block helper.
*
* @param {String} `prop` The property to get, optionally using dot notation for nested properties.
* @param {Object} `context` The context object
* @return {String}
* @api public
*/
helpers.getObject = function(prop, context) {
return utils.getObject(context, prop);
};
/**
* Return true if `key` is an own, enumerable property
* of the given `context` object.
*
* ```handlebars
* {{hasOwn context key}}
* ```
*
* @param {String} `key`
* @param {Object} `context` The context object.
* @return {Boolean}
* @api public
*/
helpers.hasOwn = function(context, key) {
return hasOwn.call(context, key);
};
/**
* Return true if `value` is an object.
*
* ```handlebars
* {{isObject "foo"}}
* //=> false
* ```
* @param {String} `value`
* @return {Boolean}
* @api public
*/
helpers.isObject = function(value) {
return utils.typeOf(value) === 'object';
};
/**
* Parses the given string using `JSON.parse`.
*
* ```handlebars
* <!-- string: '{"foo": "bar"}' -->
* {{JSONparse string}}
* <!-- results in: { foo: 'bar' } -->
* ```
* @param {String} `string` The string to parse
* @contributor github.com/keeganstreet
* @block
* @api public
*/
helpers.JSONparse = function(str, options) {
return JSON.parse(str);
};
/**
* Stringify an object using `JSON.stringify`.
*
* ```handlebars
* <!-- object: { foo: 'bar' } -->
* {{JSONstringify object}}
* <!-- results in: '{"foo": "bar"}' -->
* ```
* @param {Object} `obj` Object to stringify
* @return {String}
* @api public
*/
helpers.JSONstringify = function(obj, indent) {
if (!utils.isNumber(indent)) {
indent = 0;
}
return JSON.stringify(obj, null, indent);
};
/**
* Deeply merge the properties of the given `objects` with the
* context object.
*
* @param {Object} `object` The target object. Pass an empty object to shallow clone.
* @param {Object} `objects`
* @return {Object}
* @api public
*/
helpers.merge = function(context/*, objects, options*/) {
var args = [].slice.call(arguments);
var opts = {};
if (util.isOptions(args[args.length - 1])) {
// remove handlebars options object
opts = args.pop().hash;
// re-add options.hash
args.push(opts);
}
return Object.assign.apply(null, args);
};
/**
* Alias for parseJSON. this will be
* deprecated in a future release
*/
helpers.parseJSON = helpers.JSONparse;
/**
* Pick properties from the context object.
*
* @param {Array|String} `properties` One or more properties to pick.
* @param {Object} `context`
* @param {Object} `options` Handlebars options object.
* @return {Object} Returns an object with the picked values. If used as a block helper, the values are passed as context to the inner block. If no values are found, the context is passed to the inverse block.
* @block
* @api public
*/
helpers.pick = function(props, context, options) {
var keys = array.arrayify(props);
var len = keys.length, i = -1;
var result = {};
while (++i < len) {
result = helpers.extend({}, result, utils.getObject(context, keys[i]));
}
if (options.fn) {
if (Object.keys(result).length) {
return options.fn(result);
}
return options.inverse(context);
}
return result;
};
/**
* Alias for JSONstringify. this will be
* deprecated in a future release
*/
helpers.stringify = helpers.JSONstringify;