fix(utils): big bumber should give numeric value of 0 for small numbers (#13394)

* fix(utils): big bumber should give numeric value of 0 for small numbers

* circular dep

* fix imports

* fix infinite loop

* fix test

* changeset
This commit is contained in:
William Bouchard
2025-09-03 13:45:41 -04:00
committed by GitHub
parent 115a1deb49
commit faae150a58
6 changed files with 42 additions and 35 deletions

View File

@@ -1,6 +1,7 @@
import { BigNumberInput, BigNumberRawValue, IBigNumber } from "@medusajs/types"
import { BigNumber as BigNumberJS } from "bignumber.js"
import { isBigNumber, isString } from "../common"
import { isBigNumber } from "../common/is-big-number"
import { isString } from "../common/is-string"
export class BigNumber implements IBigNumber {
static DEFAULT_PRECISION = 20
@@ -80,12 +81,19 @@ export class BigNumber implements IBigNumber {
}
get numeric(): number {
let value: number
let raw = this.raw_ as BigNumberRawValue
if (raw) {
return new BigNumberJS(raw.value).toNumber()
value = new BigNumberJS(raw.value).toNumber()
} else {
return this.numeric_
value = this.numeric_
}
if (Math.abs(value) <= MEDUSA_EPSILON.numeric_) {
return 0
}
return value
}
set numeric(value: BigNumberInput) {
@@ -111,15 +119,21 @@ export class BigNumber implements IBigNumber {
}
toJSON(): number {
return this.bignumber_
const value = this.bignumber_
? this.bignumber_?.toNumber()
: this.raw_
? new BigNumberJS(this.raw_.value).toNumber()
: this.numeric_
if (Math.abs(value) <= MEDUSA_EPSILON.numeric_) {
return 0
}
return value
}
valueOf(): number {
return this.numeric_
return this.numeric
}
[Symbol.toPrimitive](hint) {
@@ -127,6 +141,10 @@ export class BigNumber implements IBigNumber {
return this.raw?.value
}
return this.numeric_
return this.numeric
}
}
export const MEDUSA_EPSILON = new BigNumber(
process.env.MEDUSA_EPSILON || "0.0001"
)

View File

@@ -1,7 +1,7 @@
import { BigNumberInput } from "@medusajs/types"
import { isDefined } from "../../common"
import { BigNumber } from "../big-number"
import { MathBN, MEDUSA_EPSILON } from "../math"
import { BigNumber, MEDUSA_EPSILON } from "../big-number"
import { MathBN } from "../math"
export function calculateCreditLinesTotal({
creditLines,

View File

@@ -3,10 +3,6 @@ import { BigNumber as BigNumberJS } from "bignumber.js"
import { isDefined } from "../common"
import { BigNumber } from "./big-number"
export const MEDUSA_EPSILON = new BigNumber(
process.env.MEDUSA_EPSILON || "0.0001"
)
type BNInput = BigNumberInput | BigNumber
export class MathBN {
static convert(num: BNInput, decimalPlaces?: number): BigNumberJS {

View File

@@ -1,9 +1,7 @@
import { BigNumberInput } from "@medusajs/types"
import {
ApplicationMethodAllocation,
ApplicationMethodType,
} from "../../promotion"
import { MathBN, MEDUSA_EPSILON } from "../math"
import { ApplicationMethodAllocation, ApplicationMethodType, } from "../../promotion"
import { MathBN } from "../math"
import { MEDUSA_EPSILON } from "../big-number"
function getPromotionValueForPercentage(promotion, lineItemAmount) {
return MathBN.mult(MathBN.div(promotion.value, 100), lineItemAmount)