Skip to content

Instantly share code, notes, and snippets.

@x7c1
Created April 15, 2017 14:45
Show Gist options
  • Select an option

  • Save x7c1/2bf2fcfa674d86d28f14df94f08892c9 to your computer and use it in GitHub Desktop.

Select an option

Save x7c1/2bf2fcfa674d86d28f14df94f08892c9 to your computer and use it in GitHub Desktop.
const onUnknown = key => new Proxy({}, {
get(target, name) {
switch (name) {
case 'or':
Console.debug(`unknown key [${key}]`);
return initial => initial;
default:
return onUnknown(key);
}
},
});
const extractorOf = underlying => {
return new Proxy(underlying, {
get: (target, name) => {
if (!(name in target)) {
return onUnknown(name);
}
switch (typeof target[name]) {
case 'function':
return target[name];
case 'object':
return extractorOf(target[name]);
default:
return extractorOf({ or: () => target[name] });
}
},
});
};
export default extractorOf;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment