File size: 857 Bytes
1e92f2d |
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 |
export const byString = (o, s) => {
if (!s) {
return;
}
s = s.replace(/\[(\w+)\]/g, ".$1"); // convert indexes to properties
s = s.replace(/^\./, ""); // strip a leading dot
var a = s.split(".");
for (var i = 0, n = a.length; i < n; ++i) {
var x = a[i];
if (o && x in o) {
o = o[x];
} else {
return;
}
}
return o;
};
export const setByString = (obj, path, value) => {
var schema = obj; // a moving reference to internal objects within obj
path = path.replace(/\[(\w+)\]/g, ".$1"); // convert indexes to properties
path = path.replace(/^\./, ""); // strip a leading dot
var pList = path.split(".");
var len = pList.length;
for (var i = 0; i < len - 1; i++) {
var elem = pList[i];
if (!schema[elem]) schema[elem] = {};
schema = schema[elem];
}
schema[pList[len - 1]] = value;
};
|