JSFiddle - React, Tailwind, and code Playground
by Graham Dixon
Babel + JSX
class Primitive {
constructor (value) {
this.___ = value;
}
}
let reassigner = (obj) => {
let orig = obj;
const objIsTarget = (obj instanceof Primitive);
if (typeof orig == "object" && !objIsTarget) {
obj = new Primitive(orig.constructor());
// itterate the values and assign to the Target
Object.keys(orig).forEach((k) => {
// check if obj[key] is a Target
const objKeyIsTarget = (orig[k] instanceof Primitive);
// only proceed if it isnt an instance of the Target
if (!objKeyIsTarget) {
// if obj[key] isnt a Target but is an object
if (typeof orig[k] === "object") {
// else create and proxy the Target
Object.keys(orig[k]).forEach((kk) => {
// convert children to Targets
orig[k][kk] = reassigner(orig[k][kk]);
});
}
// convert the value to an instance of the Target, reassigner is fed as final argument so that Target can Reassign its own values in the future
obj['___'][k] = new Primitive(orig[k]);
}
});
} else {
// not an object, set as Target instance, reassigner is fed as final argument so that Target can Reassign its own values in the future
obj = new Primitive(orig);
}
// return the finalised obj (with descendents)
return obj;
}
let obj = {
'scope': {
'a': 1
}
}
let test1 = reassigner(obj);
console.log(test1)
// top is a primitive which contains an object containing primitives
console.log(test1 instanceof Primitive);
// objects dont need to be second level objects
console.log(!(test1.___ instanceof Primitive));
// value is a primitive
console.log((test1.___.scope instanceof Primitive));
// if we always assign a shared primitive as a child... never get data out of primitive just pass primitive into...