Skip to content

Instantly share code, notes, and snippets.

@karolk
Last active July 12, 2017 21:10
Show Gist options
  • Save karolk/09680c17a89df7c5e7dc531e2a60e2b0 to your computer and use it in GitHub Desktop.
Save karolk/09680c17a89df7c5e7dc531e2a60e2b0 to your computer and use it in GitHub Desktop.
camelcasing proxy (toy)
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)
@karolk
Copy link
Author

karolk commented Jul 12, 2017

const test = {
  test_me: {
    i_am: [1,2,3]
  }
}
const proxied = getProxy(test)
proxied.testMe.iAm
// > [1,2,3]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment