Skip to content

Instantly share code, notes, and snippets.

@ypetya
Created November 12, 2019 15:44
Show Gist options
  • Save ypetya/86cf2b4081c3c6b9f2faa71c6c5c893e to your computer and use it in GitHub Desktop.
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
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