优化提交

This commit is contained in:
2026-10-10 13:47:47 +08:00
parent 5751973c83
commit 90e1e646ec
3 changed files with 48 additions and 3 deletions
@@ -0,0 +1,22 @@
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import ts from 'typescript'
const source = readFileSync(new URL('./returnReceiptDisplay.ts', import.meta.url), 'utf8')
const compiled = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }).outputText
const exports = {}
new Function('exports', compiled)(exports)
test('volume rounds upward to exactly three decimal places without increasing exact thousandths', () => {
for (const [input, expected] of [[0.186624, '0.187'], [0.15, '0.150'], [1.001, '1.001'], [1.00100001, '1.002'], [0.0000001, '0.001'], [0, '0.000'], [null, '—'], [NaN, '—']]) {
assert.equal(exports.formatReturnVolume(input), expected)
}
})
test('parcel waybills appear as tracking numbers while generated parcel codes remain physical codes', () => {
assert.equal(exports.receiptTrackingNo({ unitType: 'PARCEL', unitCode: 'sf561626' }), 'sf561626')
assert.equal(exports.receiptTrackingNo({ unitType: 'PARCEL', unitCode: 'PK0123456789ABCDEF' }), '')
assert.equal(exports.receiptTrackingNo({ unitType: 'PARCEL', unitCode: 'PK0123456789ABCDEF', trackingNo: ' SF123 ' }), 'SF123')
assert.equal(exports.receiptTrackingNo({ unitType: 'PALLET', unitCode: 'PLT123' }), '')
})
@@ -0,0 +1,21 @@
import type { ReturnReceipt } from '../api/returnOrders'
export function receiptTrackingNo(receipt: Pick<ReturnReceipt, 'trackingNo' | 'unitCode' | 'unitType'> | Record<string, unknown>) {
const trackingNo = typeof receipt.trackingNo === 'string' ? receipt.trackingNo.trim() : ''
if (trackingNo) return trackingNo
const code = typeof receipt.unitCode === 'string' ? receipt.unitCode.trim() : ''
return receipt.unitType === 'PARCEL' && code && !/^PK[0-9A-F]{16}$/.test(code) ? code : ''
}
export function formatReturnVolume(value?: number | null) {
if (value == null || !Number.isFinite(value) || value < 0) return '—'
// Shift the decimal in text to avoid floating-point over-rounding exact thousandths.
const [coefficient, exponent = '0'] = String(value).toLowerCase().split('e')
const [integer, fraction = ''] = coefficient!.split('.')
const digits = integer! + fraction
const cutoff = integer!.length + Number(exponent) + 3
const scaled = cutoff <= 0 ? '0' : digits.slice(0, cutoff).padEnd(cutoff, '0')
const remainder = cutoff < 0 ? digits : digits.slice(Math.max(0, cutoff))
const rounded = BigInt(scaled) + (/[1-9]/.test(remainder) ? 1n : 0n)
return `${rounded / 1000n}.${String(rounded % 1000n).padStart(3, '0')}`
}