a-calc 3.0.0-beta.20260105 → 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 -379
- package/a-calc.versions.js +3 -3
- package/browser/index.js +2 -2
- package/calc.d.ts +163 -22
- package/cjs/index.js +2 -2
- package/es/index.js +2 -2
- package/package.json +5 -4
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
|
|
@@ -102,17 +110,21 @@ export type CalcSum = <const Expr extends string | number, const Fmt extends str
|
|
|
102
110
|
export interface FmtOptions {
|
|
103
111
|
_error?: any;
|
|
104
112
|
_fmt?: string;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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>;
|
|
115
126
|
_unit_thousands?: Record<string, string>;
|
|
127
|
+
_unit_vol?: Record<string, string>;
|
|
116
128
|
[key: string]: any;
|
|
117
129
|
}
|
|
118
130
|
|
|
@@ -128,16 +140,15 @@ export interface Fmt {
|
|
|
128
140
|
): If_StrIncludes<FmtStr, ["!n"]> extends true ? number | Err : string | Err;
|
|
129
141
|
}
|
|
130
142
|
|
|
131
|
-
export interface CalcWrap
|
|
132
|
-
{
|
|
143
|
+
export interface CalcWrap {
|
|
133
144
|
<const Expr extends string | number>
|
|
134
|
-
(
|
|
145
|
+
(expr: Expr): If_IncludesVar<Expr> extends true ? FuncReturn<Expr> : If_StrIncludes<Expr, ["!n", "!N"]> extends true ? number | "-" : string | "-";
|
|
135
146
|
|
|
136
147
|
<const Expr extends string | number, const Fmt extends string, const Err, const Options extends CalcConfig<Fmt, Err>>
|
|
137
|
-
(
|
|
148
|
+
(expr: Expr, options: Options): DirectReturn<Expr, Options>;
|
|
138
149
|
|
|
139
150
|
<const Fmt extends string, const Err, const Options extends CalcConfig<Fmt, Err>>
|
|
140
|
-
(
|
|
151
|
+
(options: Options): FuncReturn<Options>;
|
|
141
152
|
}
|
|
142
153
|
|
|
143
154
|
declare const calc_util: {
|
|
@@ -152,7 +163,7 @@ declare const calc_util: {
|
|
|
152
163
|
};
|
|
153
164
|
|
|
154
165
|
/**
|
|
155
|
-
*
|
|
166
|
+
* 传入字符串算术式或数字返回计算的结果(标准语法)
|
|
156
167
|
* @param expr 一个字符串算术式或一个数字
|
|
157
168
|
* @param options 一个配置或者用来填充算术式的数据源,或者两者的结合
|
|
158
169
|
* @returns 返回进过计算和格式化之后的结果,结果可能是字符串或数字或自定义的错误类型
|
|
@@ -169,6 +180,24 @@ declare const calc_util: {
|
|
|
169
180
|
*/
|
|
170
181
|
export const calc: Calc;
|
|
171
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
|
+
|
|
172
201
|
/**
|
|
173
202
|
* 传入字符串算术式和数据源的数组进行计算并最终归集,这个方法类似 options_arr.reduce((memo, data) => calc("expr + memo", {expr, memo, _fill_data: data}), 0)
|
|
174
203
|
* @param expr 一个字符串算术式或一个数字
|
|
@@ -383,6 +412,75 @@ export type ComputeMode = 'decimal' | 'bigint' | 'wasm';
|
|
|
383
412
|
*/
|
|
384
413
|
export type AngleUnit = 'deg' | 'rad';
|
|
385
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
|
+
|
|
386
484
|
/**
|
|
387
485
|
* 全局配置项
|
|
388
486
|
*/
|
|
@@ -399,6 +497,37 @@ export interface CalcConfig {
|
|
|
399
497
|
_debug_console?: boolean;
|
|
400
498
|
/** 自定义函数映射 */
|
|
401
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
|
+
|
|
402
531
|
/** 其他自定义配置 */
|
|
403
532
|
[key: string]: any;
|
|
404
533
|
}
|
|
@@ -425,6 +554,18 @@ export function set_config(config: CalcConfig): void;
|
|
|
425
554
|
export function get_config(): CalcConfig;
|
|
426
555
|
export function get_config<K extends keyof CalcConfig>(key: K): CalcConfig[K];
|
|
427
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
|
+
|
|
428
569
|
/**
|
|
429
570
|
* 运算符方法类型(内部使用,通过 r* 函数访问)
|
|
430
571
|
*/
|