Last active
July 12, 2017 21:10
-
-
Save karolk/09680c17a89df7c5e7dc531e2a60e2b0 to your computer and use it in GitHub Desktop.
camelcasing proxy (toy)
This file contains hidden or 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 toSnakeCase = str => str.replace(/([A-Z])/g, match => '_' + match.toLowerCase()) | |
const handler = { | |
get(target, name) { | |
const snakeName = toSnakeCase(name) | |
const value = target[snakeName] | |
if (value === Object(value) && !Array.isArray(value)) { | |
return new Proxy(value, handler) | |
} else { | |
return Reflect.get(target, snakeName); | |
} | |
} | |
} | |
const getProxy = target => new Proxy(target, handler) |
Author
karolk
commented
Jul 12, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment