|
1
|
import { MathBN } from "@medusajs/framework/utils" |
|
2
|
import { |
|
3
|
ComputeActionItemLine, |
|
4
|
ComputeActionShippingLine, |
|
5
|
} from "@medusajs/types" |
|
6
|
|
|
7
|
export function sortLineItemByPriceAscending( |
|
8
|
a: ComputeActionItemLine, |
|
9
|
b: ComputeActionItemLine |
|
10
|
) { |
|
11
|
const priceA = MathBN.div(a.subtotal, a.quantity) |
|
12
|
const priceB = MathBN.div(b.subtotal, b.quantity) |
|
13
|
|
|
14
|
// Returning a non-zero value for equal prices makes this an inconsistent |
|
15
|
// comparator, which leaves the order of equally priced lines up to the |
|
16
|
// sort implementation rather than keeping it stable. |
|
17
|
if (MathBN.eq(priceA, priceB)) { |
|
18
|
return 0 |
|
19
|
} |
|
20
|
|
|
21
|
return MathBN.lt(priceA, priceB) ? -1 : 1 |
|
22
|
} |
|
23
|
|
|
24
|
export function sortShippingLineByPriceAscending( |
|
25
|
a: ComputeActionShippingLine, |
|
26
|
b: ComputeActionShippingLine |
|
27
|
) { |
|
28
|
const priceA = a.subtotal ?? 0 |
|
29
|
const priceB = b.subtotal ?? 0 |
|
30
|
|
|
31
|
if (MathBN.eq(priceA, priceB)) { |
|
32
|
return 0 |
|
33
|
} |
|
34
|
|
|
35
|
return MathBN.lt(priceA, priceB) ? -1 : 1 |
|
36
|
} |