Created
November 12, 2019 15:44
-
-
Save ypetya/86cf2b4081c3c6b9f2faa71c6c5c893e to your computer and use it in GitHub Desktop.
access infinite deep properties - meta programming with Proxy Object - helpful for JSON parse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const endlessProxyHandler = { | |
get: (target, path) => { | |
if (path === 'value') { | |
return target.value; | |
} else if (path === 'array') { | |
return target.value || []; | |
} | |
let newTarget = {}; | |
if (typeof target[path] === 'object' && target[path] !== null) { | |
newTarget = target[path]; | |
} | |
const proxy = new Proxy(newTarget, endlessProxyHandler); | |
proxy.value = target[path]; | |
return proxy; | |
} | |
}; | |
export const wrap = (o) => new Proxy(o, endlessProxyHandler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment