Files
medusa-store/packages/medusa-js/src/utils.ts
Oliver Windall Juhl 7bee57f7c5 fix(medusa-js): Fix stringifyNullProperties util (#1766)
**What**
Changes the order of object type evaluation to properly handle null properties.

Previously, `stringifyNullProperties({ test: null })` would fail in the 2nd iteration due to an attempt to iterate `null` in `Object.keys(obj)`.
2022-07-02 15:29:27 +00:00

22 lines
486 B
TypeScript

export function stringifyNullProperties<T extends object>(input: T): T {
const convertProperties = (obj: T) => {
const res = {} as T
Object.keys(obj).reduce((acc: T, key: string) => {
if (obj[key] === null) {
acc[key] = "null"
} else if (typeof obj[key] === "object") {
acc[key] = convertProperties(obj[key])
} else {
acc[key] = obj[key]
}
return acc
}, res)
return res
}
return convertProperties(input)
}