Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.