Created
March 24, 2020 17:42
-
-
Save FlorianWendelborn/7e4b3a4abbc030e67034f0a1e2df4aa1 to your computer and use it in GitHub Desktop.
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 replaceObjectStrings = <T extends object>( | |
data: T, | |
from: string | RegExp, | |
to: string | |
): T => | |
Object.fromEntries( | |
Object.entries(data).map(([key, value]) => { | |
if (value === null) return [key, null] | |
switch (typeof value) { | |
case 'string': | |
return [key, value.replace(from, to)] | |
case 'object': | |
return [key, replaceObjectStrings(value, from, to)] | |
default: | |
throw new Error('Unsupported Data Type') | |
} | |
}) | |
) | |
console.log(replaceObjectStrings({ a: { b: 'top.kek' } }, '.', '###')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment