Files
boursecu_admin/src/utils/weightUnit.ts
T
2026-06-23 22:31:07 +03:30

30 lines
988 B
TypeScript

export const TRADE_WEIGHT_UNIT_GRAM = 'گرم'
export const TRADE_WEIGHT_UNIT_KILOGRAM = 'کیلوگرم'
export function isKilogramUnit(unit?: string | null) {
const value = (unit ?? '').trim()
return value === TRADE_WEIGHT_UNIT_KILOGRAM || value === 'kilogram' || value === 'kg'
}
export function resolveTradeWeightUnit(settingValue?: string | number | null) {
return isKilogramUnit(settingValue?.toString()) ? TRADE_WEIGHT_UNIT_KILOGRAM : TRADE_WEIGHT_UNIT_GRAM
}
export function weightFieldLabelForUnit(unit?: string | null) {
return isKilogramUnit(unit) ? 'وزن به کیلوگرم' : 'وزن به گرم'
}
export function formatAmountWithUnit(
value: number | null | undefined,
unit?: string | null,
decimals = 3
): string {
if (value == null || Number.isNaN(Number(value))) {
return '-'
}
const resolvedUnit = (unit ?? TRADE_WEIGHT_UNIT_GRAM).trim() || TRADE_WEIGHT_UNIT_GRAM
return `${Number(Number(value).toFixed(decimals))} ${resolvedUnit}`
}