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)`.
This commit is contained in:
@@ -3,12 +3,10 @@ export function stringifyNullProperties<T extends object>(input: T): T {
|
|||||||
const res = {} as T
|
const res = {} as T
|
||||||
|
|
||||||
Object.keys(obj).reduce((acc: T, key: string) => {
|
Object.keys(obj).reduce((acc: T, key: string) => {
|
||||||
if (typeof obj[key] === "object") {
|
|
||||||
acc[key] = convertProperties(obj[key])
|
|
||||||
}
|
|
||||||
|
|
||||||
if (obj[key] === null) {
|
if (obj[key] === null) {
|
||||||
acc[key] = "null"
|
acc[key] = "null"
|
||||||
|
} else if (typeof obj[key] === "object") {
|
||||||
|
acc[key] = convertProperties(obj[key])
|
||||||
} else {
|
} else {
|
||||||
acc[key] = obj[key]
|
acc[key] = obj[key]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import { stringifyNullProperties } from "../../src/utils"
|
||||||
|
|
||||||
|
describe("stringifyNullProperties", () => {
|
||||||
|
test("returns empty object on no props", () => {
|
||||||
|
const result = stringifyNullProperties({})
|
||||||
|
expect(result).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
test("successfully stringifies null property", () => {
|
||||||
|
const result = stringifyNullProperties({ test: null })
|
||||||
|
expect(result).toEqual({ test: "null" })
|
||||||
|
})
|
||||||
|
|
||||||
|
test("successfully stringifies nested null property", () => {
|
||||||
|
const result = stringifyNullProperties({
|
||||||
|
test: { test_2: { test_3: null } },
|
||||||
|
another_test: "test",
|
||||||
|
})
|
||||||
|
expect(result).toEqual({
|
||||||
|
test: { test_2: { test_3: "null" } },
|
||||||
|
another_test: "test",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test("successfully stringifies string property", () => {
|
||||||
|
const result = stringifyNullProperties({
|
||||||
|
test: "test",
|
||||||
|
})
|
||||||
|
expect(result).toEqual({ test: "test" })
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user