Skip to content

Instantly share code, notes, and snippets.

@baharalidurrani
Last active August 4, 2021 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baharalidurrani/783d24b5e97c4dfc2cf64cf5a8632111 to your computer and use it in GitHub Desktop.
Save baharalidurrani/783d24b5e97c4dfc2cf64cf5a8632111 to your computer and use it in GitHub Desktop.
Decrypt one level of a generic object without compromising on it's structure
type IData = { [keys: string]: number };
const encrypted1: IData = {
a: 10,
b: 20,
c: 30,
};
const encrypted2: IData = {
d: 40,
e: 50,
f: 60,
};
function decrypt(data: IData): IData {
const result: IData = {};
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
// custom decryption step
const decrypting = data[key] / 10;
result[key] = decrypting;
}
}
return result;
}
const decrypted1: IData = decrypt(encrypted1);
decrypted1; // { a: 1, b: 2, c: 3 }
const decrypted2: IData = decrypt(encrypted2);
decrypted2; // { d: 4, e: 5, f: 6 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment