Skip to content

Instantly share code, notes, and snippets.

@deepakshrma
Last active May 20, 2020 17:03
Show Gist options
  • Save deepakshrma/325d734d55f44060fdab2d16d28aac11 to your computer and use it in GitHub Desktop.
Save deepakshrma/325d734d55f44060fdab2d16d28aac11 to your computer and use it in GitHub Desktop.
Utils in typescript
const evalReg = /(\.)|(\[(\d)\])/;
const safeEval = (key: string, obj: any) => {
let lastKey;
let match;
do {
if (lastKey) {
if (match && match[2]) {
obj = obj[lastKey][match[3]];
} else {
obj = obj[lastKey];
}
}
match = evalReg.exec(key);
if (!match) {
lastKey = key;
break;
} else {
lastKey = key.substr(0, match.index);
key = key.slice(!match[3] ? match.index + 1 : match.index + 3);
}
} while (match);
if (lastKey) {
obj = obj[lastKey];
}
return obj;
};
/**
*
const obj = {
id: 1,
version: "1.0.1",
contributors: ["deepak", "gary"],
actor: {
name: "Tom Cruise",
age: 56,
"Born At": "Syracuse, NY",
Birthdate: "July 3 1962",
movies: ["Top Gun", "Mission: Impossible", "Oblivion"],
photo: "https://jsonformatter.org/img/tom-cruise.jpg",
},
};
console.log(JSON.stringify(obj, null, 2))
console.log(safeEval("id", obj));
console.log(safeEval("contributors", obj));
console.log(safeEval("contributors[1]", obj));
console.log(safeEval("actor.movies[2]", obj));
*/
export default safeEval;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment