-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (36 loc) · 1.11 KB
/
index.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
/**
* Module dependencies
*/
var type = require('component-type');
var types = {
'array': require('./lib/array'),
'boolean': require('./lib/boolean'),
'null': require('./lib/null'),
'number': require('./lib/number'),
'object': require('./lib/object'),
'string': require('./lib/string'),
'undefined': require('./lib/null')
};
exports = module.exports = function(value, dom, opts) {
return handle(dom, opts || {}, [], value);
};
function handle(dom, opts, path, item, key, handleFn) {
var itemType = type(item);
var mod = types[itemType];
if (!mod) throw new Error('Unsupported type "' + itemType + '"');
if (typeof key !== 'undefined' &&
key !== null &&
key !== '' &&
key !== 'collection' &&
key !== 'data') path = append(path, key);
handleFn = handleFn || handle.bind(null, dom, opts, path);
return mod(item, dom, opts, handleFn, resolve.bind(null, path));
};
function resolve(path, id) {
return '/' + append(path, id).join('/');
}
function append(arr, item) {
arr = (arr || []).slice();
Array.isArray(item) ? arr.push.apply(arr, item) : arr.push(item);
return arr;
}