a-calc 3.0.0-beta.2025120808 → 3.0.0-beta.20260121
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.
- package/README.md +83 -403
- package/a-calc.versions.js +3 -2
- package/browser/index.js +8 -1
- package/calc.d.ts +522 -84
- package/cjs/index.js +8 -1
- package/es/index.js +8 -1
- package/package.json +18 -7
package/calc.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { U, StrRemoveSome, If_StrIncludes, Or, Equal } from "typescript-treasure";
|
|
2
2
|
|
|
3
3
|
export type InjectDataFn<Expr> = <const Data>
|
|
4
|
-
(
|
|
4
|
+
(fill_data: Data extends string | number | undefined | null ? never : Data) =>
|
|
5
5
|
(
|
|
6
6
|
If_HasNumFmt<Expr, Data> extends true ?
|
|
7
7
|
number | GetWrapErr<Data>
|
|
@@ -11,7 +11,7 @@ export type InjectDataFn<Expr> = <const Data>
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
export type InjectExprFn<Data> = <const Expr>
|
|
14
|
-
(
|
|
14
|
+
(expr: Expr extends string | number ? Expr : never) =>
|
|
15
15
|
(
|
|
16
16
|
If_HasNumFmt<Expr, Data> extends true ?
|
|
17
17
|
number | GetWrapErr<Data>
|
|
@@ -20,7 +20,7 @@ export type InjectExprFn<Data> = <const Expr>
|
|
|
20
20
|
);
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
type If_IncludesVar<T> = U.If_IncludeVar<StrRemoveSome<T, ["!n","!e","!u","!N","!E","!U"]>>;
|
|
23
|
+
type If_IncludesVar<T> = U.If_IncludeVar<StrRemoveSome<T, ["!n", "!e", "!u", "!N", "!E", "!U"]>>;
|
|
24
24
|
|
|
25
25
|
|
|
26
26
|
type If_NumOrStr<T> = T extends string | number ? true : false;
|
|
@@ -74,12 +74,20 @@ type CalcConfig<Fmt extends string, Err> =
|
|
|
74
74
|
_unit: boolean,
|
|
75
75
|
_fmt: Fmt,
|
|
76
76
|
_memo: boolean,
|
|
77
|
-
_mode: "space" | "space-all" | "normal", // space 只会影响表达式部分 space_all 也会影响 fmt 部分
|
|
78
77
|
[Prop: string]: any;
|
|
79
78
|
}>;
|
|
80
79
|
|
|
81
80
|
export type Calc = <const Expr extends string | number, const Fmt extends string, const Err = never>
|
|
82
|
-
(
|
|
81
|
+
(expr: Expr, options?: CalcConfig<Fmt, Err>) =>
|
|
82
|
+
(
|
|
83
|
+
If_StrIncludes<Fmt, ["!n"]> & If_StrIncludes<Expr, ["!n"]> extends true ?
|
|
84
|
+
number | Err
|
|
85
|
+
:
|
|
86
|
+
string | Err
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
export type CalcSpace = <const Expr extends string | number, const Fmt extends string, const Err = never>
|
|
90
|
+
(expr: Expr, options?: CalcConfig<Fmt, Err>) =>
|
|
83
91
|
(
|
|
84
92
|
If_StrIncludes<Fmt, ["!n"]> & If_StrIncludes<Expr, ["!n"]> extends true ?
|
|
85
93
|
number | Err
|
|
@@ -88,7 +96,7 @@ export type Calc = <const Expr extends string | number, const Fmt extends string
|
|
|
88
96
|
);
|
|
89
97
|
|
|
90
98
|
export type CalcSum = <const Expr extends string | number, const Fmt extends string, const Err = never>
|
|
91
|
-
(
|
|
99
|
+
(expr: Expr, options: Array<CalcConfig<Fmt, Err>>) =>
|
|
92
100
|
(
|
|
93
101
|
If_StrIncludes<Fmt, ["!n"]> & If_StrIncludes<Expr, ["!n"]> extends true ?
|
|
94
102
|
number | Err
|
|
@@ -96,16 +104,51 @@ export type CalcSum = <const Expr extends string | number, const Fmt extends str
|
|
|
96
104
|
string | Err
|
|
97
105
|
);
|
|
98
106
|
|
|
99
|
-
|
|
100
|
-
|
|
107
|
+
/**
|
|
108
|
+
* fmt 格式化选项
|
|
109
|
+
*/
|
|
110
|
+
export interface FmtOptions {
|
|
111
|
+
_error?: any;
|
|
112
|
+
_fmt?: string;
|
|
113
|
+
/** 交易量单位数组(仅选项,临时覆盖) */
|
|
114
|
+
_vol_units?: string[];
|
|
115
|
+
/** 交易量阶梯值(仅选项,临时覆盖) */
|
|
116
|
+
_vol_step?: number | number[];
|
|
117
|
+
/** 交易量预设配置(仅选项) */
|
|
118
|
+
_vol?: Record<string, VolumePreset>;
|
|
119
|
+
_unit_convert_out?: Record<string, any>;
|
|
120
|
+
_unit_convert_in?: Record<string, any>;
|
|
121
|
+
_unit_default_out?: string | string[];
|
|
122
|
+
_unit_default_in?: string | string[];
|
|
123
|
+
_unit_default_position?: 'before' | 'after' | 'middle';
|
|
124
|
+
_unit_default_positions?: Record<string, 'before' | 'after' | 'middle'>;
|
|
125
|
+
_thousands?: Record<string, any>;
|
|
126
|
+
_unit_thousands?: Record<string, string>;
|
|
127
|
+
_unit_vol?: Record<string, string>;
|
|
128
|
+
[key: string]: any;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* fmt 函数类型
|
|
133
|
+
* 直接格式化数字,跳过表达式解析
|
|
134
|
+
*/
|
|
135
|
+
export interface Fmt {
|
|
136
|
+
<const FmtStr extends string | undefined = undefined, const Err = never>(
|
|
137
|
+
value: number | string,
|
|
138
|
+
format_str?: FmtStr,
|
|
139
|
+
options?: FmtOptions & { _error?: Err }
|
|
140
|
+
): If_StrIncludes<FmtStr, ["!n"]> extends true ? number | Err : string | Err;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface CalcWrap {
|
|
101
144
|
<const Expr extends string | number>
|
|
102
|
-
(
|
|
145
|
+
(expr: Expr): If_IncludesVar<Expr> extends true ? FuncReturn<Expr> : If_StrIncludes<Expr, ["!n", "!N"]> extends true ? number | "-" : string | "-";
|
|
103
146
|
|
|
104
147
|
<const Expr extends string | number, const Fmt extends string, const Err, const Options extends CalcConfig<Fmt, Err>>
|
|
105
|
-
(
|
|
148
|
+
(expr: Expr, options: Options): DirectReturn<Expr, Options>;
|
|
106
149
|
|
|
107
150
|
<const Fmt extends string, const Err, const Options extends CalcConfig<Fmt, Err>>
|
|
108
|
-
(
|
|
151
|
+
(options: Options): FuncReturn<Options>;
|
|
109
152
|
}
|
|
110
153
|
|
|
111
154
|
declare const calc_util: {
|
|
@@ -120,7 +163,7 @@ declare const calc_util: {
|
|
|
120
163
|
};
|
|
121
164
|
|
|
122
165
|
/**
|
|
123
|
-
*
|
|
166
|
+
* 传入字符串算术式或数字返回计算的结果(标准语法)
|
|
124
167
|
* @param expr 一个字符串算术式或一个数字
|
|
125
168
|
* @param options 一个配置或者用来填充算术式的数据源,或者两者的结合
|
|
126
169
|
* @returns 返回进过计算和格式化之后的结果,结果可能是字符串或数字或自定义的错误类型
|
|
@@ -137,6 +180,24 @@ declare const calc_util: {
|
|
|
137
180
|
*/
|
|
138
181
|
export const calc: Calc;
|
|
139
182
|
|
|
183
|
+
/**
|
|
184
|
+
* 空格语法计算函数
|
|
185
|
+
* 使用空格分隔的语法,支持所有 3.x 新功能(千分位预设、格式化分组、范围限制等)
|
|
186
|
+
* @param expr 一个字符串算术式或一个数字(使用空格语法)
|
|
187
|
+
* @param options 一个配置或者用来填充算术式的数据源,或者两者的结合
|
|
188
|
+
* @returns 返回进过计算和格式化之后的结果,结果可能是字符串或数字或自定义的错误类型
|
|
189
|
+
*
|
|
190
|
+
* @example:
|
|
191
|
+
* ```typescript
|
|
192
|
+
* calc_space("a + b", {a: 1, b: 2}) // 3
|
|
193
|
+
* calc_space("1 + 2 * 3 | = 2") // "7.00"
|
|
194
|
+
* calc_space("100 | , = 2") // "100.00"
|
|
195
|
+
* calc_space("1000000 | @cn") // "1,000,000"(使用千分位预设)
|
|
196
|
+
* calc_space("100 | % = 2") // "100.00%"
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
export const calc_space: CalcSpace;
|
|
200
|
+
|
|
140
201
|
/**
|
|
141
202
|
* 传入字符串算术式和数据源的数组进行计算并最终归集,这个方法类似 options_arr.reduce((memo, data) => calc("expr + memo", {expr, memo, _fill_data: data}), 0)
|
|
142
203
|
* @param expr 一个字符串算术式或一个数字
|
|
@@ -150,12 +211,33 @@ export const calc: Calc;
|
|
|
150
211
|
*/
|
|
151
212
|
export const calc_sum: CalcSum;
|
|
152
213
|
/**
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
214
|
+
* 专用格式化函数,用于对数字进行格式化处理
|
|
215
|
+
* 相比 calc,它跳过表达式解析,性能更好
|
|
216
|
+
*
|
|
217
|
+
* 支持多种调用方式:
|
|
218
|
+
* 1. 新 API: fmt(100, '=2') - 直接格式化数字,性能最好
|
|
219
|
+
* 2. 旧 API: fmt('100 | =2') - calc 风格(向后兼容)
|
|
220
|
+
* 3. 旧 API: fmt(100, { _fmt: '=2' }) - 对象选项风格(向后兼容)
|
|
221
|
+
*
|
|
222
|
+
* @param value 要格式化的数字,或包含 | 的 calc 风格表达式
|
|
223
|
+
* @param format_str 格式化字符串,或选项对象(旧 API 兼容)
|
|
224
|
+
* @param options 可选配置项
|
|
225
|
+
* @returns 返回格式化后的结果
|
|
226
|
+
*
|
|
227
|
+
* @example:
|
|
228
|
+
* ```typescript
|
|
229
|
+
* // 新 API(推荐)
|
|
230
|
+
* fmt(100, '=2') // '100.00'
|
|
231
|
+
* fmt(1234567, ',') // '1,234,567'
|
|
232
|
+
* fmt(0.1234, '%=2') // '12.34%'
|
|
233
|
+
*
|
|
234
|
+
* // 旧 API(向后兼容)
|
|
235
|
+
* fmt('100 | =2') // '100.00'
|
|
236
|
+
* fmt('num | ,', { num: 1234567 }) // '1,234,567'
|
|
237
|
+
* fmt(100, { _fmt: '=2' }) // '100.00'
|
|
238
|
+
* ```
|
|
157
239
|
*/
|
|
158
|
-
export const fmt:
|
|
240
|
+
export const fmt: Fmt;
|
|
159
241
|
/**
|
|
160
242
|
* calc方法的包装版本,除了支持calc所有的功能还额外提供了更强大的类型推导和更灵活的编写方式
|
|
161
243
|
* @param expr 一个字符串算术式或一个数字
|
|
@@ -175,89 +257,445 @@ declare const version: string;
|
|
|
175
257
|
|
|
176
258
|
export const parse_thousands: (str: string) => string;
|
|
177
259
|
|
|
260
|
+
// ==================== 原始运算函数类型 ====================
|
|
261
|
+
|
|
262
|
+
type NumOrStr = number | string;
|
|
263
|
+
type TypeArg = "number" | "string";
|
|
264
|
+
|
|
178
265
|
/**
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
* @returns 计算得到的结果
|
|
266
|
+
* 二元运算函数类型(支持多参数)
|
|
267
|
+
* - 默认返回 number
|
|
268
|
+
* - 最后一个参数为 "string" 时返回 string
|
|
269
|
+
* - 最后一个参数为 "number" 时返回 number
|
|
184
270
|
*/
|
|
185
|
-
|
|
271
|
+
interface BinaryOp {
|
|
272
|
+
// 无参数
|
|
273
|
+
(): number;
|
|
274
|
+
// 单参数
|
|
275
|
+
(a: NumOrStr): number;
|
|
276
|
+
(a: NumOrStr, type: "string"): string;
|
|
277
|
+
(a: NumOrStr, type: "number"): number;
|
|
278
|
+
// 双参数(兼容旧版)
|
|
279
|
+
(a: NumOrStr, b: NumOrStr): number;
|
|
280
|
+
(a: NumOrStr, b: NumOrStr, type: "string"): string;
|
|
281
|
+
(a: NumOrStr, b: NumOrStr, type: "number"): number;
|
|
282
|
+
// 三参数
|
|
283
|
+
(a: NumOrStr, b: NumOrStr, c: NumOrStr): number;
|
|
284
|
+
(a: NumOrStr, b: NumOrStr, c: NumOrStr, type: "string"): string;
|
|
285
|
+
(a: NumOrStr, b: NumOrStr, c: NumOrStr, type: "number"): number;
|
|
286
|
+
// 四参数
|
|
287
|
+
(a: NumOrStr, b: NumOrStr, c: NumOrStr, d: NumOrStr): number;
|
|
288
|
+
(a: NumOrStr, b: NumOrStr, c: NumOrStr, d: NumOrStr, type: "string"): string;
|
|
289
|
+
(a: NumOrStr, b: NumOrStr, c: NumOrStr, d: NumOrStr, type: "number"): number;
|
|
290
|
+
// 更多参数(fallback)
|
|
291
|
+
(...args: NumOrStr[]): number | string;
|
|
292
|
+
}
|
|
293
|
+
|
|
186
294
|
/**
|
|
187
|
-
*
|
|
188
|
-
* @param a 一个数字或数字字符串
|
|
189
|
-
* @param b 另一个数字或数字字符串
|
|
190
|
-
* @param type 输出数字还是字符串
|
|
191
|
-
* @returns 计算得到的结果
|
|
295
|
+
* 一元运算函数类型
|
|
192
296
|
*/
|
|
193
|
-
|
|
297
|
+
interface UnaryOp {
|
|
298
|
+
(value: NumOrStr): number;
|
|
299
|
+
(value: NumOrStr, type: "string"): string;
|
|
300
|
+
(value: NumOrStr, type: "number"): number;
|
|
301
|
+
}
|
|
302
|
+
|
|
194
303
|
/**
|
|
195
|
-
*
|
|
196
|
-
* @
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
304
|
+
* 加法 - 支持多参数
|
|
305
|
+
* @example
|
|
306
|
+
* add(1, 2) // => 3 (number)
|
|
307
|
+
* add(1, 2, 3, 4) // => 10 (number)
|
|
308
|
+
* add(1, 2, "string") // => "3" (string)
|
|
200
309
|
*/
|
|
201
|
-
export const
|
|
310
|
+
export const add: BinaryOp;
|
|
311
|
+
|
|
202
312
|
/**
|
|
203
|
-
*
|
|
204
|
-
* @
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
313
|
+
* 减法 - 支持多参数
|
|
314
|
+
* @example
|
|
315
|
+
* sub(10, 3) // => 7 (number)
|
|
316
|
+
* sub(10, 3, 2) // => 5 (number)
|
|
317
|
+
* sub(10, 3, "string") // => "7" (string)
|
|
208
318
|
*/
|
|
209
|
-
export const
|
|
319
|
+
export const sub: BinaryOp;
|
|
320
|
+
|
|
210
321
|
/**
|
|
211
|
-
*
|
|
212
|
-
* @
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
322
|
+
* 乘法 - 支持多参数
|
|
323
|
+
* @example
|
|
324
|
+
* mul(2, 3) // => 6 (number)
|
|
325
|
+
* mul(2, 3, 4) // => 24 (number)
|
|
326
|
+
* mul(2, 3, "string") // => "6" (string)
|
|
216
327
|
*/
|
|
217
|
-
export const
|
|
328
|
+
export const mul: BinaryOp;
|
|
329
|
+
|
|
218
330
|
/**
|
|
219
|
-
*
|
|
220
|
-
* @
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
331
|
+
* 除法 - 支持多参数
|
|
332
|
+
* @example
|
|
333
|
+
* div(100, 2) // => 50 (number)
|
|
334
|
+
* div(100, 2, 5) // => 10 (number)
|
|
335
|
+
* div(10, 0) // throws Error or returns _error
|
|
224
336
|
*/
|
|
225
|
-
export const
|
|
337
|
+
export const div: BinaryOp;
|
|
338
|
+
|
|
226
339
|
/**
|
|
227
|
-
*
|
|
228
|
-
* @
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
* @returns 计算得到的结果
|
|
340
|
+
* 取模 - 支持多参数
|
|
341
|
+
* @example
|
|
342
|
+
* mod(10, 3) // => 1 (number)
|
|
343
|
+
* mod(10, 3, "string") // => "1" (string)
|
|
232
344
|
*/
|
|
233
|
-
export const mod:
|
|
345
|
+
export const mod: BinaryOp;
|
|
234
346
|
|
|
235
347
|
/**
|
|
236
|
-
*
|
|
237
|
-
* @
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
* @param err_value 计算错误时返回的结果,模式是 -
|
|
241
|
-
* @returns string | number | Err
|
|
348
|
+
* 幂运算 - 支持多参数(右结合)
|
|
349
|
+
* @example
|
|
350
|
+
* pow(2, 3) // => 8 (number)
|
|
351
|
+
* pow(2, 3, 2) // => 512 (number) = 2^(3^2)
|
|
242
352
|
*/
|
|
243
|
-
export const
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
353
|
+
export const pow: BinaryOp;
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* 整除 - 支持多参数
|
|
357
|
+
* @example
|
|
358
|
+
* idiv(10, 3) // => 3 (number)
|
|
359
|
+
* idiv(100, 7, 2) // => 7 (number)
|
|
360
|
+
*/
|
|
361
|
+
export const idiv: BinaryOp;
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* 绝对值
|
|
365
|
+
* @example
|
|
366
|
+
* abs(-5) // => 5 (number)
|
|
367
|
+
* abs(-5, "string") // => "5" (string)
|
|
368
|
+
*/
|
|
369
|
+
export const abs: UnaryOp;
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* 取负
|
|
373
|
+
* @example
|
|
374
|
+
* neg(5) // => -5 (number)
|
|
375
|
+
* neg(-5, "string") // => "5" (string)
|
|
376
|
+
*/
|
|
377
|
+
export const neg: UnaryOp;
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* 平方根
|
|
381
|
+
* @example
|
|
382
|
+
* sqrt(16) // => 4 (number)
|
|
383
|
+
* sqrt(2, "string") // => "1.4142135623730950488" (string)
|
|
384
|
+
*/
|
|
385
|
+
export const sqrt: UnaryOp;
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* 自然对数
|
|
389
|
+
* @example
|
|
390
|
+
* ln(Math.E) // => 1 (number)
|
|
391
|
+
* ln(10, "string") // => "2.302585..." (string)
|
|
392
|
+
*/
|
|
393
|
+
export const ln: UnaryOp;
|
|
394
|
+
|
|
256
395
|
/**
|
|
257
|
-
*
|
|
258
|
-
* @
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
* @returns 计算结果
|
|
396
|
+
* e 的 x 次方
|
|
397
|
+
* @example
|
|
398
|
+
* exp(1) // => 2.718281828... (number)
|
|
399
|
+
* exp(0, "string") // => "1" (string)
|
|
262
400
|
*/
|
|
263
|
-
export const
|
|
401
|
+
export const exp: UnaryOp;
|
|
402
|
+
|
|
403
|
+
// ==================== 计算模式 ====================
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* 计算模式类型
|
|
407
|
+
*/
|
|
408
|
+
export type ComputeMode = 'decimal' | 'bigint' | 'wasm';
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* 角度单位类型
|
|
412
|
+
*/
|
|
413
|
+
export type AngleUnit = 'deg' | 'rad';
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* 单位转换规则类型
|
|
417
|
+
*/
|
|
418
|
+
export type UnitConvertRule =
|
|
419
|
+
| number
|
|
420
|
+
| ((value: number, input_unit: string, output_unit: string) => number)
|
|
421
|
+
| {
|
|
422
|
+
mul?: number;
|
|
423
|
+
div?: number;
|
|
424
|
+
plus?: number;
|
|
425
|
+
minus?: number;
|
|
426
|
+
fn?: (value: number, input_unit: string, output_unit: string) => number;
|
|
427
|
+
_position?: 'before' | 'after' | 'middle';
|
|
428
|
+
_thousands?: string;
|
|
429
|
+
_vol?: string;
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* 单位转换映射类型(输出单位为key)
|
|
434
|
+
*/
|
|
435
|
+
export type UnitConvertOut = Record<string, Record<string, UnitConvertRule> & {
|
|
436
|
+
_position?: 'before' | 'after' | 'middle';
|
|
437
|
+
_thousands?: string;
|
|
438
|
+
_vol?: string;
|
|
439
|
+
}>;
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* 单位转换映射类型(输入单位为key)
|
|
443
|
+
*/
|
|
444
|
+
export type UnitConvertIn = Record<string, Record<string, UnitConvertRule>>;
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* 千分位预设配置类型
|
|
448
|
+
*/
|
|
449
|
+
export interface ThousandsPreset {
|
|
450
|
+
/** 千分位分隔符,默认 ',' */
|
|
451
|
+
sep?: string;
|
|
452
|
+
/** 小数点符号,默认 '.' */
|
|
453
|
+
point?: string;
|
|
454
|
+
/** 分组规则,默认 [3] */
|
|
455
|
+
grouping?: number[];
|
|
456
|
+
/** 最小触发位数,0 表示不限制 */
|
|
457
|
+
min_len?: number;
|
|
458
|
+
/** 是否对小数部分分组 */
|
|
459
|
+
point_group?: boolean;
|
|
460
|
+
/** 自定义格式化函数 */
|
|
461
|
+
fn?: (num_str: string, context: { int_part: string; dec_part: string | null; sign: string; options: any }) => string;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* 交易量预设配置类型
|
|
466
|
+
*/
|
|
467
|
+
export type VolumePreset =
|
|
468
|
+
| string[] // 简写形式:只有单位数组,step 默认 1000
|
|
469
|
+
| { // 完整形式:包含 units 和 step
|
|
470
|
+
units: string[];
|
|
471
|
+
step?: number | number[];
|
|
472
|
+
}
|
|
473
|
+
| ((num_str: string, context: { // 自定义函数形式
|
|
474
|
+
decimals: number;
|
|
475
|
+
options: any;
|
|
476
|
+
current_unit?: string;
|
|
477
|
+
}) => {
|
|
478
|
+
sign: string;
|
|
479
|
+
number: string;
|
|
480
|
+
unit: string;
|
|
481
|
+
formatted: string;
|
|
482
|
+
} | string); // 可以返回对象或字符串
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* 全局配置项
|
|
486
|
+
*/
|
|
487
|
+
export interface CalcConfig {
|
|
488
|
+
/** 计算模式: "decimal" | "bigint" | "wasm" */
|
|
489
|
+
_compute_mode?: ComputeMode;
|
|
490
|
+
/** 角度单位: "deg" (度) | "rad" (弧度),默认 "deg" */
|
|
491
|
+
_angle_unit?: AngleUnit;
|
|
492
|
+
/** 除法精度(小数位数),默认 20 */
|
|
493
|
+
_div_precision?: number;
|
|
494
|
+
/** 错误时的返回值 */
|
|
495
|
+
_error?: any;
|
|
496
|
+
/** 是否启用调试模式控制台输出,默认 true */
|
|
497
|
+
_debug_console?: boolean;
|
|
498
|
+
/** 自定义函数映射 */
|
|
499
|
+
_custom_functions?: Record<string, (...args: any[]) => any>;
|
|
500
|
+
|
|
501
|
+
// ==================== 交易量配置 ====================
|
|
502
|
+
/** 交易量预设配置(支持对象、数组简写、自定义函数) */
|
|
503
|
+
_vol?: Record<string, VolumePreset>;
|
|
504
|
+
/** 默认交易量预设名称 */
|
|
505
|
+
_vol_default?: string;
|
|
506
|
+
|
|
507
|
+
// ==================== 单位转换配置 ====================
|
|
508
|
+
/** 单位转换映射(输出单位为key) */
|
|
509
|
+
_unit_convert_out?: UnitConvertOut;
|
|
510
|
+
/** 单位转换映射(输入单位为key,内部会转换为 _unit_convert_out 格式) */
|
|
511
|
+
_unit_convert_in?: UnitConvertIn;
|
|
512
|
+
/** 默认输出单位 */
|
|
513
|
+
_unit_default_out?: string | string[];
|
|
514
|
+
/** 默认输入单位(当有多个输入单位时指定默认从哪个单位转换) */
|
|
515
|
+
_unit_default_in?: string | string[];
|
|
516
|
+
/** 默认单位位置: 'after' | 'before' | 'middle' */
|
|
517
|
+
_unit_default_position?: 'before' | 'after' | 'middle';
|
|
518
|
+
|
|
519
|
+
// ==================== 单位联动映射 ====================
|
|
520
|
+
/** 单位到位置的映射 */
|
|
521
|
+
_unit_position_map?: Record<string, 'before' | 'after' | 'middle'>;
|
|
522
|
+
/** 单位到千分位预设的映射 */
|
|
523
|
+
_unit_thousands_map?: Record<string, string>;
|
|
524
|
+
/** 单位到交易量预设的映射 */
|
|
525
|
+
_unit_vol_map?: Record<string, string>;
|
|
526
|
+
|
|
527
|
+
// ==================== 千分位配置 ====================
|
|
528
|
+
/** 千分位预设配置 */
|
|
529
|
+
_thousands?: Record<string, ThousandsPreset>;
|
|
530
|
+
|
|
531
|
+
/** 其他自定义配置 */
|
|
532
|
+
[key: string]: any;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* 设置全局配置
|
|
537
|
+
* @param config 配置对象
|
|
538
|
+
* @example
|
|
539
|
+
* set_config({ _compute_mode: 'bigint' }) // 切换到 BigInt 模式
|
|
540
|
+
* set_config({ _angle_unit: 'rad' }) // 使用弧度制
|
|
541
|
+
* set_config({ _div_precision: 30 }) // 设置除法精度为 30 位
|
|
542
|
+
* set_config({ _error: 0 }) // 错误时返回 0
|
|
543
|
+
*/
|
|
544
|
+
export function set_config(config: CalcConfig): void;
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* 获取全局配置
|
|
548
|
+
* @param key 配置项名称,不传则返回全部配置
|
|
549
|
+
* @returns 配置值或全部配置对象
|
|
550
|
+
* @example
|
|
551
|
+
* get_config('_compute_mode') // => 'decimal'
|
|
552
|
+
* get_config() // => { _compute_mode: 'decimal', ... }
|
|
553
|
+
*/
|
|
554
|
+
export function get_config(): CalcConfig;
|
|
555
|
+
export function get_config<K extends keyof CalcConfig>(key: K): CalcConfig[K];
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* 重置全局配置到默认值
|
|
559
|
+
*
|
|
560
|
+
* @example
|
|
561
|
+
* reset_config() // 重置所有配置
|
|
562
|
+
* reset_config('_unit_convert_out') // 重置单个配置
|
|
563
|
+
* reset_config(['_unit_convert_out', '_unit_default_out']) // 重置多个配置
|
|
564
|
+
*/
|
|
565
|
+
export function reset_config(): void;
|
|
566
|
+
export function reset_config(key: keyof CalcConfig): void;
|
|
567
|
+
export function reset_config(keys: (keyof CalcConfig)[]): void;
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* 运算符方法类型(内部使用,通过 r* 函数访问)
|
|
571
|
+
*/
|
|
572
|
+
export interface Ops {
|
|
573
|
+
// 基础运算
|
|
574
|
+
add: (a: string, b: string) => string;
|
|
575
|
+
sub: (a: string, b: string) => string;
|
|
576
|
+
mul: (a: string, b: string) => string;
|
|
577
|
+
div: (a: string, b: string) => string;
|
|
578
|
+
mod: (a: string, b: string) => string;
|
|
579
|
+
pow: (a: string, b: string) => string;
|
|
580
|
+
idiv: (a: string, b: string) => string;
|
|
581
|
+
// 一元运算
|
|
582
|
+
abs: (a: string) => string;
|
|
583
|
+
neg: (a: string) => string;
|
|
584
|
+
sqrt: (a: string) => string;
|
|
585
|
+
cbrt: (a: string) => string;
|
|
586
|
+
// 指数对数
|
|
587
|
+
exp: (a: string) => string;
|
|
588
|
+
ln: (a: string) => string;
|
|
589
|
+
log: (a: string, base: string) => string;
|
|
590
|
+
log2: (a: string) => string;
|
|
591
|
+
log10: (a: string) => string;
|
|
592
|
+
// 三角函数
|
|
593
|
+
sin: (a: string) => string;
|
|
594
|
+
cos: (a: string) => string;
|
|
595
|
+
tan: (a: string) => string;
|
|
596
|
+
asin: (a: string) => string;
|
|
597
|
+
acos: (a: string) => string;
|
|
598
|
+
atan: (a: string) => string;
|
|
599
|
+
// 双曲函数
|
|
600
|
+
sinh: (a: string) => string;
|
|
601
|
+
cosh: (a: string) => string;
|
|
602
|
+
tanh: (a: string) => string;
|
|
603
|
+
asinh: (a: string) => string;
|
|
604
|
+
acosh: (a: string) => string;
|
|
605
|
+
atanh: (a: string) => string;
|
|
606
|
+
// 取整
|
|
607
|
+
floor: (a: string) => string;
|
|
608
|
+
ceil: (a: string) => string;
|
|
609
|
+
trunc: (a: string) => string;
|
|
610
|
+
round: (a: string) => string;
|
|
611
|
+
// 比较
|
|
612
|
+
max: (a: string, b: string) => string;
|
|
613
|
+
min: (a: string, b: string) => string;
|
|
614
|
+
compare: (a: string, b: string) => number;
|
|
615
|
+
eq: (a: string, b: string) => boolean;
|
|
616
|
+
lt: (a: string, b: string) => boolean;
|
|
617
|
+
gt: (a: string, b: string) => boolean;
|
|
618
|
+
lte: (a: string, b: string) => boolean;
|
|
619
|
+
gte: (a: string, b: string) => boolean;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* 加载 WASM 模块(异步)
|
|
624
|
+
* @returns Promise,加载完成后 resolve
|
|
625
|
+
*/
|
|
626
|
+
export function load_wasm(): Promise<any>;
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* 检查 WASM 模块是否已加载
|
|
630
|
+
* @returns 是否已加载
|
|
631
|
+
*/
|
|
632
|
+
export function is_wasm_loaded(): boolean;
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* WASM 表达式计算
|
|
636
|
+
* 直接传入复杂表达式字符串,内部完成解析和计算
|
|
637
|
+
* @param expr 表达式字符串
|
|
638
|
+
* @returns 计算结果字符串
|
|
639
|
+
* @example
|
|
640
|
+
* wcalc("1 + 2 * 3") // => "7"
|
|
641
|
+
* wcalc("sqrt(16) + pow(2, 3)") // => "12"
|
|
642
|
+
*/
|
|
643
|
+
export function wcalc(expr: string): string;
|
|
644
|
+
|
|
645
|
+
// ==================== r 前缀原始运算函数 ====================
|
|
646
|
+
// 这些函数直接调用底层计算模式,字符串进字符串出,会跟随 _compute_mode 配置变化
|
|
647
|
+
// 切换计算模式请使用: set_config({ _compute_mode: 'bigint' })
|
|
648
|
+
|
|
649
|
+
// 基础运算
|
|
650
|
+
export function radd(a: string, b: string): string;
|
|
651
|
+
export function rsub(a: string, b: string): string;
|
|
652
|
+
export function rmul(a: string, b: string): string;
|
|
653
|
+
export function rdiv(a: string, b: string): string;
|
|
654
|
+
export function rmod(a: string, b: string): string;
|
|
655
|
+
export function rpow(a: string, b: string): string;
|
|
656
|
+
export function ridiv(a: string, b: string): string;
|
|
657
|
+
|
|
658
|
+
// 一元运算
|
|
659
|
+
export function rabs(a: string): string;
|
|
660
|
+
export function rneg(a: string): string;
|
|
661
|
+
export function rsqrt(a: string): string;
|
|
662
|
+
export function rcbrt(a: string): string;
|
|
663
|
+
|
|
664
|
+
// 指数对数
|
|
665
|
+
export function rexp(a: string): string;
|
|
666
|
+
export function rln(a: string): string;
|
|
667
|
+
export function rlog(a: string, base: string): string;
|
|
668
|
+
export function rlog2(a: string): string;
|
|
669
|
+
export function rlog10(a: string): string;
|
|
670
|
+
|
|
671
|
+
// 三角函数
|
|
672
|
+
export function rsin(a: string): string;
|
|
673
|
+
export function rcos(a: string): string;
|
|
674
|
+
export function rtan(a: string): string;
|
|
675
|
+
export function rasin(a: string): string;
|
|
676
|
+
export function racos(a: string): string;
|
|
677
|
+
export function ratan(a: string): string;
|
|
678
|
+
|
|
679
|
+
// 双曲函数
|
|
680
|
+
export function rsinh(a: string): string;
|
|
681
|
+
export function rcosh(a: string): string;
|
|
682
|
+
export function rtanh(a: string): string;
|
|
683
|
+
export function rasinh(a: string): string;
|
|
684
|
+
export function racosh(a: string): string;
|
|
685
|
+
export function ratanh(a: string): string;
|
|
686
|
+
|
|
687
|
+
// 取整
|
|
688
|
+
export function rfloor(a: string): string;
|
|
689
|
+
export function rceil(a: string): string;
|
|
690
|
+
export function rtrunc(a: string): string;
|
|
691
|
+
export function rround(a: string): string;
|
|
692
|
+
|
|
693
|
+
// 比较
|
|
694
|
+
export function rmax(a: string, b: string): string;
|
|
695
|
+
export function rmin(a: string, b: string): string;
|
|
696
|
+
export function rcompare(a: string, b: string): number;
|
|
697
|
+
export function req(a: string, b: string): boolean;
|
|
698
|
+
export function rlt(a: string, b: string): boolean;
|
|
699
|
+
export function rgt(a: string, b: string): boolean;
|
|
700
|
+
export function rlte(a: string, b: string): boolean;
|
|
701
|
+
export function rgte(a: string, b: string): boolean;
|