Created
February 13, 2020 12:09
-
-
Save v1vendi/b87ad119e12b5fab9da43ffda8f5f7cb to your computer and use it in GitHub Desktop.
Object wrapper for getting fields case-insensitively
This file contains 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 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