a-calc 3.0.0-beta.20260124.1 → 3.0.0-beta.20260124.3
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/a-calc.versions.js +4 -3
- package/browser/index.js +2 -2
- package/calc.d.ts +89 -45
- package/cjs/index.js +2 -2
- package/es/index.js +2 -2
- package/package.json +1 -1
package/calc.d.ts
CHANGED
|
@@ -67,18 +67,29 @@ type If_DirectReturn<Arg1, Arg2> =
|
|
|
67
67
|
If_NumOrStr<Arg1> & Not<If_IncludesVar<Arg1>>
|
|
68
68
|
>;
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
/**
|
|
71
|
+
* calc/calc_space 运行时选项类型(可以包含数据和配置)
|
|
72
|
+
*/
|
|
73
|
+
type CalcRuntimeOptions<Fmt extends string, Err> =
|
|
71
74
|
any[] | Partial<{
|
|
72
|
-
_error: Err
|
|
73
|
-
|
|
74
|
-
_unit: boolean
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
_error: Err;
|
|
76
|
+
_fmt: Fmt;
|
|
77
|
+
_unit: boolean;
|
|
78
|
+
_debug?: boolean;
|
|
79
|
+
_on_debug?: (info: any) => void;
|
|
80
|
+
_angle_unit?: 'deg' | 'rad';
|
|
81
|
+
_compact_units?: string[];
|
|
82
|
+
_compact_step?: number | number[];
|
|
83
|
+
_thousands?: Record<string, ThousandsPreset>;
|
|
84
|
+
/** 多路取值时视为空值的值数组(仅运行时选项) */
|
|
85
|
+
_empty_values?: any[];
|
|
86
|
+
/** 多路取值时自定义空值判断函数(仅运行时选项) */
|
|
87
|
+
_empty_check?: (value: any, path?: string) => boolean;
|
|
88
|
+
[key: string]: any; // 允许任意数据字段
|
|
78
89
|
}>;
|
|
79
90
|
|
|
80
91
|
export type Calc = <const Expr extends string | number, const Fmt extends string, const Err = never>
|
|
81
|
-
(expr: Expr, options?:
|
|
92
|
+
(expr: Expr, options?: CalcRuntimeOptions<Fmt, Err>) =>
|
|
82
93
|
(
|
|
83
94
|
If_StrIncludes<Fmt, ["!n"]> & If_StrIncludes<Expr, ["!n"]> extends true ?
|
|
84
95
|
number | Err
|
|
@@ -87,7 +98,7 @@ export type Calc = <const Expr extends string | number, const Fmt extends string
|
|
|
87
98
|
);
|
|
88
99
|
|
|
89
100
|
export type CalcSpace = <const Expr extends string | number, const Fmt extends string, const Err = never>
|
|
90
|
-
(expr: Expr, options?:
|
|
101
|
+
(expr: Expr, options?: CalcRuntimeOptions<Fmt, Err>) =>
|
|
91
102
|
(
|
|
92
103
|
If_StrIncludes<Fmt, ["!n"]> & If_StrIncludes<Expr, ["!n"]> extends true ?
|
|
93
104
|
number | Err
|
|
@@ -96,7 +107,7 @@ export type CalcSpace = <const Expr extends string | number, const Fmt extends s
|
|
|
96
107
|
);
|
|
97
108
|
|
|
98
109
|
export type CalcSum = <const Expr extends string | number, const Fmt extends string, const Err = never>
|
|
99
|
-
(expr: Expr, options: Array<
|
|
110
|
+
(expr: Expr, options: Array<CalcRuntimeOptions<Fmt, Err>>) =>
|
|
100
111
|
(
|
|
101
112
|
If_StrIncludes<Fmt, ["!n"]> & If_StrIncludes<Expr, ["!n"]> extends true ?
|
|
102
113
|
number | Err
|
|
@@ -105,26 +116,30 @@ export type CalcSum = <const Expr extends string | number, const Fmt extends str
|
|
|
105
116
|
);
|
|
106
117
|
|
|
107
118
|
/**
|
|
108
|
-
* fmt
|
|
119
|
+
* fmt 格式化选项(运行时选项,可以包含数据)
|
|
109
120
|
*/
|
|
110
121
|
export interface FmtOptions {
|
|
111
122
|
_error?: any;
|
|
112
123
|
_fmt?: string;
|
|
113
|
-
/**
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
|
|
124
|
+
/** 紧凑格式单位数组(仅选项,临时覆盖) */
|
|
125
|
+
_compact_units?: string[];
|
|
126
|
+
/** 紧凑格式阶梯值(仅选项,临时覆盖) */
|
|
127
|
+
_compact_step?: number | number[];
|
|
128
|
+
/** 紧凑格式预设配置(仅选项) */
|
|
129
|
+
_compact?: Record<string, CompactPreset>;
|
|
119
130
|
_unit_convert_out?: Record<string, any>;
|
|
120
131
|
_unit_convert_in?: Record<string, any>;
|
|
121
132
|
_unit_default_out?: string | string[];
|
|
122
133
|
_unit_default_in?: string | string[];
|
|
123
134
|
_unit_default_position?: 'before' | 'after' | 'middle';
|
|
124
|
-
|
|
125
|
-
_thousands?: Record<string,
|
|
126
|
-
|
|
127
|
-
|
|
135
|
+
_unit_position_map?: Record<string, 'before' | 'after' | 'middle'>;
|
|
136
|
+
_thousands?: Record<string, ThousandsPreset>;
|
|
137
|
+
_unit_thousands_map?: Record<string, string>;
|
|
138
|
+
_unit_compact_map?: Record<string, string>;
|
|
139
|
+
/** 多路取值时视为空值的值数组(仅运行时选项) */
|
|
140
|
+
_empty_values?: any[];
|
|
141
|
+
/** 多路取值时自定义空值判断函数(仅运行时选项) */
|
|
142
|
+
_empty_check?: (value: any, path?: string) => boolean;
|
|
128
143
|
[key: string]: any;
|
|
129
144
|
}
|
|
130
145
|
|
|
@@ -144,10 +159,10 @@ export interface CalcWrap {
|
|
|
144
159
|
<const Expr extends string | number>
|
|
145
160
|
(expr: Expr): If_IncludesVar<Expr> extends true ? FuncReturn<Expr> : If_StrIncludes<Expr, ["!n", "!N"]> extends true ? number | "-" : string | "-";
|
|
146
161
|
|
|
147
|
-
<const Expr extends string | number, const Fmt extends string, const Err, const Options extends
|
|
162
|
+
<const Expr extends string | number, const Fmt extends string, const Err, const Options extends CalcRuntimeOptions<Fmt, Err>>
|
|
148
163
|
(expr: Expr, options: Options): DirectReturn<Expr, Options>;
|
|
149
164
|
|
|
150
|
-
<const Fmt extends string, const Err, const Options extends
|
|
165
|
+
<const Fmt extends string, const Err, const Options extends CalcRuntimeOptions<Fmt, Err>>
|
|
151
166
|
(options: Options): FuncReturn<Options>;
|
|
152
167
|
}
|
|
153
168
|
|
|
@@ -443,6 +458,18 @@ export type UnitConvertOut = Record<string, Record<string, UnitConvertRule> & {
|
|
|
443
458
|
*/
|
|
444
459
|
export type UnitConvertIn = Record<string, Record<string, UnitConvertRule>>;
|
|
445
460
|
|
|
461
|
+
/**
|
|
462
|
+
* 千分位预设名称(系统内置)
|
|
463
|
+
* - us: 美式/英式/国际标准 (1,234,567.89)
|
|
464
|
+
* - eu: 欧洲大陆格式 (1.234.567,89)
|
|
465
|
+
* - swiss: 瑞士格式 (1'234'567.89)
|
|
466
|
+
* - space: 空格分隔/ISO标准 (1 234 567.89)
|
|
467
|
+
* - fr: 法国格式 (1 234 567,89 - 使用不换行空格)
|
|
468
|
+
* - indian: 印度格式 (12,34,567.89)
|
|
469
|
+
* - wan: 万进制/东亚格式 (123,4567.89)
|
|
470
|
+
*/
|
|
471
|
+
export type ThousandsPresetName = 'us' | 'eu' | 'swiss' | 'space' | 'fr' | 'indian' | 'wan';
|
|
472
|
+
|
|
446
473
|
/**
|
|
447
474
|
* 千分位预设配置类型
|
|
448
475
|
*/
|
|
@@ -462,9 +489,9 @@ export interface ThousandsPreset {
|
|
|
462
489
|
}
|
|
463
490
|
|
|
464
491
|
/**
|
|
465
|
-
*
|
|
492
|
+
* 紧凑格式预设配置类型
|
|
466
493
|
*/
|
|
467
|
-
export type
|
|
494
|
+
export type CompactPreset =
|
|
468
495
|
| string[] // 简写形式:只有单位数组,step 默认 1000
|
|
469
496
|
| { // 完整形式:包含 units 和 step
|
|
470
497
|
units: string[];
|
|
@@ -482,27 +509,35 @@ export type VolumePreset =
|
|
|
482
509
|
} | string); // 可以返回对象或字符串
|
|
483
510
|
|
|
484
511
|
/**
|
|
485
|
-
*
|
|
512
|
+
* 全局配置项(只能通过 set_config 设置)
|
|
486
513
|
*/
|
|
487
|
-
export interface
|
|
514
|
+
export interface GlobalConfig {
|
|
488
515
|
/** 计算模式: "decimal" | "bigint" | "wasm" */
|
|
489
516
|
_compute_mode?: ComputeMode;
|
|
490
517
|
/** 角度单位: "deg" (度) | "rad" (弧度),默认 "deg" */
|
|
491
518
|
_angle_unit?: AngleUnit;
|
|
492
519
|
/** 除法精度(小数位数),默认 20 */
|
|
493
520
|
_div_precision?: number;
|
|
494
|
-
/**
|
|
521
|
+
/** 错误时的返回值,默认 '-' */
|
|
495
522
|
_error?: any;
|
|
496
523
|
/** 是否启用调试模式控制台输出,默认 true */
|
|
497
524
|
_debug_console?: boolean;
|
|
498
|
-
/**
|
|
499
|
-
|
|
525
|
+
/** 默认舍入模式 */
|
|
526
|
+
_default_round?: string;
|
|
527
|
+
/** 默认格式化字符串(全局配置) */
|
|
528
|
+
_fmt?: string;
|
|
529
|
+
/** 内置函数映射 */
|
|
530
|
+
_builtin_functions?: Record<string, (...args: any[]) => any>;
|
|
500
531
|
|
|
501
|
-
// ====================
|
|
502
|
-
/**
|
|
503
|
-
|
|
504
|
-
/**
|
|
505
|
-
|
|
532
|
+
// ==================== 紧凑格式配置 ====================
|
|
533
|
+
/** 紧凑格式预设配置(全局配置) */
|
|
534
|
+
_compact?: Record<string, CompactPreset>;
|
|
535
|
+
/** 默认紧凑格式预设名称 */
|
|
536
|
+
_compact_default?: string;
|
|
537
|
+
|
|
538
|
+
// ==================== 格式化分组配置 ====================
|
|
539
|
+
/** 格式化分组(全局配置) */
|
|
540
|
+
_fmt_groups?: Record<string, string>;
|
|
506
541
|
|
|
507
542
|
// ==================== 单位转换配置 ====================
|
|
508
543
|
/** 单位转换映射(输出单位为key) */
|
|
@@ -521,15 +556,24 @@ export interface CalcConfig {
|
|
|
521
556
|
_unit_position_map?: Record<string, 'before' | 'after' | 'middle'>;
|
|
522
557
|
/** 单位到千分位预设的映射 */
|
|
523
558
|
_unit_thousands_map?: Record<string, string>;
|
|
524
|
-
/**
|
|
525
|
-
|
|
559
|
+
/** 单位到紧凑格式预设的映射 */
|
|
560
|
+
_unit_compact_map?: Record<string, string>;
|
|
526
561
|
|
|
527
562
|
// ==================== 千分位配置 ====================
|
|
528
563
|
/** 千分位预设配置 */
|
|
529
564
|
_thousands?: Record<string, ThousandsPreset>;
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
565
|
+
/** 默认千分位预设名称 */
|
|
566
|
+
_thousands_default?: string;
|
|
567
|
+
|
|
568
|
+
// ==================== 快捷语法配置 ====================
|
|
569
|
+
/** 快捷语法前缀(:xxx 等同于 _shortcut_prefix:xxx),默认 '!u' */
|
|
570
|
+
_shortcut_prefix?: string;
|
|
571
|
+
|
|
572
|
+
// ==================== 多路取值配置 ====================
|
|
573
|
+
/** 多路取值时视为空值的值数组(全局配置,运行时选项可覆盖) */
|
|
574
|
+
_empty_values?: any[];
|
|
575
|
+
/** 多路取值时自定义空值判断函数(全局配置,运行时选项可覆盖) */
|
|
576
|
+
_empty_check?: (value: any, path?: string) => boolean;
|
|
533
577
|
}
|
|
534
578
|
|
|
535
579
|
/**
|
|
@@ -541,7 +585,7 @@ export interface CalcConfig {
|
|
|
541
585
|
* set_config({ _div_precision: 30 }) // 设置除法精度为 30 位
|
|
542
586
|
* set_config({ _error: 0 }) // 错误时返回 0
|
|
543
587
|
*/
|
|
544
|
-
export function set_config(config:
|
|
588
|
+
export function set_config(config: GlobalConfig): void;
|
|
545
589
|
|
|
546
590
|
/**
|
|
547
591
|
* 获取全局配置
|
|
@@ -551,8 +595,8 @@ export function set_config(config: CalcConfig): void;
|
|
|
551
595
|
* get_config('_compute_mode') // => 'decimal'
|
|
552
596
|
* get_config() // => { _compute_mode: 'decimal', ... }
|
|
553
597
|
*/
|
|
554
|
-
export function get_config():
|
|
555
|
-
export function get_config<K extends keyof
|
|
598
|
+
export function get_config(): GlobalConfig;
|
|
599
|
+
export function get_config<K extends keyof GlobalConfig>(key: K): GlobalConfig[K];
|
|
556
600
|
|
|
557
601
|
/**
|
|
558
602
|
* 重置全局配置到默认值
|
|
@@ -563,8 +607,8 @@ export function get_config<K extends keyof CalcConfig>(key: K): CalcConfig[K];
|
|
|
563
607
|
* reset_config(['_unit_convert_out', '_unit_default_out']) // 重置多个配置
|
|
564
608
|
*/
|
|
565
609
|
export function reset_config(): void;
|
|
566
|
-
export function reset_config(key: keyof
|
|
567
|
-
export function reset_config(keys: (keyof
|
|
610
|
+
export function reset_config(key: keyof GlobalConfig): void;
|
|
611
|
+
export function reset_config(keys: (keyof GlobalConfig)[]): void;
|
|
568
612
|
|
|
569
613
|
/**
|
|
570
614
|
* 运算符方法类型(内部使用,通过 r* 函数访问)
|