JSFiddle - React, Tailwind, and code Playground
by Darby Rathbone
JavaScript
(function () {
var o = Object.create({});
o.foo = {
a: 'a',
b: ["B", 'b']
};
function _subEval(s) {
s = s.match(/(?:{{)(.+)(}}/gim);
return s.map(function () {
return Function('return ' + splitObjects(s).join(''))()
}).join('');
}
function toObject(Obj) {
var o =Object.create(null);
Object.getOwnPropertyNames(Obj).forEach(function func(e) {
if (typeof this[e] == "object" || Array.isArray(this[e])) {
Object.getOwnPropertyNames(this[e]).forEach(function (f) {
o[e + "." + f] = this[e][f]);
});
}
o.forEach(func,o);
},Obj)
return o;
}
function toValues(str) {
if (Array.isArray(str)) return str.map(toValues);
if (!Boolean(str.trim())) return str;
var s = str.split('.');
console.log(s);
return s.reduce(function (e, a) {
if (e === '') {
return '';
}
if (typeof e[a] !== 'undefined') return e[a];
return "";
}, o) || null;
}
function splitObjects(str) {
return toValues.call(o, str.match(/(?:(?:\b)([a-z0-9.]+)(?:\b))|(?:([^a-z0-9.]+))/gim));
}
console.log(toObject(o));
}())