I have function decodeVals
in NodeJS that decodes all of the values of the given object from Base64 to ASCII. This is achieved by traversing the values of said object then converting each element one at a time via a function call to the "homemade" atob
function.
Though the results are fast on objects I've tested - which are relatively small - I imagine this could be inefficient for much larger objects.
Does anyone know of a more efficient/less expensive way of achieving this?
const atob = (str) => Buffer.from(str, 'base64').toString();
// Decoder Function
function decodeVals(obj) {
for (let [key, value] of Object.entries(obj)) {
if (!Array.isArray(value)) {
obj[key] = atob(value);
} else {
for (let arrayElement of value) {
obj[key][obj[key].indexOf(arrayElement)] = atob(arrayElement);
}
}
}
return obj;
}
const testObj = {
correct_answer: 'VHJlbnQgUmV6bm9y',
incorrect_answers: ['TWFyaWx5biBNYW5zb24=', 'Um9iaW4gRmluY2s=', 'Sm9zaCBIb21tZQ=='],
};
const res = decodeVals(testObj);
console.log(res);
/*
{
correct_answer: 'Trent Reznor',
incorrect_answers: ['Marilyn Manson', 'Robin Finck', 'Josh Homme']
}
*/
testObj
, with the values being in base64. The goal is to decode given JSON from said API \$\endgroup\$