第4项:对页面、表单及两版共享规则启用严格类型检查
This commit is contained in:
1 parent
389952f31c
commit
f570929cfe
77 files changed
+7629
-2981
No files matched your search
@@ -1,13 +1,16 @@
|
||||
'use strict';
|
||||
// 只接受约定的销售公式,不执行输入代码。金额复用开单的整数计算。
|
||||
(function (root) {
|
||||
/** @type {Pick<import('../web/src/shared/types').LedgerCalcApi, 'sale'|'cents'|'orderTotal'>} */
|
||||
const Calc = typeof module === 'object' && module.exports ? require('./calculations') : root.LedgerCalc;
|
||||
/** @param {string} value */
|
||||
const decimal = value => {
|
||||
const [whole, fraction = ''] = value.split('.');
|
||||
return (
|
||||
(whole.replace(/^0+(?=\d)/, '') || '0') + (fraction.replace(/0+$/, '') ? '.' + fraction.replace(/0+$/, '') : '')
|
||||
);
|
||||
};
|
||||
/** @param {string} input */
|
||||
const normalize = input =>
|
||||
input
|
||||
.normalize('NFKC')
|
||||
@@ -22,7 +25,9 @@
|
||||
weight = number + '(?:克|g)?\\*';
|
||||
const gramPattern = new RegExp('^' + weight + '\\(' + number + '\\+' + number + '\\)' + extras + ending, 'i');
|
||||
const piecePattern = new RegExp('^' + weight + number + '\\+' + number + extras + ending, 'i');
|
||||
/** @param {string} value */
|
||||
const money = value => Calc.cents(decimal(value), '公式金额');
|
||||
/** @param {string} input @param {{roundingMode?: 'cent'|'yuan'}} [options] */
|
||||
function parse(input, { roundingMode = 'yuan' } = {}) {
|
||||
if (typeof input !== 'string' || input.length > 300) throw new Error('每行请填写300字以内的销售公式');
|
||||
const formula = normalize(input),
|
||||
@@ -32,6 +37,7 @@
|
||||
const [, weight, gold, labor, extra, stated] = match;
|
||||
const quantities = extra ? extra.slice(1).split('+') : [];
|
||||
if (quantities.length > 100) throw new Error('每件商品最多100项附加费');
|
||||
/** @type {import('../web/src/shared/orderEntryTypes').FormulaLine} */
|
||||
const line = {
|
||||
unit: 'g',
|
||||
pricingMode: 'auto',
|
||||
@@ -56,6 +62,7 @@
|
||||
mismatch: statedTotal !== null && statedTotal !== total.total,
|
||||
};
|
||||
}
|
||||
/** @param {string} input @param {{roundingMode?: 'cent'|'yuan'}} [options] */
|
||||
function parseBatch(input, { roundingMode = 'yuan' } = {}) {
|
||||
if (typeof input !== 'string' || input.length > 18000) throw new Error('请填写18000字以内的公式,每件商品一行');
|
||||
const rows = input
|
||||
@@ -63,10 +70,11 @@
|
||||
.map(value => value.trim())
|
||||
.filter(Boolean);
|
||||
if (!rows.length || rows.length > 61) throw new Error('请填写1~60件商品公式,可在最后另写一行合计');
|
||||
/** @type {ReturnType<typeof parse>[]} */
|
||||
const items = [],
|
||||
warnings = [];
|
||||
let summary = null;
|
||||
rows.forEach((raw, index) => {
|
||||
for (const [index, raw] of rows.entries()) {
|
||||
const normalized = normalize(raw).replace(/^(?:合计|总价|总计|总金额):?/, '');
|
||||
const sumMatch = normalized.match(/^(\d+(?:\.\d+)?(?:\+\d+(?:\.\d+)?)+)(?:=(\d+(?:\.\d+)?))?$/);
|
||||
if (sumMatch) {
|
||||
@@ -87,10 +95,10 @@
|
||||
try {
|
||||
item = parse(raw, { roundingMode });
|
||||
} catch (err) {
|
||||
throw new Error('第' + (index + 1) + '行:' + err.message);
|
||||
throw new Error('第' + (index + 1) + '行:' + (err instanceof Error ? err.message : String(err)));
|
||||
}
|
||||
items.push(item);
|
||||
if (item.mismatch)
|
||||
if (item.mismatch && item.statedTotal !== null)
|
||||
warnings.push(
|
||||
'商品 ' +
|
||||
items.length +
|
||||
@@ -101,7 +109,7 @@
|
||||
'元。',
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
const total = Calc.orderTotal(
|
||||
items.map(item => item.subtotal),
|
||||
0,
|
||||
@@ -130,4 +138,4 @@
|
||||
const api = { parse, parseBatch };
|
||||
if (typeof module === 'object' && module.exports) module.exports = api;
|
||||
else root.OrderFormula = api;
|
||||
})(typeof globalThis === 'object' ? globalThis : this);
|
||||
})(typeof globalThis === 'object' ? globalThis : /** @type {typeof globalThis} */ (/** @type {unknown} */ (this)));
|
||||
Reference in new issue
Block a user