You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1003 B
JavaScript
52 lines
1003 B
JavaScript
11 years ago
|
|
||
|
// TODO: handle DOM nodes, fns, etc better.
|
||
|
var subModel = function (obj) {
|
||
|
return obj instanceof Array ?
|
||
|
{ '~array-length': obj.length } :
|
||
|
obj === null ?
|
||
|
null :
|
||
|
typeof obj === 'object' ?
|
||
|
{ '~object': true } :
|
||
|
obj;
|
||
|
};
|
||
|
|
||
|
module.exports = function (id, path) {
|
||
|
|
||
|
if (path === undefined || path === '') {
|
||
|
path = [];
|
||
|
} else if (typeof path === 'string') {
|
||
|
path = path.split('.');
|
||
|
}
|
||
|
|
||
|
var dest = debug.scopes[id],
|
||
|
segment;
|
||
|
|
||
|
if (!dest) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
while (path.length > 0) {
|
||
|
segment = path.shift();
|
||
|
dest = dest[segment];
|
||
|
if (!dest) {
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (dest instanceof Array) {
|
||
|
return dest.map(subModel);
|
||
|
} else if (typeof dest === 'object') {
|
||
|
return Object.
|
||
|
keys(dest).
|
||
|
filter(function (key) {
|
||
|
return key[0] !== '$' || key[1] !== '$';
|
||
|
}).
|
||
|
reduce(function (obj, prop) {
|
||
|
obj[prop] = subModel(dest[prop]);
|
||
|
return obj;
|
||
|
}, {});
|
||
|
} else {
|
||
|
return dest;
|
||
|
}
|
||
|
};
|