@rockn/vue-formula-editor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1383 @@
1
+ var H = Object.defineProperty;
2
+ var P = (e, t, r) => t in e ? H(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
+ var x = (e, t, r) => P(e, typeof t != "symbol" ? t + "" : t, r);
4
+ const V = ["===", "!=="], _ = ["==", "!=", "<=", ">=", "&&", "||", "**"], Y = "+-*/%<>!?=,():[]";
5
+ function G(e) {
6
+ return e.replace(
7
+ /\\u([0-9a-fA-F]{4})/g,
8
+ (t, r) => String.fromCharCode(parseInt(r, 16))
9
+ ).replace(/\\(.)/g, (t, r) => r === "n" ? `
10
+ ` : r === "t" ? " " : r === "r" ? "\r" : r);
11
+ }
12
+ function W(e) {
13
+ const t = [], r = e.length;
14
+ let n = 0;
15
+ const s = (i, l, u, m, p = !0) => t.push({ type: i, value: l, start: u, end: m, raw: e.slice(u, m), closed: p });
16
+ for (; n < r; ) {
17
+ const i = e[n];
18
+ if (/\s/.test(i)) {
19
+ n++;
20
+ continue;
21
+ }
22
+ if (i === "{") {
23
+ const m = n;
24
+ let p = n + 1, f = !1;
25
+ for (; p < r && e[p] !== "}"; ) p++;
26
+ p < r && (f = !0);
27
+ const o = e.slice(n + 1, p), N = f ? p + 1 : p;
28
+ s("variable", o, m, N, f), n = N;
29
+ continue;
30
+ }
31
+ if (/[0-9]/.test(i) || i === "." && /[0-9]/.test(e[n + 1] ?? "")) {
32
+ const m = /^(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/.exec(e.slice(n));
33
+ if (m) {
34
+ s("number", m[0], n, n + m[0].length), n += m[0].length;
35
+ continue;
36
+ }
37
+ }
38
+ if (i === ".") {
39
+ s("dot", i, n, n + 1), n++;
40
+ continue;
41
+ }
42
+ if (i === '"' || i === "'") {
43
+ const m = n, p = i;
44
+ let f = n + 1, o = !1, N = !1;
45
+ for (; f < r; ) {
46
+ const a = e[f];
47
+ if (N) N = !1;
48
+ else if (a === "\\") N = !0;
49
+ else if (a === p) {
50
+ o = !0, f++;
51
+ break;
52
+ } else if (a === `
53
+ `) break;
54
+ f++;
55
+ }
56
+ const g = e.slice(m + 1, o ? f - 1 : f);
57
+ s("string", o ? G(g) : g, m, f, o), n = f;
58
+ continue;
59
+ }
60
+ if (/[A-Za-z_\u4e00-\u9fa5]/.test(i)) {
61
+ const m = /^[A-Za-z_\u4e00-\u9fa5][A-Za-z0-9_\u4e00-\u9fa5]*/.exec(e.slice(n));
62
+ if (m) {
63
+ s("identifier", m[0], n, n + m[0].length), n += m[0].length;
64
+ continue;
65
+ }
66
+ }
67
+ const l = e.slice(n, n + 3);
68
+ if (V.includes(l)) {
69
+ s("operator", l, n, n + 3), n += 3;
70
+ continue;
71
+ }
72
+ const u = e.slice(n, n + 2);
73
+ if (_.includes(u)) {
74
+ s("operator", u, n, n + 2), n += 2;
75
+ continue;
76
+ }
77
+ if (Y.includes(i)) {
78
+ s(i === "(" ? "lparen" : i === ")" ? "rparen" : i === "[" ? "lbracket" : i === "]" ? "rbracket" : i === "," ? "comma" : i === "?" ? "question" : i === ":" ? "colon" : "operator", i, n, n + 1), n++;
79
+ continue;
80
+ }
81
+ s("error", i, n, n + 1), n++;
82
+ }
83
+ return t.push({ type: "eof", value: "", start: r, end: r, raw: "" }), t;
84
+ }
85
+ class E extends Error {
86
+ constructor(r, n, s) {
87
+ super(r);
88
+ x(this, "start");
89
+ x(this, "end");
90
+ this.name = "ParseError", this.start = n, this.end = s;
91
+ }
92
+ }
93
+ const C = {
94
+ OR: 1,
95
+ "||": 1,
96
+ AND: 2,
97
+ "&&": 2,
98
+ "==": 3,
99
+ "!=": 3,
100
+ "===": 3,
101
+ "!==": 3,
102
+ "<": 4,
103
+ "<=": 4,
104
+ ">": 4,
105
+ ">=": 4,
106
+ "+": 5,
107
+ "-": 5,
108
+ "*": 6,
109
+ "/": 6,
110
+ "%": 6,
111
+ "**": 7
112
+ };
113
+ function O(e, ...t) {
114
+ if (!e || e.type !== "identifier") return !1;
115
+ for (const r of t)
116
+ if (e.value === r || /^[A-Za-z]+$/.test(e.value) && /^[A-Za-z]+$/.test(r) && e.value.toUpperCase() === r.toUpperCase())
117
+ return !0;
118
+ return !1;
119
+ }
120
+ function K(e) {
121
+ return O(e, "IF", "如果") && e !== void 0;
122
+ }
123
+ function U(e) {
124
+ const t = W(e);
125
+ let r = 0;
126
+ const n = (a = 0) => t[r + a] ?? t[t.length - 1], s = () => t[r++];
127
+ function i(a, c) {
128
+ const b = n();
129
+ if (b.type !== a) throw new E(c, b.start, b.end);
130
+ return s();
131
+ }
132
+ function l(a = 0) {
133
+ let c = f();
134
+ for (; ; ) {
135
+ const b = n(), h = b.type === "operator" ? b.value : b.type === "identifier" ? b.value.toUpperCase() : void 0;
136
+ if (h !== void 0 && C[h] !== void 0) {
137
+ const I = C[h];
138
+ if (I < a) break;
139
+ s();
140
+ const $ = l(h === "**" ? I : I + 1);
141
+ c = { type: "binary", op: h, left: c, right: $, start: c.start, end: $.end };
142
+ } else if (b.type === "question" && a === 0) {
143
+ s();
144
+ const I = l(0);
145
+ i("colon", '三元表达式缺少 ":" 分隔符');
146
+ const $ = l(0);
147
+ c = {
148
+ type: "conditional",
149
+ test: c,
150
+ consequent: I,
151
+ alternate: $,
152
+ start: c.start,
153
+ end: $.end
154
+ };
155
+ } else
156
+ break;
157
+ }
158
+ return c;
159
+ }
160
+ function u(a) {
161
+ const c = [];
162
+ let b = null;
163
+ if (n().type === "eof" || O(n(), "THEN", "那么", "ELSE", "否则", "ELSEIF", "否则如果"))
164
+ throw new E(`IF/如果 后缺少条件表达式(遇到 "${n().raw}")`, n().start, n().end);
165
+ let h = l(0);
166
+ m();
167
+ let I = l(0);
168
+ c.push({ condition: h, body: I });
169
+ let $ = I.end;
170
+ for (; ; ) {
171
+ const d = n();
172
+ if (O(d, "ELSE", "否则", "ELSEIF", "否则如果")) {
173
+ if (s(), O(d, "ELSEIF", "否则如果") || O(n(), "IF", "如果") && n(1).type !== "lparen") {
174
+ if (O(d, "ELSEIF", "否则如果") || s(), n().type === "eof" || O(n(), "THEN", "那么", "ELSE", "否则", "ELSEIF", "否则如果"))
175
+ throw new E(`ELSE IF/否则如果 后缺少条件表达式(遇到 "${n().raw}")`, n().start, n().end);
176
+ h = l(0), m(), I = l(0), c.push({ condition: h, body: I }), $ = I.end;
177
+ continue;
178
+ }
179
+ if (n().type === "eof")
180
+ throw new E("ELSE/否则 后缺少表达式", d.start, d.end);
181
+ b = l(0), $ = b.end;
182
+ break;
183
+ }
184
+ break;
185
+ }
186
+ return { type: "if", branches: c, elseBody: b, start: a.start, end: $ };
187
+ }
188
+ function m() {
189
+ const a = n();
190
+ if (!O(a, "THEN", "那么"))
191
+ throw new E(
192
+ `IF 条件后缺少 "THEN/那么"(遇到 "${a.raw}",块状 IF 写法:IF 条件 THEN 真值 ELSE 假值)`,
193
+ a.start,
194
+ a.end
195
+ );
196
+ s();
197
+ }
198
+ function p(a) {
199
+ let c = a;
200
+ for (; ; ) {
201
+ const b = n();
202
+ if (b.type === "dot") {
203
+ s();
204
+ const h = n();
205
+ if (h.type !== "identifier")
206
+ throw new E(`"." 后应为成员名,遇到 "${h.raw}"`, h.start, h.end);
207
+ s(), c = { type: "member", object: c, prop: h.value, index: null, start: c.start, end: h.end };
208
+ continue;
209
+ }
210
+ if (b.type === "lbracket") {
211
+ s();
212
+ const h = l(0), I = i("rbracket", '缺少右括号 "]"');
213
+ h.type === "string" ? c = { type: "member", object: c, prop: h.value, index: null, start: c.start, end: I.end } : c = { type: "member", object: c, prop: "", index: h, start: c.start, end: I.end };
214
+ continue;
215
+ }
216
+ break;
217
+ }
218
+ return c;
219
+ }
220
+ function f() {
221
+ const a = n();
222
+ if (a.type === "operator" && (a.value === "-" || a.value === "+" || a.value === "!")) {
223
+ s();
224
+ const c = f();
225
+ return { type: "unary", op: a.value, operand: c, start: a.start, end: c.end };
226
+ }
227
+ return p(o());
228
+ }
229
+ function o() {
230
+ const a = n();
231
+ if (a.type === "number")
232
+ return s(), { type: "number", value: parseFloat(a.value), start: a.start, end: a.end };
233
+ if (a.type === "string") {
234
+ if (s(), !a.closed) throw new E(`字符串未闭合(缺少 ${a.raw[0] ?? "引号"})`, a.start, a.end);
235
+ return { type: "string", value: a.value, start: a.start, end: a.end };
236
+ }
237
+ if (a.type === "variable") {
238
+ if (s(), !a.closed) throw new E('变量缺少结束符 "}"', a.start, a.end);
239
+ if (!a.value.trim()) throw new E("变量名不能为空,例如 {订单金额}", a.start, a.end);
240
+ return { type: "variable", name: a.value.trim(), start: a.start, end: a.end };
241
+ }
242
+ if (a.type === "identifier") {
243
+ s();
244
+ const c = a.value.toLowerCase();
245
+ if (c === "true" || c === "false")
246
+ return { type: "boolean", value: c === "true", start: a.start, end: a.end };
247
+ if (c === "null" || c === "undefined") return { type: "null", start: a.start, end: a.end };
248
+ if (K(a) && n().type !== "lparen")
249
+ return u(a);
250
+ if (O(a, "THEN", "那么"))
251
+ throw new E('意外的 "THEN/那么"(缺少与之配对的 IF?)', a.start, a.end);
252
+ if (O(a, "ELSE", "否则", "ELSEIF", "否则如果"))
253
+ throw new E('意外的 "ELSE/否则"(缺少与之配对的 IF?)', a.start, a.end);
254
+ if (n().type === "lparen") {
255
+ s();
256
+ const b = [];
257
+ if (n().type !== "rparen")
258
+ for (b.push(l(0)); n().type === "comma"; )
259
+ s(), b.push(l(0));
260
+ const h = i("rparen", `函数 ${a.value}() 缺少右括号 ")"`);
261
+ return { type: "call", name: a.value, args: b, start: a.start, end: h.end };
262
+ }
263
+ throw new E(
264
+ `无法识别的标识符 "${a.value}"(变量请用 {名称} 引用;函数需要括号调用)`,
265
+ a.start,
266
+ a.end
267
+ );
268
+ }
269
+ if (a.type === "lparen") {
270
+ s();
271
+ const c = l(0);
272
+ return i("rparen", '缺少右括号 ")"'), c;
273
+ }
274
+ throw a.type === "eof" ? new E("表达式不完整,结尾缺少操作数", a.start, a.end) : a.type === "operator" && a.value === "=" ? new E('单个 "=" 不能用于比较,请使用 "==" 或 "==="', a.start, a.end) : a.type === "operator" && (a.value === ")" || a.value === "]") ? new E(`多余的 "${a.value}"`, a.start, a.end) : a.type === "rbracket" ? new E('多余的 "]"', a.start, a.end) : a.type === "dot" ? new E('"." 前缺少被访问的表达式', a.start, a.end) : a.type === "comma" ? new E('多余的逗号 ","', a.start, a.end) : new E(`意外的符号 "${a.raw}"`, a.start, a.end);
275
+ }
276
+ const N = l(0), g = n();
277
+ if (g.type !== "eof")
278
+ throw g.type === "operator" && g.value === "=" ? new E('单个 "=" 不能用于比较,请使用 "==" 或 "==="', g.start, g.end) : new E(`意外的符号 "${g.raw}"(表达式已结束)`, g.start, g.end);
279
+ return N;
280
+ }
281
+ class y extends Error {
282
+ constructor(r, n, s) {
283
+ super(r);
284
+ x(this, "start");
285
+ x(this, "end");
286
+ this.name = "RuntimeError", this.start = n, this.end = s;
287
+ }
288
+ }
289
+ function q(e) {
290
+ const t = /* @__PURE__ */ new Map();
291
+ for (const r of e ?? []) {
292
+ if (r.type !== "enum" || !r.options) continue;
293
+ const n = /* @__PURE__ */ new Set(), s = /* @__PURE__ */ new Set(), i = /* @__PURE__ */ new Map();
294
+ for (const l of r.options) {
295
+ const u = typeof l == "string" ? { label: l, value: l } : l;
296
+ n.add(u.label), s.add(u.value), i.set(u.label, u.value);
297
+ }
298
+ t.set(r.name, { labels: n, values: s, labelToValue: i });
299
+ }
300
+ return { byName: t };
301
+ }
302
+ function w(e) {
303
+ return e == null ? "空值 NULL" : typeof e == "number" ? `数字 ${e}` : typeof e == "string" ? `字符串 "${e.length > 12 ? e.slice(0, 12) + "…" : e}"` : typeof e == "boolean" ? `布尔值 ${e}` : Array.isArray(e) ? `数组(${e.length} 项)` : typeof e == "object" ? `对象(${Object.keys(e).length} 个键)` : "其他";
304
+ }
305
+ function M(e, t) {
306
+ if (typeof e == "number" && !Number.isNaN(e)) return e;
307
+ throw new y(`${t} 需要数字,实际为 ${w(e)}`, 0, 0);
308
+ }
309
+ function S(e, t, r) {
310
+ if (typeof e == "number" && !Number.isNaN(e)) return e;
311
+ if (typeof e == "string" && e.trim() !== "" && !Number.isNaN(Number(e))) return Number(e);
312
+ throw new y(
313
+ `函数 ${t}() 的第 ${r + 1} 个参数需要数字,实际为 ${w(e)}`,
314
+ 0,
315
+ 0
316
+ );
317
+ }
318
+ function k(e) {
319
+ return typeof e == "number" && !Number.isNaN(e) ? e : typeof e == "string" && e.trim() !== "" && !Number.isNaN(Number(e)) ? Number(e) : 0;
320
+ }
321
+ function R(e, t) {
322
+ if (e === t) return !0;
323
+ if (e == null || t === null || t === void 0) return !1;
324
+ if (typeof e == "boolean" && (e = e ? 1 : 0), typeof t == "boolean" && (t = t ? 1 : 0), typeof e == "number" && typeof t == "string") {
325
+ const r = Number(t);
326
+ return !Number.isNaN(r) && e === r;
327
+ }
328
+ if (typeof t == "number" && typeof e == "string") {
329
+ const r = Number(e);
330
+ return !Number.isNaN(r) && r === t;
331
+ }
332
+ return !1;
333
+ }
334
+ function X(e, t, r, n, s, i) {
335
+ const l = (u, m) => s ? Object.is(u, m) : R(u, m);
336
+ if (e.type === "variable") {
337
+ const u = i.byName.get(e.name);
338
+ if (u) {
339
+ const m = u.labelToValue.has(String(r)) ? u.labelToValue.get(String(r)) : r;
340
+ let p = n;
341
+ return typeof n == "string" && u.labelToValue.has(n) && (p = u.labelToValue.get(n)), l(m, p);
342
+ }
343
+ }
344
+ if (t.type === "variable") {
345
+ const u = i.byName.get(t.name);
346
+ if (u) {
347
+ const m = u.labelToValue.has(String(n)) ? u.labelToValue.get(String(n)) : n;
348
+ let p = r;
349
+ return typeof r == "string" && u.labelToValue.has(r) && (p = u.labelToValue.get(r)), l(p, m);
350
+ }
351
+ }
352
+ return null;
353
+ }
354
+ function z(e, t, r) {
355
+ let n, s;
356
+ if (typeof t == "number" && typeof r == "number")
357
+ n = t, s = r;
358
+ else if (typeof t == "string" && typeof r == "string")
359
+ n = t, s = r;
360
+ else {
361
+ const i = typeof t == "boolean" ? t ? 1 : 0 : typeof t == "string" ? Number(t) : t, l = typeof r == "boolean" ? r ? 1 : 0 : typeof r == "string" ? Number(r) : r;
362
+ if (typeof i == "number" && !Number.isNaN(i) || typeof l == "number" && !Number.isNaN(l))
363
+ if (typeof i == "number" && typeof l == "number" && !Number.isNaN(i) && !Number.isNaN(l))
364
+ n = i, s = l;
365
+ else
366
+ throw new y(`无法比较 ${w(t)} 与 ${w(r)}`, 0, 0);
367
+ else
368
+ throw new y(`无法比较 ${w(t)} 与 ${w(r)}`, 0, 0);
369
+ }
370
+ switch (e) {
371
+ case "<":
372
+ return n < s;
373
+ case "<=":
374
+ return n <= s;
375
+ case ">":
376
+ return n > s;
377
+ case ">=":
378
+ return n >= s;
379
+ default:
380
+ return !1;
381
+ }
382
+ }
383
+ function Z(e, t) {
384
+ return e === t ? `${e}` : t === 1 / 0 ? `至少 ${e}` : `${e}~${t}`;
385
+ }
386
+ function J(e, t, r, n) {
387
+ const s = A(e.object, t, r, n);
388
+ let i;
389
+ if (e.index !== null ? i = A(e.index, t, r, n) : i = e.prop, s == null)
390
+ throw new y(`无法访问 ${String(i)}:左侧是 ${w(s)}`, e.start, e.end);
391
+ if (Array.isArray(s)) {
392
+ const l = typeof i == "number" ? i : Number(i);
393
+ if (!Number.isInteger(l) || l < 0 || l >= s.length)
394
+ throw new y(`数组下标 ${String(i)} 越界(长度 ${s.length})`, e.start, e.end);
395
+ return s[l];
396
+ }
397
+ if (typeof s == "object") {
398
+ const l = String(i);
399
+ if (!(l in s))
400
+ throw new y(`对象中不存在字段 "${l}"`, e.start, e.end);
401
+ return s[l];
402
+ }
403
+ throw new y(`无法访问 ${String(i)}:${w(s)} 不是对象或数组`, e.start, e.end);
404
+ }
405
+ function Q(e, t, r = {}, n) {
406
+ return A(e, t, r, q(n));
407
+ }
408
+ function A(e, t, r, n) {
409
+ switch (e.type) {
410
+ case "number":
411
+ return e.value;
412
+ case "string":
413
+ return e.value;
414
+ case "boolean":
415
+ return e.value;
416
+ case "null":
417
+ return null;
418
+ case "variable": {
419
+ if (!(e.name in t))
420
+ throw new y(
421
+ `变量 {${e.name}} 未定义,请先在测试值中填写`,
422
+ e.start,
423
+ e.end
424
+ );
425
+ return t[e.name];
426
+ }
427
+ case "member":
428
+ return J(e, t, r, n);
429
+ case "unary": {
430
+ const s = A(e.operand, t, r, n);
431
+ if (e.op === "!") return !s;
432
+ if (e.op === "-") return -M(s, '一元 "-"');
433
+ if (e.op === "+") return +M(s, '一元 "+"');
434
+ throw new y(`未知一元运算符 "${e.op}"`, e.start, e.end);
435
+ }
436
+ case "binary": {
437
+ if (e.op === "&&" || e.op === "AND")
438
+ return A(e.left, t, r, n) ? !!A(e.right, t, r, n) : !1;
439
+ if (e.op === "||" || e.op === "OR")
440
+ return A(e.left, t, r, n) ? !0 : !!A(e.right, t, r, n);
441
+ if (e.op === "==" || e.op === "!=" || e.op === "===" || e.op === "!==") {
442
+ const l = A(e.left, t, r, n), u = A(e.right, t, r, n), m = e.op === "===" || e.op === "!==", p = e.op === "!=" || e.op === "!==", f = X(e.left, e.right, l, u, m, n), o = f !== null ? f : m ? Object.is(l, u) : R(l, u);
443
+ return p ? !o : o;
444
+ }
445
+ const s = A(e.left, t, r, n), i = A(e.right, t, r, n);
446
+ switch (e.op) {
447
+ case "<":
448
+ case "<=":
449
+ case ">":
450
+ case ">=":
451
+ return z(e.op, s, i);
452
+ case "+": {
453
+ if (typeof s == "string" || typeof i == "string") return String(s) + String(i);
454
+ if (typeof s == "number" && typeof i == "number") return s + i;
455
+ throw new y(
456
+ `"+" 需要数字或字符串操作数,得到 ${w(s)} 与 ${w(i)}`,
457
+ e.start,
458
+ e.end
459
+ );
460
+ }
461
+ case "-":
462
+ case "*":
463
+ case "/":
464
+ case "%":
465
+ case "**": {
466
+ if (typeof s != "number" || typeof i != "number")
467
+ throw new y(
468
+ `"${e.op}" 需要数字操作数,得到 ${w(s)} 与 ${w(i)}`,
469
+ e.start,
470
+ e.end
471
+ );
472
+ if (e.op === "/" && i === 0)
473
+ throw new y("除法运算中除数不能为 0", e.start, e.end);
474
+ if (e.op === "%" && i === 0)
475
+ throw new y("取余运算中除数不能为 0", e.start, e.end);
476
+ switch (e.op) {
477
+ case "-":
478
+ return s - i;
479
+ case "*":
480
+ return s * i;
481
+ case "/":
482
+ return s / i;
483
+ case "%":
484
+ return s % i;
485
+ case "**":
486
+ return Math.pow(s, i);
487
+ default:
488
+ throw new y(`未知运算符 "${e.op}"`, e.start, e.end);
489
+ }
490
+ }
491
+ default:
492
+ throw new y(`未知运算符 "${e.op}"`, e.start, e.end);
493
+ }
494
+ }
495
+ case "conditional": {
496
+ const s = A(e.test, t, r, n);
497
+ return A(s ? e.consequent : e.alternate, t, r, n);
498
+ }
499
+ case "if": {
500
+ for (const s of e.branches)
501
+ if (A(s.condition, t, r, n)) return A(s.body, t, r, n);
502
+ return e.elseBody ? A(e.elseBody, t, r, n) : null;
503
+ }
504
+ case "call": {
505
+ const s = e.name, i = r[s], l = F[s];
506
+ if (!i && !l)
507
+ throw new y(
508
+ `未知函数 ${s}()(内置函数:${Object.keys(F).join("、")})`,
509
+ e.start,
510
+ e.end
511
+ );
512
+ if (s === "IF") {
513
+ if (e.args.length !== 3)
514
+ throw new y(
515
+ `函数 IF() 需要 3 个参数:IF(条件, 真值, 假值),实际 ${e.args.length} 个`,
516
+ e.start,
517
+ e.end
518
+ );
519
+ const m = A(e.args[0], t, r, n);
520
+ return A(m ? e.args[1] : e.args[2], t, r, n);
521
+ }
522
+ if (s === "IFS") {
523
+ if (e.args.length < 3)
524
+ throw new y(
525
+ "函数 IFS() 至少需要 3 个参数:IFS(条件1, 值1, 条件2, 值2, …, 默认值)",
526
+ e.start,
527
+ e.end
528
+ );
529
+ const m = e.args.length, p = (m - 1) / 2;
530
+ if (!Number.isInteger(p))
531
+ throw new y(
532
+ "函数 IFS() 参数应为“成对的条件/值”,最后可跟一个默认值",
533
+ e.start,
534
+ e.end
535
+ );
536
+ for (let f = 0; f < p; f++)
537
+ if (A(e.args[f * 2], t, r, n)) return A(e.args[f * 2 + 1], t, r, n);
538
+ return A(e.args[m - 1], t, r, n);
539
+ }
540
+ const u = e.args.map((m) => A(m, t, r, n));
541
+ if (l) {
542
+ if (u.length < l.minArgs || u.length > l.maxArgs)
543
+ throw new y(
544
+ `函数 ${s}() 需要 ${Z(l.minArgs, l.maxArgs)} 个参数,实际 ${u.length} 个`,
545
+ e.start,
546
+ e.end
547
+ );
548
+ return l.fn(u);
549
+ }
550
+ return i(u);
551
+ }
552
+ default: {
553
+ const s = e;
554
+ throw new y("无法求值的表达式", s.start, s.end);
555
+ }
556
+ }
557
+ }
558
+ function ee(e) {
559
+ return e == null ? "" : String(e);
560
+ }
561
+ function B(e, t, r) {
562
+ if (Array.isArray(e)) return e;
563
+ throw new y(`函数 ${t}() 的第 ${r + 1} 个参数需要数组,实际为 ${w(e)}`, 0, 0);
564
+ }
565
+ function j(e, t, r) {
566
+ if (e !== null && typeof e == "object" && !Array.isArray(e)) return e;
567
+ throw new y(`函数 ${t}() 的第 ${r + 1} 个参数需要对象,实际为 ${w(e)}`, 0, 0);
568
+ }
569
+ const F = {
570
+ /* ---------- 逻辑 ---------- */
571
+ IF: {
572
+ name: "IF",
573
+ signature: "IF(条件, 真值, 假值)",
574
+ description: "条件判断:条件成立返回真值,否则返回假值(短路求值)。",
575
+ minArgs: 3,
576
+ maxArgs: 3,
577
+ params: ["条件:布尔表达式", "真值:条件成立时返回", "假值:条件不成立时返回"],
578
+ example: "IF({年龄} >= 18, '成年', '未成年')",
579
+ fn: () => null
580
+ // 在 evaluate 中特判
581
+ },
582
+ IFS: {
583
+ name: "IFS",
584
+ signature: "IFS(条件1, 值1, 条件2, 值2, …, 默认值)",
585
+ description: "多级条件判断:依次判断,命中即返回对应值;都不命中返回默认值。",
586
+ minArgs: 3,
587
+ maxArgs: 1 / 0,
588
+ params: ["条件/值成对出现", "…", "默认值(可选)"],
589
+ example: "IFS({等级} == '黑金', 0.8, {等级} == '金卡', 0.85, 1)",
590
+ fn: () => null
591
+ },
592
+ AND: {
593
+ name: "AND",
594
+ signature: "AND(条件1, 条件2, …)",
595
+ description: "逻辑与:全部为真才返回 true。",
596
+ minArgs: 1,
597
+ maxArgs: 1 / 0,
598
+ params: ["条件:布尔表达式,至少 1 个"],
599
+ example: "AND({金额} > 100, {包邮})",
600
+ fn: (e) => e.every(Boolean)
601
+ },
602
+ OR: {
603
+ name: "OR",
604
+ signature: "OR(条件1, 条件2, …)",
605
+ description: "逻辑或:任一为真即返回 true。",
606
+ minArgs: 1,
607
+ maxArgs: 1 / 0,
608
+ params: ["条件:布尔表达式,至少 1 个"],
609
+ example: "OR({等级} == '金卡', {等级} == '黑金')",
610
+ fn: (e) => e.some(Boolean)
611
+ },
612
+ NOT: {
613
+ name: "NOT",
614
+ signature: "NOT(条件)",
615
+ description: "逻辑非:取反。",
616
+ minArgs: 1,
617
+ maxArgs: 1,
618
+ params: ["条件:布尔表达式"],
619
+ example: "NOT({包邮})",
620
+ fn: (e) => !e[0]
621
+ },
622
+ IFNULL: {
623
+ name: "IFNULL",
624
+ signature: "IFNULL(值, 兜底值)",
625
+ description: "值为 NULL / 未定义时返回兜底值,否则返回值本身。",
626
+ minArgs: 2,
627
+ maxArgs: 2,
628
+ params: ["值:待判断的值", "兜底值:为空时返回"],
629
+ example: "IFNULL({优惠券码}, '无优惠')",
630
+ fn: (e) => e[0] === null || e[0] === void 0 ? e[1] : e[0]
631
+ },
632
+ COALESCE: {
633
+ name: "COALESCE",
634
+ signature: "COALESCE(值1, 值2, …)",
635
+ description: "返回第一个非 NULL 的值;全部为空返回 NULL。",
636
+ minArgs: 1,
637
+ maxArgs: 1 / 0,
638
+ params: ["值列表:从前往后取第一个非空"],
639
+ example: "COALESCE({备注}, {优惠券码}, '无')",
640
+ fn: (e) => {
641
+ for (const t of e) if (t != null) return t;
642
+ return null;
643
+ }
644
+ },
645
+ SWITCH: {
646
+ name: "SWITCH",
647
+ signature: "SWITCH(值, 情况1, 结果1, …, 默认值)",
648
+ description: "按值匹配:与某个情况相等则返回对应结果;都不匹配返回默认值(可选)。",
649
+ minArgs: 2,
650
+ maxArgs: 1 / 0,
651
+ params: ["值:待匹配值", "情况/结果成对出现", "…", "默认值(可选)"],
652
+ example: "SWITCH({会员等级}, '金卡', 0.85, '黑金', 0.8, 1)",
653
+ fn: (e) => {
654
+ const t = e[0], r = e.slice(1), n = Math.floor(r.length / 2);
655
+ for (let s = 0; s < n; s++)
656
+ if (R(t, r[s * 2])) return r[s * 2 + 1];
657
+ return r.length % 2 === 1 ? r[r.length - 1] : null;
658
+ }
659
+ },
660
+ /* ---------- 数字 ---------- */
661
+ ABS: {
662
+ name: "ABS",
663
+ signature: "ABS(数字)",
664
+ description: "取绝对值。",
665
+ minArgs: 1,
666
+ maxArgs: 1,
667
+ params: ["数字"],
668
+ example: "ABS(-9)",
669
+ fn: (e) => Math.abs(S(e[0], "ABS", 0))
670
+ },
671
+ ROUND: {
672
+ name: "ROUND",
673
+ signature: "ROUND(数字[, 小数位])",
674
+ description: "四舍五入到指定小数位(默认 0 位)。",
675
+ minArgs: 1,
676
+ maxArgs: 2,
677
+ params: ["数字", "小数位(可选,默认 0)"],
678
+ example: "ROUND(3.14159, 2)",
679
+ fn: (e) => {
680
+ const t = S(e[0], "ROUND", 0), r = e.length > 1 ? S(e[1], "ROUND", 1) : 0, n = Math.pow(10, r);
681
+ return Math.round(t * n) / n;
682
+ }
683
+ },
684
+ FLOOR: {
685
+ name: "FLOOR",
686
+ signature: "FLOOR(数字)",
687
+ description: "向下取整。",
688
+ minArgs: 1,
689
+ maxArgs: 1,
690
+ params: ["数字"],
691
+ example: "FLOOR(3.7)",
692
+ fn: (e) => Math.floor(S(e[0], "FLOOR", 0))
693
+ },
694
+ CEIL: {
695
+ name: "CEIL",
696
+ signature: "CEIL(数字)",
697
+ description: "向上取整。",
698
+ minArgs: 1,
699
+ maxArgs: 1,
700
+ params: ["数字"],
701
+ example: "CEIL(3.2)",
702
+ fn: (e) => Math.ceil(S(e[0], "CEIL", 0))
703
+ },
704
+ MIN: {
705
+ name: "MIN",
706
+ signature: "MIN(数字1, 数字2, …)",
707
+ description: "取最小值。",
708
+ minArgs: 1,
709
+ maxArgs: 1 / 0,
710
+ params: ["数字列表"],
711
+ example: "MIN({金额}, 100)",
712
+ fn: (e) => Math.min(...e.map((t, r) => S(t, "MIN", r)))
713
+ },
714
+ MAX: {
715
+ name: "MAX",
716
+ signature: "MAX(数字1, 数字2, …)",
717
+ description: "取最大值。",
718
+ minArgs: 1,
719
+ maxArgs: 1 / 0,
720
+ params: ["数字列表"],
721
+ example: "MAX({金额}, 0)",
722
+ fn: (e) => Math.max(...e.map((t, r) => S(t, "MAX", r)))
723
+ },
724
+ SUM: {
725
+ name: "SUM",
726
+ signature: "SUM(数字1, 数字2, …)",
727
+ description: "求数值总和(忽略非数字参数)。",
728
+ minArgs: 1,
729
+ maxArgs: 1 / 0,
730
+ params: ["数字列表(可引用数组变量)"],
731
+ example: "SUM(1, 2, 3)",
732
+ fn: (e) => e.reduce((t, r) => t + k(r), 0)
733
+ },
734
+ AVERAGE: {
735
+ name: "AVERAGE",
736
+ signature: "AVERAGE(数字1, 数字2, …)",
737
+ description: "求数值平均值(忽略非数字参数)。",
738
+ minArgs: 1,
739
+ maxArgs: 1 / 0,
740
+ params: ["数字列表"],
741
+ example: "AVERAGE(2, 4, 6)",
742
+ fn: (e) => {
743
+ const t = [];
744
+ for (const r of e)
745
+ typeof r == "number" ? t.push(r) : typeof r == "string" && r.trim() !== "" && !Number.isNaN(Number(r)) && t.push(Number(r));
746
+ return t.length ? t.reduce((r, n) => r + n, 0) / t.length : 0;
747
+ }
748
+ },
749
+ /* ---------- 文本 ---------- */
750
+ CONCAT: {
751
+ name: "CONCAT",
752
+ signature: "CONCAT(文本1, 文本2, …)",
753
+ description: "拼接多个值为一个字符串。",
754
+ minArgs: 1,
755
+ maxArgs: 1 / 0,
756
+ params: ["值列表(非文本会转成字符串)"],
757
+ example: "CONCAT('订单#', {订单编号})",
758
+ fn: (e) => e.map((t) => String(t ?? "")).join("")
759
+ },
760
+ LEN: {
761
+ name: "LEN",
762
+ signature: "LEN(文本)",
763
+ description: "返回文本长度(按字符数)。",
764
+ minArgs: 1,
765
+ maxArgs: 1,
766
+ params: ["文本"],
767
+ example: "LEN('你好abc')",
768
+ fn: (e) => String(e[0] ?? "").length
769
+ },
770
+ UPPER: {
771
+ name: "UPPER",
772
+ signature: "UPPER(文本)",
773
+ description: "转大写。",
774
+ minArgs: 1,
775
+ maxArgs: 1,
776
+ params: ["文本"],
777
+ example: "UPPER('ab')",
778
+ fn: (e) => String(e[0] ?? "").toUpperCase()
779
+ },
780
+ LOWER: {
781
+ name: "LOWER",
782
+ signature: "LOWER(文本)",
783
+ description: "转小写。",
784
+ minArgs: 1,
785
+ maxArgs: 1,
786
+ params: ["文本"],
787
+ example: "LOWER('AB')",
788
+ fn: (e) => String(e[0] ?? "").toLowerCase()
789
+ },
790
+ TRIM: {
791
+ name: "TRIM",
792
+ signature: "TRIM(文本)",
793
+ description: "去除首尾空白。",
794
+ minArgs: 1,
795
+ maxArgs: 1,
796
+ params: ["文本"],
797
+ example: "TRIM(' hi ')",
798
+ fn: (e) => String(e[0] ?? "").trim()
799
+ },
800
+ SUBSTR: {
801
+ name: "SUBSTR",
802
+ signature: "SUBSTR(文本, 起始下标[, 长度])",
803
+ description: '截取子串(0 起始,如 SUBSTR("hello", 1, 3) = "ell")。',
804
+ minArgs: 2,
805
+ maxArgs: 3,
806
+ params: ["文本", "起始下标(0 起始)", "长度(可选)"],
807
+ example: "SUBSTR('hello', 1, 3)",
808
+ fn: (e) => {
809
+ const t = String(e[0] ?? ""), r = S(e[1], "SUBSTR", 1), n = e.length > 2 ? S(e[2], "SUBSTR", 2) : void 0;
810
+ return n === void 0 ? t.slice(r) : t.slice(r, r + n);
811
+ }
812
+ },
813
+ MID: {
814
+ name: "MID",
815
+ signature: "MID(文本, 起始位, 长度)",
816
+ description: 'Excel 风格截取(1 起始,如 MID("hello", 2, 3) = "ell")。',
817
+ minArgs: 3,
818
+ maxArgs: 3,
819
+ params: ["文本", "起始位(1 起始)", "长度"],
820
+ example: "MID('hello', 2, 3)",
821
+ fn: (e) => {
822
+ const t = String(e[0] ?? ""), r = S(e[1], "MID", 1), n = S(e[2], "MID", 2);
823
+ return t.slice(r - 1, r - 1 + n);
824
+ }
825
+ },
826
+ LEFT: {
827
+ name: "LEFT",
828
+ signature: "LEFT(文本, 长度)",
829
+ description: "取左侧 N 个字符。",
830
+ minArgs: 2,
831
+ maxArgs: 2,
832
+ params: ["文本", "长度"],
833
+ example: "LEFT('hello', 2)",
834
+ fn: (e) => String(e[0] ?? "").slice(0, S(e[1], "LEFT", 1))
835
+ },
836
+ RIGHT: {
837
+ name: "RIGHT",
838
+ signature: "RIGHT(文本, 长度)",
839
+ description: "取右侧 N 个字符。",
840
+ minArgs: 2,
841
+ maxArgs: 2,
842
+ params: ["文本", "长度"],
843
+ example: "RIGHT('hello', 2)",
844
+ fn: (e) => String(e[0] ?? "").slice(-S(e[1], "RIGHT", 1))
845
+ },
846
+ CONTAINS: {
847
+ name: "CONTAINS",
848
+ signature: "CONTAINS(文本, 子串)",
849
+ description: "文本是否包含子串(大小写敏感)。",
850
+ minArgs: 2,
851
+ maxArgs: 2,
852
+ params: ["文本", "子串"],
853
+ example: "CONTAINS({优惠券码}, 'FM')",
854
+ fn: (e) => String(e[0] ?? "").includes(String(e[1] ?? ""))
855
+ },
856
+ STARTSWITH: {
857
+ name: "STARTSWITH",
858
+ signature: "STARTSWITH(文本, 前缀)",
859
+ description: "文本是否以指定前缀开头。",
860
+ minArgs: 2,
861
+ maxArgs: 2,
862
+ params: ["文本", "前缀"],
863
+ example: "STARTSWITH({订单编号}, 'A')",
864
+ fn: (e) => String(e[0] ?? "").startsWith(String(e[1] ?? ""))
865
+ },
866
+ ENDSWITH: {
867
+ name: "ENDSWITH",
868
+ signature: "ENDSWITH(文本, 后缀)",
869
+ description: "文本是否以指定后缀结尾。",
870
+ minArgs: 2,
871
+ maxArgs: 2,
872
+ params: ["文本", "后缀"],
873
+ example: "ENDSWITH({订单编号}, '8')",
874
+ fn: (e) => String(e[0] ?? "").endsWith(String(e[1] ?? ""))
875
+ },
876
+ INDEXOF: {
877
+ name: "INDEXOF",
878
+ signature: "INDEXOF(文本, 子串[, 起始下标])",
879
+ description: "返回子串首次出现的位置(0 起始),未找到返回 -1。",
880
+ minArgs: 2,
881
+ maxArgs: 3,
882
+ params: ["文本", "子串", "起始下标(可选,默认 0)"],
883
+ example: "INDEXOF('hello', 'l')",
884
+ fn: (e) => {
885
+ const t = String(e[0] ?? ""), r = String(e[1] ?? ""), n = e.length > 2 ? S(e[2], "INDEXOF", 2) : 0;
886
+ return t.indexOf(r, n);
887
+ }
888
+ },
889
+ REPLACE: {
890
+ name: "REPLACE",
891
+ signature: "REPLACE(文本, 被替换, 替换为)",
892
+ description: "把文本中所有匹配的子串替换为新内容。",
893
+ minArgs: 3,
894
+ maxArgs: 3,
895
+ params: ["文本", "被替换的子串", "替换为"],
896
+ example: "REPLACE('a-b-c', '-', '/')",
897
+ fn: (e) => String(e[0] ?? "").split(String(e[1] ?? "")).join(String(e[2] ?? ""))
898
+ },
899
+ SPLIT: {
900
+ name: "SPLIT",
901
+ signature: "SPLIT(文本, 分隔符)",
902
+ description: "按分隔符拆分为数组。",
903
+ minArgs: 2,
904
+ maxArgs: 2,
905
+ params: ["文本", "分隔符"],
906
+ example: "SPLIT('a,b,c', ',')",
907
+ fn: (e) => String(e[0] ?? "").split(String(e[1] ?? ""))
908
+ },
909
+ /* ---------- 类型转换 ---------- */
910
+ TOSTRING: {
911
+ name: "TOSTRING",
912
+ signature: "TOSTRING(值)",
913
+ description: "转字符串(NULL 转空串)。",
914
+ minArgs: 1,
915
+ maxArgs: 1,
916
+ params: ["值"],
917
+ example: "TOSTRING({金额})",
918
+ fn: (e) => ee(e[0])
919
+ },
920
+ TONUMBER: {
921
+ name: "TONUMBER",
922
+ signature: "TONUMBER(值)",
923
+ description: "转数字;无法转换时报错。",
924
+ minArgs: 1,
925
+ maxArgs: 1,
926
+ params: ["值(数字或数字字符串)"],
927
+ example: "TONUMBER('42')",
928
+ fn: (e) => S(e[0], "TONUMBER", 0)
929
+ },
930
+ TOBOOLEAN: {
931
+ name: "TOBOOLEAN",
932
+ signature: "TOBOOLEAN(值)",
933
+ description: '转布尔:布尔原样;数字非 0 为 true;字符串 "true"/"false"(大小写不敏感)转换,其他为 true(非空)。',
934
+ minArgs: 1,
935
+ maxArgs: 1,
936
+ params: ["值"],
937
+ example: "TOBOOLEAN('true')",
938
+ fn: (e) => {
939
+ const t = e[0];
940
+ if (typeof t == "boolean") return t;
941
+ if (typeof t == "number") return t !== 0;
942
+ if (typeof t == "string") {
943
+ const r = t.trim().toLowerCase();
944
+ return r === "true" ? !0 : r === "false" ? !1 : t.trim() !== "";
945
+ }
946
+ return t != null;
947
+ }
948
+ },
949
+ /* ---------- 日期 ---------- */
950
+ NOW: {
951
+ name: "NOW",
952
+ signature: "NOW()",
953
+ description: "返回当前时间戳(毫秒)。",
954
+ minArgs: 0,
955
+ maxArgs: 0,
956
+ params: [],
957
+ example: "NOW()",
958
+ fn: () => Date.now()
959
+ },
960
+ TODAY: {
961
+ name: "TODAY",
962
+ signature: "TODAY()",
963
+ description: "返回今天零点的时间戳(毫秒)。",
964
+ minArgs: 0,
965
+ maxArgs: 0,
966
+ params: [],
967
+ example: "TODAY()",
968
+ fn: () => {
969
+ const e = /* @__PURE__ */ new Date();
970
+ return e.setHours(0, 0, 0, 0), e.getTime();
971
+ }
972
+ },
973
+ DATEADD: {
974
+ name: "DATEADD",
975
+ signature: "DATEADD(时间戳, 天数)",
976
+ description: "时间戳加 N 天,返回新时间戳。",
977
+ minArgs: 2,
978
+ maxArgs: 2,
979
+ params: ["时间戳(毫秒)", "天数(可为负)"],
980
+ example: "DATEADD(TODAY(), 7)",
981
+ fn: (e) => {
982
+ const t = S(e[0], "DATEADD", 0), r = S(e[1], "DATEADD", 1);
983
+ return t + r * 864e5;
984
+ }
985
+ },
986
+ DATEDIF: {
987
+ name: "DATEDIF",
988
+ signature: "DATEDIF(起始时间戳, 结束时间戳)",
989
+ description: "两个时间戳相隔的整天数。",
990
+ minArgs: 2,
991
+ maxArgs: 2,
992
+ params: ["起始时间戳", "结束时间戳"],
993
+ example: "DATEDIF(TODAY(), DATEADD(TODAY(), 3))",
994
+ fn: (e) => {
995
+ const t = S(e[0], "DATEDIF", 0), r = S(e[1], "DATEDIF", 1);
996
+ return Math.round((r - t) / 864e5);
997
+ }
998
+ },
999
+ /* ---------- 判断 ---------- */
1000
+ ISNULL: {
1001
+ name: "ISNULL",
1002
+ signature: "ISNULL(值)",
1003
+ description: "是否为空值(NULL / 未定义)。",
1004
+ minArgs: 1,
1005
+ maxArgs: 1,
1006
+ params: ["值"],
1007
+ example: "ISNULL({备注})",
1008
+ fn: (e) => e[0] === null || e[0] === void 0
1009
+ },
1010
+ ISEMPTY: {
1011
+ name: "ISEMPTY",
1012
+ signature: "ISEMPTY(值)",
1013
+ description: "是否为空(NULL、空字符串、空数组/对象均视为空)。",
1014
+ minArgs: 1,
1015
+ maxArgs: 1,
1016
+ params: ["值"],
1017
+ example: "ISEMPTY({优惠券码})",
1018
+ fn: (e) => {
1019
+ const t = e[0];
1020
+ return t == null ? !0 : typeof t == "string" ? t.trim() === "" : Array.isArray(t) ? t.length === 0 : typeof t == "object" ? Object.keys(t).length === 0 : !1;
1021
+ }
1022
+ },
1023
+ ISNUMERIC: {
1024
+ name: "ISNUMERIC",
1025
+ signature: "ISNUMERIC(值)",
1026
+ description: "是否为数字(含可转换的数字字符串)。",
1027
+ minArgs: 1,
1028
+ maxArgs: 1,
1029
+ params: ["值"],
1030
+ example: "ISNUMERIC('42')",
1031
+ fn: (e) => {
1032
+ const t = e[0];
1033
+ return typeof t == "number" ? !Number.isNaN(t) : typeof t == "string" ? t.trim() !== "" && !Number.isNaN(Number(t)) : !1;
1034
+ }
1035
+ },
1036
+ BETWEEN: {
1037
+ name: "BETWEEN",
1038
+ signature: "BETWEEN(值, 下限, 上限)",
1039
+ description: "值是否在 [下限, 上限] 区间内(含边界)。",
1040
+ minArgs: 3,
1041
+ maxArgs: 3,
1042
+ params: ["值", "下限", "上限"],
1043
+ example: "BETWEEN({重量}, 1, 5)",
1044
+ fn: (e) => {
1045
+ const t = S(e[0], "BETWEEN", 0), r = S(e[1], "BETWEEN", 1), n = S(e[2], "BETWEEN", 2);
1046
+ return t >= r && t <= n;
1047
+ }
1048
+ },
1049
+ /* ---------- 数组 / 对象 ---------- */
1050
+ COUNT: {
1051
+ name: "COUNT",
1052
+ signature: "COUNT(数组)",
1053
+ description: "返回数组长度;空值返回 0。",
1054
+ minArgs: 1,
1055
+ maxArgs: 1,
1056
+ params: ["数组"],
1057
+ example: "COUNT({商品列表})",
1058
+ fn: (e) => Array.isArray(e[0]) ? e[0].length : 0
1059
+ },
1060
+ COUNTIF: {
1061
+ name: "COUNTIF",
1062
+ signature: "COUNTIF(数组, 目标值)",
1063
+ description: "统计数组中与目标值相等的元素个数(宽松比较)。",
1064
+ minArgs: 2,
1065
+ maxArgs: 2,
1066
+ params: ["数组", "目标值"],
1067
+ example: "COUNTIF({地区列表}, '华东')",
1068
+ fn: (e) => B(e[0], "COUNTIF", 0).filter((r) => R(r, e[1])).length
1069
+ },
1070
+ SUMIF: {
1071
+ name: "SUMIF",
1072
+ signature: "SUMIF(数组, 目标值)",
1073
+ description: "对数组中与目标值相等的数字元素求和(宽松比较,忽略非数字)。",
1074
+ minArgs: 2,
1075
+ maxArgs: 2,
1076
+ params: ["数组", "目标值"],
1077
+ example: "SUMIF({价格列表}, 10)",
1078
+ fn: (e) => B(e[0], "SUMIF", 0).reduce((r, n) => R(n, e[1]) ? r + k(n) : r, 0)
1079
+ },
1080
+ KEYS: {
1081
+ name: "KEYS",
1082
+ signature: "KEYS(对象)",
1083
+ description: "返回对象的键名数组。",
1084
+ minArgs: 1,
1085
+ maxArgs: 1,
1086
+ params: ["对象(键值对变量)"],
1087
+ example: "KEYS({订单})",
1088
+ fn: (e) => Object.keys(j(e[0], "KEYS", 0))
1089
+ },
1090
+ VALUES: {
1091
+ name: "VALUES",
1092
+ signature: "VALUES(对象)",
1093
+ description: "返回对象的键值数组。",
1094
+ minArgs: 1,
1095
+ maxArgs: 1,
1096
+ params: ["对象(键值对变量)"],
1097
+ example: "VALUES({订单})",
1098
+ fn: (e) => Object.values(j(e[0], "VALUES", 0))
1099
+ }
1100
+ }, D = 2;
1101
+ function L(e, t, r) {
1102
+ return r.slice(e.end, t.start).trim();
1103
+ }
1104
+ function T(e, t, r) {
1105
+ switch (e.type) {
1106
+ case "if":
1107
+ return te(e, t, r);
1108
+ case "number":
1109
+ case "string":
1110
+ case "boolean":
1111
+ case "null":
1112
+ case "variable":
1113
+ return t.slice(e.start, e.end).trim();
1114
+ case "member": {
1115
+ const n = T(e.object, t, r);
1116
+ return e.index !== null ? `${n}[${T(e.index, t, r)}]` : `${n}.${e.prop}`;
1117
+ }
1118
+ case "call": {
1119
+ const n = e.args.map((i) => T(i, t, r)).join(", ");
1120
+ return `${t.slice(e.start, e.start + e.name.length).trim()}(${n})`;
1121
+ }
1122
+ case "unary":
1123
+ return `${e.op}${T(e.operand, t, r)}`;
1124
+ case "binary": {
1125
+ const n = t.slice(e.left.end, e.right.start).trim(), s = T(e.left, t, r), i = T(e.right, t, r), l = e.op === "**" && e.right.type === "binary" && e.right.op === "**" ? `(${i})` : i;
1126
+ return `${s} ${n} ${l}`;
1127
+ }
1128
+ case "conditional": {
1129
+ const n = T(e.test, t, r), s = T(e.consequent, t, r), i = T(e.alternate, t, r);
1130
+ return `${n} ? ${s} : ${i}`;
1131
+ }
1132
+ default: {
1133
+ const n = e;
1134
+ return t.slice(n.start, n.end).trim();
1135
+ }
1136
+ }
1137
+ }
1138
+ function te(e, t, r) {
1139
+ const n = " ".repeat(r * D), s = " ".repeat((r + 1) * D), i = [], l = (f) => {
1140
+ f.type === "if" ? i.push(T(f, t, r + 1)) : i.push(`${s}${T(f, t, r + 1)}`);
1141
+ }, u = e.branches[0], m = L({ end: e.start }, u.condition, t), p = L(u.condition, u.body, t);
1142
+ i.push(`${n}${m} ${T(u.condition, t, r)} ${p}`), l(u.body);
1143
+ for (let f = 1; f < e.branches.length; f++) {
1144
+ const o = e.branches[f], N = e.branches[f - 1], g = L(N.body, o.condition, t), a = L(o.condition, o.body, t);
1145
+ i.push(`${n}${g} ${T(o.condition, t, r)} ${a}`), l(o.body);
1146
+ }
1147
+ if (e.elseBody) {
1148
+ const f = e.branches[e.branches.length - 1], o = L(f.body, e.elseBody, t);
1149
+ i.push(`${n}${o}`), l(e.elseBody);
1150
+ }
1151
+ return i.join(`
1152
+ `);
1153
+ }
1154
+ const re = /^(IF|如果)(?=\s|$)/i, ne = /^(ELSE\s+IF|否则如果)(?=\s|$)/i, se = /^(ELSE|否则)(?=\s|$)/i, ae = /(ELSE|否则)$/i;
1155
+ function v(e) {
1156
+ const t = e.split(`
1157
+ `);
1158
+ if (t.length <= 1) return e.trim();
1159
+ const r = [], n = [];
1160
+ let s = 0;
1161
+ const i = (l) => {
1162
+ let u = s;
1163
+ if (ne.test(l))
1164
+ u = n.length ? n[n.length - 1] : 0;
1165
+ else if (se.test(l)) {
1166
+ const m = n.length ? n[n.length - 1] : 0;
1167
+ u = m, n.length && n.pop(), s = m + 1;
1168
+ } else re.test(l) && (u = s, n.push(s), s += 1);
1169
+ r.push(" ".repeat(u * D) + l);
1170
+ };
1171
+ for (const l of t) {
1172
+ const u = l.trim();
1173
+ if (!u) {
1174
+ r.push("");
1175
+ continue;
1176
+ }
1177
+ const m = ae.exec(u);
1178
+ if (m && m.index > 0) {
1179
+ i(u.slice(0, m.index).trim()), i(u.slice(m.index));
1180
+ continue;
1181
+ }
1182
+ i(u);
1183
+ }
1184
+ return r.join(`
1185
+ `);
1186
+ }
1187
+ function le(e) {
1188
+ const t = e.replace(/\r\n/g, `
1189
+ `);
1190
+ if (!t.trim()) return "";
1191
+ try {
1192
+ const r = U(t);
1193
+ return T(r, t, 0) || v(t);
1194
+ } catch {
1195
+ return v(t);
1196
+ }
1197
+ }
1198
+ function ue(e) {
1199
+ return U(e);
1200
+ }
1201
+ function me(e, t = {}, r = {}, n = []) {
1202
+ try {
1203
+ const s = U(e);
1204
+ return { ok: !0, value: Q(s, t, r, n) };
1205
+ } catch (s) {
1206
+ if (s instanceof E)
1207
+ return {
1208
+ ok: !1,
1209
+ error: {
1210
+ message: s.message,
1211
+ start: s.start,
1212
+ end: s.end,
1213
+ severity: "error",
1214
+ kind: "parse"
1215
+ }
1216
+ };
1217
+ if (s instanceof y)
1218
+ return {
1219
+ ok: !1,
1220
+ error: {
1221
+ message: s.message,
1222
+ start: s.start,
1223
+ end: s.end,
1224
+ severity: "error",
1225
+ kind: "runtime"
1226
+ }
1227
+ };
1228
+ throw s;
1229
+ }
1230
+ }
1231
+ function fe(e, t = [], r = {}, n = []) {
1232
+ const s = [];
1233
+ let i = null;
1234
+ if (!e.trim())
1235
+ return { ok: !1, errors: [], ast: null };
1236
+ try {
1237
+ i = U(e);
1238
+ } catch (o) {
1239
+ if (o instanceof E)
1240
+ return {
1241
+ ok: !1,
1242
+ errors: [
1243
+ {
1244
+ message: o.message,
1245
+ start: o.start,
1246
+ end: o.end,
1247
+ severity: "error",
1248
+ kind: "parse"
1249
+ }
1250
+ ],
1251
+ ast: null
1252
+ };
1253
+ throw o;
1254
+ }
1255
+ const l = /* @__PURE__ */ new Set([
1256
+ ...t.map((o) => o.name),
1257
+ ...n
1258
+ ]), u = new Set(t.map((o) => o.name)), m = /* @__PURE__ */ new Map();
1259
+ for (const o of t)
1260
+ o.type === "object" && o.fields && o.fields.length > 0 && m.set(o.name, new Set(o.fields.map((N) => N.key)));
1261
+ const p = /* @__PURE__ */ new Set([
1262
+ ...Object.keys(F),
1263
+ ...Object.keys(r)
1264
+ ]), f = (o) => {
1265
+ switch (o.type) {
1266
+ case "variable": {
1267
+ if (!l.has(o.name)) {
1268
+ const N = u.size > 0 ? `(已定义:${Array.from(u).join("、")})` : "(尚未定义任何变量)";
1269
+ s.push({
1270
+ message: `未定义的变量 {${o.name}}${N}`,
1271
+ start: o.start,
1272
+ end: o.end,
1273
+ severity: "error",
1274
+ kind: "semantic"
1275
+ });
1276
+ }
1277
+ return;
1278
+ }
1279
+ case "member": {
1280
+ if (f(o.object), o.index === null && o.object.type === "variable") {
1281
+ const N = o.object.name, g = m.get(N);
1282
+ g && !g.has(o.prop) && s.push({
1283
+ message: `变量 {${N}} 中不存在字段 "${o.prop}"(已定义:${Array.from(g).join("、")})`,
1284
+ start: o.start,
1285
+ end: o.end,
1286
+ severity: "error",
1287
+ kind: "semantic"
1288
+ });
1289
+ } else o.index && f(o.index);
1290
+ return;
1291
+ }
1292
+ case "call": {
1293
+ const N = r[o.name];
1294
+ if (!p.has(o.name))
1295
+ s.push({
1296
+ message: `未知函数 ${o.name}()(内置:${Object.keys(F).join("、")})`,
1297
+ start: o.start,
1298
+ end: o.end,
1299
+ severity: "error",
1300
+ kind: "semantic"
1301
+ });
1302
+ else if (!N) {
1303
+ const g = F[o.name];
1304
+ if (g && (o.args.length < g.minArgs || o.args.length > g.maxArgs)) {
1305
+ const a = g.minArgs === g.maxArgs ? `${g.minArgs}` : g.maxArgs === 1 / 0 ? `至少 ${g.minArgs}` : `${g.minArgs}~${g.maxArgs}`;
1306
+ s.push({
1307
+ message: `函数 ${o.name}() 需要 ${a} 个参数,实际 ${o.args.length} 个`,
1308
+ start: o.start,
1309
+ end: o.end,
1310
+ severity: "error",
1311
+ kind: "semantic"
1312
+ });
1313
+ }
1314
+ }
1315
+ o.args.forEach(f);
1316
+ return;
1317
+ }
1318
+ case "binary":
1319
+ f(o.left), f(o.right);
1320
+ return;
1321
+ case "unary":
1322
+ f(o.operand);
1323
+ return;
1324
+ case "conditional":
1325
+ f(o.test), f(o.consequent), f(o.alternate);
1326
+ return;
1327
+ case "if":
1328
+ for (const N of o.branches)
1329
+ f(N.condition), f(N.body);
1330
+ o.elseBody && f(o.elseBody);
1331
+ return;
1332
+ default:
1333
+ return;
1334
+ }
1335
+ };
1336
+ return f(i), { ok: s.length === 0, errors: s, ast: i };
1337
+ }
1338
+ function ce(e, t = 80) {
1339
+ let r;
1340
+ if (e == null) r = "NULL";
1341
+ else if (typeof e == "string") r = `"${e}"`;
1342
+ else if (typeof e == "number")
1343
+ r = Number.isInteger(e) ? String(e) : String(Math.round(e * 1e6) / 1e6);
1344
+ else if (typeof e == "boolean") r = String(e);
1345
+ else if (Array.isArray(e) || typeof e == "object")
1346
+ try {
1347
+ r = JSON.stringify(e);
1348
+ } catch {
1349
+ r = String(e);
1350
+ }
1351
+ else r = String(e);
1352
+ return r.length > t ? r.slice(0, t) + "…" : r;
1353
+ }
1354
+ function pe(e) {
1355
+ return e == null ? "空" : typeof e == "number" ? "数字" : typeof e == "string" ? "文本" : typeof e == "boolean" ? "布尔" : Array.isArray(e) ? "数组" : typeof e == "object" ? "对象" : "其他";
1356
+ }
1357
+ function ge(e) {
1358
+ const t = [];
1359
+ for (const r of W(e))
1360
+ r.type === "error" && t.push({
1361
+ message: `无法识别的字符 "${r.raw}"`,
1362
+ start: r.start,
1363
+ end: r.end,
1364
+ severity: "error",
1365
+ kind: "lex"
1366
+ });
1367
+ return t;
1368
+ }
1369
+ export {
1370
+ F as BUILTIN_FUNCTIONS,
1371
+ E as ParseError,
1372
+ y as RuntimeError,
1373
+ Q as evaluate,
1374
+ me as evaluateFormula,
1375
+ le as formatFormula,
1376
+ ce as formatValue,
1377
+ ge as lexErrors,
1378
+ U as parse,
1379
+ ue as parseFormula,
1380
+ W as tokenize,
1381
+ fe as validateFormula,
1382
+ pe as valueTypeName
1383
+ };