JSFiddle - React, Tailwind, and code Playground
by samonela
JavaScript 1.7
const [Tree, Fragment] = ( function(){
const LeafCache = Object.create( null ),
genLeaf = ( CLASS ) => {
return LeafCache[CLASS.name] || (LeafCache[CLASS.name] = eval( `class Leaf extends ${CLASS.name} {
constructor(val) {
super(val);
}
}; Leaf.prototype.__isleaf__ = true; Leaf` ));
}
TARGET = Symbol( '__target__' ),
HANDLER = Symbol( '__handler__' ),
PROXY = Symbol( '__proxy__' ),
ISTREE = Symbol( '__istree__' ),
{ assign, defineProperty, entries, setPrototypeOf } = Object,
{ hasOwnProperty } = {},
convert = ( obj ) => {
let res = obj instanceof Branch ? obj : new Branch( obj );
for( const key in obj ) {
const value = obj[key];
if( hasOwnProperty.call( obj, key ) ) {
if( '__isfragment__' !== key ) {
if( typeof value === 'object' ) {
res[key] = convert( value );
Object.defineProperty( res[key], '__parent__', {
value: res[PROXY],
configurable: false,
protected: false
} );
} else {
let val;
res[key] = new Proxy( val = new ( genLeaf( value.constructor ) )( value ), genHandler( val ) );
}
}
}
}
return res;
},
getKey = ( obj, val ) => {
for( const key in obj ) {
const value = obj[key];
if( value[TARGET] === val ) {
return key;
}
}
};
console.log('genLeaf', typeof genLeaf);
let genHandler = ( _target ) => {
return ( function(){
let res;
const _raw = __raw__.bind( _target ),
_keys = {
'__raw__': _raw,
[ISTREE]: true,
[TARGET]: _target,
...