a-calc 3.0.1 → 3.0.2

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.
Binary file
package/calc.d.ts CHANGED
@@ -1,23 +1,13 @@
1
1
  import { U, StrRemoveSome, If_StrIncludes, Or, Equal } from "typescript-treasure";
2
2
 
3
- export type InjectDataFn<Expr> = <const Data>
4
- (fill_data: Data extends string | number | undefined | null ? never : Data) =>
5
- (
6
- If_HasNumFmt<Expr, Data> extends true ?
7
- number | GetWrapErr<Data>
8
- :
9
- string | GetWrapErr<Data>
10
- );
3
+ export type InjectDataFn<Expr> = <const Data>
4
+ (fill_data: Data extends string | number | undefined | null ? never : Data) =>
5
+ DirectReturn<Expr, Data>;
11
6
 
12
7
 
13
- export type InjectExprFn<Data> = <const Expr>
14
- (expr: Expr extends string | number ? Expr : never) =>
15
- (
16
- If_HasNumFmt<Expr, Data> extends true ?
17
- number | GetWrapErr<Data>
18
- :
19
- string | GetWrapErr<Data>
20
- );
8
+ export type InjectExprFn<Data> = <const Expr>
9
+ (expr: Expr extends string | number ? Expr : never) =>
10
+ DirectReturn<Expr, Data>;
21
11
 
22
12
 
23
13
  type If_IncludesVar<T> = U.If_IncludeVar<StrRemoveSome<T, ["!n", "!e", "!u", "!N", "!E", "!U"]>>;
@@ -28,23 +18,49 @@ type If_FillData<T> = T extends string | number | undefined | null ? false : tru
28
18
  type Not<T> = T extends true ? false : true;
29
19
 
30
20
  type GetWrapErr<T> = T extends { _error: infer I; } ? I : "-";
31
- type GetFmt<T> = T extends { _fmt: infer I; } ? I : never;
32
-
33
- type If_HasNumFmt<Expr, Data> =
34
- Or<
35
- If_StrIncludes<Expr, ["!n"]> | If_StrIncludes<GetFmt<Data>, ["!n"]>
36
- >;
21
+ type GetFmt<T> = T extends { _fmt: infer I; } ? I : "";
37
22
 
38
-
39
- type DirectReturn<Expr, Data> =
40
- If_HasNumFmt<Expr, Data> extends true ?
41
- (
42
- number | GetWrapErr<Data>
43
- )
44
- :
45
- (
46
- string | GetWrapErr<Data>
47
- );
23
+ type TrimExpr<S> = S extends ` ${infer R}` | `\n${infer R}` | `\t${infer R}` | `\r${infer R}`
24
+ ? TrimExpr<R>
25
+ : S extends `${infer R} ` | `${infer R}\n` | `${infer R}\t` | `${infer R}\r` ? TrimExpr<R> : S;
26
+
27
+ type ExprBody<Expr> = Expr extends `${string}||${string}` ? Expr
28
+ : Expr extends `${infer Body}|${string}` ? Body : Expr;
29
+
30
+ type NumericChar = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
31
+ | "." | "+" | "-" | "*" | "/" | "%" | "^" | "(" | ")" | " " | "\n" | "\r" | "\t";
32
+
33
+ // 只对能确认的算术表达式保留窄类型;变量、函数和条件表达式可能返回布尔值。
34
+ // 长表达式保守返回联合类型,避免类型计算耗尽编译器的递归预算。
35
+ type NumericChars<S, Depth extends unknown[] = []> = Depth["length"] extends 128 ? false
36
+ : S extends "" ? true
37
+ : S extends `${infer C}${infer Rest}` ? C extends NumericChar ? NumericChars<Rest, [...Depth, unknown]> : false
38
+ : false;
39
+
40
+ type NumericExpr<S> = TrimExpr<S> extends `${number}` ? true : NumericChars<S> extends true ? true
41
+ : S extends `${string}${"<" | ">" | "=" | "!" | "&" | "|" | "?" | ":" | "'" | '"' | "(" | ")"}${string}` ? false
42
+ : S extends `${infer Left}${"+" | "-" | "*" | "/" | "%" | "^"}${infer Right}`
43
+ ? TrimExpr<Left> extends "" ? false : TrimExpr<Right> extends "" ? false : true
44
+ : false;
45
+
46
+ type StringLiteral<S> = TrimExpr<S> extends `"${infer Content}"`
47
+ ? Content extends `${string}"${string}` ? false : true
48
+ : TrimExpr<S> extends `'${infer Content}'` ? Content extends `${string}'${string}` ? false : true
49
+ : false;
50
+
51
+ type FormattedNumber<Expr, Fmt> = string extends Fmt ? string | number
52
+ : true extends If_StrIncludes<Expr, ["!n", "!N"]> | If_StrIncludes<Fmt, ["!n", "!N"]> ? number : string;
53
+
54
+ type FmtText<T> = T extends string ? T : "";
55
+
56
+ type ExpressionValue<Expr, Fmt> = 0 extends (1 & Expr) ? string | number | boolean
57
+ : Expr extends number ? FormattedNumber<Expr, Fmt>
58
+ : string extends Expr ? string | number | boolean
59
+ : StringLiteral<Expr> extends true ? string
60
+ : NumericExpr<ExprBody<Expr>> extends true ? FormattedNumber<Expr, Fmt>
61
+ : FormattedNumber<Expr, Fmt> | string | boolean;
62
+
63
+ type DirectReturn<Expr, Data> = ExpressionValue<Expr, GetFmt<Data>> | GetWrapErr<Data>;
48
64
 
