Skip to content

Instantly share code, notes, and snippets.

@v1vendi
Created February 13, 2020 12:09
Show Gist options
  • Save v1vendi/b87ad119e12b5fab9da43ffda8f5f7cb to your computer and use it in GitHub Desktop.
Save v1vendi/b87ad119e12b5fab9da43ffda8f5f7cb to your computer and use it in GitHub Desktop.
Object wrapper for getting fields case-insensitively
const model = {
normalKey: 1,
coRRupTkey: 2,
innErKey: {
x: 3,
Y: 4
},
aRR: [
{ first: 5 }
]
}
function getModel(model) {
return new Proxy(model, {
get(obj, prop) {
if (prop in obj) return obj[prop]
const lowerProp = prop.toLowerCase()
const ownProp = Object.keys(model).find(k => k.toLowerCase() === lowerProp)
const value = obj[ownProp]
return value instanceof Object ? getModel(value) : value
}
})
}
const wrapped = getModel(model)
console.log(wrapped.corruptKey)
console.log(wrapped.innerKey.x)
console.log(wrapped.innerKey.y)
console.log(wrapped.arr.map(x => x.FIRSt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment