feat(utils,types): added item/shipping adjustments for order/items/shipping_methods (#6050)
what: - adds compute actions for the following cases: - items => each & across - shipping_method => each & across - order - adds a remove compute actions when code is no longer present in adjustments array RESOLVES CORE-1625 RESOLVES CORE-1626 RESOLVES CORE-1627 RESOLVES CORE-1628 RESOLVES CORE-1585
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
import { pickValueFromObject } from "../pick-value-from-object"
|
||||
|
||||
describe("pickValueFromObject", function () {
|
||||
it("should return true or false for different types of data", function () {
|
||||
const expectations = [
|
||||
{
|
||||
input: {
|
||||
1: "attribute.another_attribute",
|
||||
2: {
|
||||
attribute: {
|
||||
another_attribute: "test",
|
||||
},
|
||||
},
|
||||
},
|
||||
output: "test",
|
||||
},
|
||||
{
|
||||
input: {
|
||||
1: "attribute.another_attribute.array_attribute",
|
||||
2: {
|
||||
attribute: {
|
||||
another_attribute: [
|
||||
{
|
||||
array_attribute: "test 1",
|
||||
},
|
||||
{
|
||||
array_attribute: "test 2",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
output: ["test 1", "test 2"],
|
||||
},
|
||||
{
|
||||
input: {
|
||||
1: "attribute.another_attribute.array_attribute.deep_array_attribute",
|
||||
2: {
|
||||
attribute: {
|
||||
another_attribute: [
|
||||
{
|
||||
array_attribute: [
|
||||
{
|
||||
deep_array_attribute: "test 1",
|
||||
},
|
||||
{
|
||||
deep_array_attribute: "test 2",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
array_attribute: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
output: ["test 1", "test 2"],
|
||||
},
|
||||
{
|
||||
input: {
|
||||
1: "attribute.another_attribute.array_attribute",
|
||||
2: {
|
||||
attribute: {
|
||||
another_attribute: [
|
||||
{
|
||||
array_attribute: [
|
||||
{
|
||||
deep_array_attribute: "test 1",
|
||||
},
|
||||
{
|
||||
deep_array_attribute: "test 2",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
array_attribute: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
output: [
|
||||
{
|
||||
deep_array_attribute: "test 1",
|
||||
},
|
||||
{
|
||||
deep_array_attribute: "test 2",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
input: {
|
||||
1: "attribute.missing_attribute",
|
||||
2: {
|
||||
attribute: {
|
||||
another_attribute: "test",
|
||||
},
|
||||
},
|
||||
},
|
||||
output: undefined,
|
||||
},
|
||||
]
|
||||
|
||||
expectations.forEach((expectation) => {
|
||||
expect(
|
||||
pickValueFromObject(expectation.input["1"], expectation.input["2"])
|
||||
).toEqual(expectation.output)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -23,6 +23,7 @@ export * from "./medusa-container"
|
||||
export * from "./object-from-string-path"
|
||||
export * from "./object-to-string-path"
|
||||
export * from "./optional-numeric-serializer"
|
||||
export * from "./pick-value-from-object"
|
||||
export * from "./promise-all"
|
||||
export * from "./remote-query-object-from-string"
|
||||
export * from "./remote-query-object-to-string"
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { isObject } from "./is-object"
|
||||
|
||||
export function pickValueFromObject(
|
||||
path: string,
|
||||
object: Record<any, any>
|
||||
): any {
|
||||
const segments = path.split(".")
|
||||
let result: any = undefined
|
||||
|
||||
for (const segment of segments) {
|
||||
const segmentsLeft = [...segments].splice(1, segments.length - 1)
|
||||
const segmentOutput = object[segment]
|
||||
|
||||
if (segmentsLeft.length === 0) {
|
||||
result = segmentOutput
|
||||
break
|
||||
}
|
||||
|
||||
if (isObject(segmentOutput)) {
|
||||
result = pickValueFromObject(segmentsLeft.join("."), segmentOutput)
|
||||
break
|
||||
}
|
||||
|
||||
if (Array.isArray(segmentOutput)) {
|
||||
result = segmentOutput
|
||||
.map((segmentOutput_) =>
|
||||
pickValueFromObject(segmentsLeft.join("."), segmentOutput_)
|
||||
)
|
||||
.flat()
|
||||
break
|
||||
}
|
||||
|
||||
result = segmentOutput
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user