49
65
 
50
66
  type FuncReturn<Arg1> =
@@ -88,25 +104,15 @@ type CalcRuntimeOptions<Fmt extends string, Err> =
88
104
  [key: string]: any; // 允许任意数据字段
89
105
  }>;
90
106
 
91
- export type Calc = <const Expr extends string | number, const Fmt extends string, const Err = never>
92
- (expr: Expr, options?: CalcRuntimeOptions<Fmt, Err>) =>
93
- (
94
- If_StrIncludes<Fmt, ["!n"]> & If_StrIncludes<Expr, ["!n"]> extends true ?
95
- number | Err
96
- :
97
- string | Err
98
- );
107
+ export type Calc = <const Expr extends string | number, const Fmt extends string = "", const Err = "-">
108
+ (expr: Expr, options?: CalcRuntimeOptions<Fmt, Err>) =>
109
+ ExpressionValue<Expr, Fmt> | Err;
99
110
 
100
- export type CalcSpace = <const Expr extends string | number, const Fmt extends string, const Err = never>
101
- (expr: Expr, options?: CalcRuntimeOptions<Fmt, Err>) =>
102
- (
103
- If_StrIncludes<Fmt, ["!n"]> & If_StrIncludes<Expr, ["!n"]> extends true ?
104
- number | Err
105
- :
106
- string | Err
107
- );
111
+ export type CalcSpace = <const Expr extends string | number, const Fmt extends string = "", const Err = "-">
112
+ (expr: Expr, options?: CalcRuntimeOptions<Fmt, Err>) =>
113
+ ExpressionValue<Expr, Fmt> | Err;
108
114
 
109
- export type CalcSum = <const Expr extends string | number, const Fmt extends string, const Err = never>
115
+ export type CalcSum = <const Expr extends string | number, const Fmt extends string = "", const Err = "-">
110
116
  (expr: Expr, options: Array<CalcRuntimeOptions<Fmt, Err>>) =>
111
117
  (
112
118
  If_StrIncludes<Fmt, ["!n"]> & If_StrIncludes<Expr, ["!n"]> extends true ?
@@ -140,11 +146,11 @@ export interface FmtOptions {
140
146
  * 直接格式化数字,跳过表达式解析
141
147
  */
142
148
  export interface Fmt {
143
- <const FmtStr extends string | undefined = undefined, const Err = never>(
144
- value: number | string,
149
+ <const FmtStr extends string | undefined = undefined, const Err = "-", const Value extends number | string = number | string, const Options extends FmtOptions = FmtOptions>(
150
+ value: Value,
145
151
  format_str?: FmtStr,
146
- options?: FmtOptions & { _error?: Err }
147
- ): If_StrIncludes<FmtStr, ["!n"]> extends true ? number | Err : string | Err;
152
+ options?: Options & { _error?: Err }
153
+ ): ExpressionValue<Value, `${FmtText<FmtStr>}${FmtText<GetFmt<Options>>}`> | Err;
148
154
 
149
155
  <const Expr extends string | number, const FmtStr extends string, const Err, const Options extends CalcRuntimeOptions<FmtStr, Err>>(
150
156
  expression: Expr,
@@ -154,7 +160,7 @@ export interface Fmt {
154
160
 
155
161
  export interface CalcWrap {
156
162
  <const Expr extends string | number>
157
- (expr: Expr): If_IncludesVar<Expr> extends true ? FuncReturn<Expr> : If_StrIncludes<Expr, ["!n", "!N"]> extends true ? number | "-" : string | "-";
163
+ (expr: Expr): If_IncludesVar<Expr> extends true ? FuncReturn<Expr> : ExpressionValue<Expr, ""> | "-";
158
164
 
159
165
  <const Expr extends string | number, const Fmt extends string, const Err, const Options extends CalcRuntimeOptions<Fmt, Err>>
160
166
  (expr: Expr, options: Options): DirectReturn<Expr, Options>;
@@ -266,7 +272,7 @@ export const calc_min: CalcSum;
266
272
  * calc_count("active", [{active: 1}, {active: 0}, {active: 1}]) // '2'
267
273
  * ```
268
274
  */
269
- export const calc_count: CalcSum;
275
+ export const calc_count: (expr: string | number, options: Array<CalcRuntimeOptions<string, unknown>>) => string;
270
276
 
271
277
  /**
272
278
  * 专用格式化函数,用于对数字进行格式化处理