@marcopeg/dotted
Version:
Extract data from an object using a dotted notation
47 lines (36 loc) • 866 B
JavaScript
;
exports.__esModule = true;
exports.dotted = void 0;
var _dotted = require("./dotted.set");
/**
* Minimalistic Interface to Dot-Access an Object
*
*/
var dotted = function dotted(source, path) {
if (!path) {
return source;
} // const res = JSON.parse(JSON.stringify(source))
return path.split('.').reduce(function (curr, key) {
if (key[0] !== '$') {
return curr[key];
}
if (key === '$JSON') {
return JSON.stringify(curr);
}
if (key === '$LENGTH') {
return curr.length;
}
if (key === '$FIRST') {
return curr[0];
}
if (key === '$LAST') {
return curr[curr.length - 1];
}
if (Array.isArray(curr)) {
var idx = parseInt(key.substr(1, key.length), 10);
return curr[idx];
}
}, source);
};
exports.dotted = dotted;
dotted.set = _dotted.dottedSet;