Skip to content

Instantly share code, notes, and snippets.

@FlorianWendelborn
Created March 24, 2020 17:42
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 FlorianWendelborn/7e4b3a4abbc030e67034f0a1e2df4aa1 to your computer and use it in GitHub Desktop.
Save FlorianWendelborn/7e4b3a4abbc030e67034f0a1e2df4aa1 to your computer and use it in GitHub Desktop.
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