@codehz/json-expr 0.4.0 → 0.5.1

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/dist/index.d.mts CHANGED
@@ -11,14 +11,85 @@ type ProxyExpression<T = unknown> = {
11
11
  * 参数可以是原始值或对应的 Proxy
12
12
  */
13
13
  type ProxifyArgs<T extends unknown[]> = { [K in keyof T]: T[K] | Proxify<T[K]> };
14
+ /**
15
+ * 需要特殊处理的数组方法名(有泛型参数,需要显式定义以保留类型推导)
16
+ */
17
+ type ProxifiedArrayMethods = "map" | "flatMap" | "filter" | "reduce" | "reduceRight" | "find" | "findIndex" | "findLast" | "findLastIndex" | "every" | "some" | "forEach" | "toSorted" | "sort";
18
+ /**
19
+ * 字符串类型的 Proxify 版本
20
+ * 将所有字符串方法返回值包装为 Proxify
21
+ * 参数允许为 Proxy 或原始值
22
+ */
23
+ type ProxifiedString = {
24
+ charAt(pos: number | Proxify<number>): Proxify<string>;
25
+ charCodeAt(index: number | Proxify<number>): Proxify<number>;
26
+ concat(...strings: (string | Proxify<string>)[]): Proxify<string>;
27
+ indexOf(searchString: string | Proxify<string>, position?: number | Proxify<number>): Proxify<number>;
28
+ lastIndexOf(searchString: string | Proxify<string>, position?: number | Proxify<number>): Proxify<number>;
29
+ localeCompare(that: string | Proxify<string>): Proxify<number>;
30
+ match(regexp: string | RegExp | Proxify<string> | Proxify<RegExp>): Proxify<RegExpMatchArray | null>;
31
+ replace(searchValue: string | RegExp | Proxify<string> | Proxify<RegExp>, replaceValue: string | Proxify<string> | Proxify<(substring: string, ...args: unknown[]) => string>): Proxify<string>;
32
+ replaceAll(searchValue: string | RegExp | Proxify<string> | Proxify<RegExp>, replaceValue: string | Proxify<string> | Proxify<(substring: string, ...args: unknown[]) => string>): Proxify<string>;
33
+ search(regexp: string | RegExp | Proxify<string> | Proxify<RegExp>): Proxify<number>;
34
+ slice(start?: number | Proxify<number>, end?: number | Proxify<number>): Proxify<string>;
35
+ split(separator: string | RegExp | Proxify<string> | Proxify<RegExp>, limit?: number | Proxify<number>): Proxify<string[]>;
36
+ substring(start: number | Proxify<number>, end?: number | Proxify<number>): Proxify<string>;
37
+ toLowerCase(): Proxify<string>;
38
+ toLocaleLowerCase(): Proxify<string>;
39
+ toUpperCase(): Proxify<string>;
40
+ toLocaleUpperCase(): Proxify<string>;
41
+ trim(): Proxify<string>;
42
+ trimStart(): Proxify<string>;
43
+ trimEnd(): Proxify<string>;
44
+ padStart(maxLength: number | Proxify<number>, fillString?: string | Proxify<string>): Proxify<string>;
45
+ padEnd(maxLength: number | Proxify<number>, fillString?: string | Proxify<string>): Proxify<string>;
46
+ repeat(count: number | Proxify<number>): Proxify<string>;
47
+ startsWith(searchString: string | Proxify<string>, position?: number | Proxify<number>): Proxify<boolean>;
48
+ endsWith(searchString: string | Proxify<string>, endPosition?: number | Proxify<number>): Proxify<boolean>;
49
+ includes(searchString: string | Proxify<string>, position?: number | Proxify<number>): Proxify<boolean>;
50
+ at(index: number | Proxify<number>): Proxify<string | undefined>;
51
+ codePointAt(pos: number | Proxify<number>): Proxify<number | undefined>;
52
+ normalize(form?: string | Proxify<string>): Proxify<string>;
53
+ matchAll(regexp: RegExp | Proxify<RegExp>): Proxify<IterableIterator<RegExpMatchArray>>;
54
+ readonly length: Proxify<number>;
55
+ [key: string]: unknown;
56
+ };
57
+ /**
58
+ * 数组类型的 Proxify 版本
59
+ * 特殊处理泛型方法(map, filter 等),确保返回值类型正确
60
+ */
61
+ type ProxifiedArray<T> = {
62
+ map<U>(callbackfn: Proxify<(value: T, index: number, array: T[]) => U>): Proxify<U[]>;
63
+ flatMap<U>(callbackfn: Proxify<(value: T, index: number, array: T[]) => U | readonly U[]>): Proxify<U[]>;
64
+ filter<S extends T>(predicate: Proxify<(value: T, index: number, array: T[]) => value is S>): Proxify<S[]>;
65
+ filter(predicate: Proxify<(value: T, index: number, array: T[]) => unknown>): Proxify<T[]>;
66
+ reduce<U>(callbackfn: Proxify<(previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U>, initialValue: U | Proxify<U>): Proxify<U>;
67
+ reduce(callbackfn: Proxify<(previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T>): Proxify<T>;
68
+ reduceRight<U>(callbackfn: Proxify<(previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U>, initialValue: U | Proxify<U>): Proxify<U>;
69
+ reduceRight(callbackfn: Proxify<(previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T>): Proxify<T>;
70
+ find<S extends T>(predicate: Proxify<(value: T, index: number, obj: T[]) => value is S>): Proxify<S | undefined>;
71
+ find(predicate: Proxify<(value: T, index: number, obj: T[]) => unknown>): Proxify<T | undefined>;
72
+ findIndex(predicate: Proxify<(value: T, index: number, obj: T[]) => unknown>): Proxify<number>;
73
+ findLast<S extends T>(predicate: Proxify<(value: T, index: number, array: T[]) => value is S>): Proxify<S | undefined>;
74
+ findLast(predicate: Proxify<(value: T, index: number, array: T[]) => unknown>): Proxify<T | undefined>;
75
+ findLastIndex(predicate: Proxify<(value: T, index: number, array: T[]) => unknown>): Proxify<number>;
76
+ every<S extends T>(predicate: Proxify<(value: T, index: number, array: T[]) => value is S>): Proxify<boolean>;
77
+ every(predicate: Proxify<(value: T, index: number, array: T[]) => unknown>): Proxify<boolean>;
78
+ some(predicate: Proxify<(value: T, index: number, array: T[]) => unknown>): Proxify<boolean>;
79
+ forEach(callbackfn: Proxify<(value: T, index: number, array: T[]) => void>): Proxify<void>;
80
+ toSorted(compareFn?: Proxify<(a: T, b: T) => number>): Proxify<T[]>;
81
+ sort(compareFn?: Proxify<(a: T, b: T) => number>): Proxify<T[]>;
82
+ } & { [K in Exclude<keyof T[], ProxifiedArrayMethods>]: Proxify<T[][K]> };
14
83
  /**
15
84
  * 将类型 T 转换为 Proxy 包装类型
16
85
  * - 始终包含 ProxyExpression<T> 标记,用于 compile 函数类型检查
17
86
  * - 函数类型:保持函数签名,但参数允许 Proxy 或原始值,返回值递归应用 Proxify
87
+ * - 字符串类型:使用 ProxifiedString 特殊处理字符串方法
88
+ * - 数组类型:使用 ProxifiedArray 特殊处理泛型方法
18
89
  * - 对象类型:映射所有属性为 Proxify
19
90
  * - 原始类型:保持不变(返回 ProxyExpression 包装)
20
91
  */
21
- type Proxify<T> = ProxyExpression<T> & (T extends ((...args: infer Args) => infer R) ? (...args: ProxifyArgs<Args>) => Proxify<R> : T extends object ? { [K in keyof T]: Proxify<T[K]> } : unknown);
92
+ type Proxify<T> = ProxyExpression<T> & (T extends string ? ProxifiedString : T extends ((...args: infer Args) => infer R) ? (...args: ProxifyArgs<Args>) => Proxify<R> : T extends readonly (infer E)[] ? ProxifiedArray<E> : T extends object ? { [K in keyof T]: Proxify<T[K]> } : unknown);
22
93
  /**
23
94
  * Variable 类型定义
24
95
  * 总是返回 Proxy 包装后的类型
@@ -100,6 +171,55 @@ type InferContextType<C> = { [K in keyof C]: C[K] extends Variable<infer T> ? T
100
171
  * @template E - Expression 类型
101
172
  */
102
173
  type InferExpressionType<E> = E extends Expression<unknown, infer R> ? R : never;
174
+ /**
175
+ * Lambda 参数代理类型
176
+ * 与普通 Proxify<T> 相同,但标记为 lambda 参数
177
+ */
178
+ type LambdaParam<T> = Proxify<T>;
179
+ /**
180
+ * 原始类型
181
+ */
182
+ type Primitive = string | number | boolean | null | undefined | symbol | bigint;
183
+ /**
184
+ * 深度允许 Proxy 或原始值的类型
185
+ * 用于 lambda 返回值,允许对象/数组中混合 Proxy 和原始值
186
+ */
187
+ type DeepPartialProxy<T> = T extends Primitive ? T : T extends readonly (infer E)[] ? readonly (Proxify<E> | DeepPartialProxy<E>)[] : T extends object ? { [K in keyof T]: Proxify<T[K]> | DeepPartialProxy<T[K]> } : T;
188
+ /**
189
+ * Lambda 函数体返回值类型
190
+ * 支持返回:
191
+ * - Proxify<R>: 完整的代理表达式
192
+ * - 原始值: 字符串、数字等
193
+ * - 对象/数组: 可以混合 Proxy 值和原始值
194
+ */
195
+ type LambdaBodyResult<T> = Proxify<T> | DeepPartialProxy<T>;
196
+ /**
197
+ * Lambda 构建函数签名
198
+ * @template Args - 参数类型元组
199
+ * @template R - 返回值类型
200
+ */
201
+ type LambdaBuilder<Args extends unknown[], R> = (...params: { [K in keyof Args]: LambdaParam<Args[K]> }) => LambdaBodyResult<R>;
202
+ /**
203
+ * Lambda 表达式类型
204
+ * 表示一个可序列化的函数
205
+ */
206
+ type Lambda<Args extends unknown[], R> = Proxify<(...args: Args) => R>;
207
+ /**
208
+ * 从 Lambda 类型提取参数类型
209
+ */
210
+ type InferLambdaArgs<L> = L extends Lambda<infer Args, unknown> ? Args : never;
211
+ /**
212
+ * 从 Lambda 类型提取返回类型
213
+ */
214
+ type InferLambdaReturn<L> = L extends Lambda<unknown[], infer R> ? R : never;
215
+ /**
216
+ * 常用 Lambda 类型别名
217
+ */
218
+ type MapCallback<T, R> = Lambda<[T, number, T[]], R>;
219
+ type FilterCallback<T> = Lambda<[T, number, T[]], boolean>;
220
+ type ReduceCallback<T, R> = Lambda<[R, T, number, T[]], R>;
221
+ type FindCallback<T> = Lambda<[T, number, T[]], boolean>;
222
+ type SortCallback<T> = Lambda<[T, T], number>;
103
223
  //#endregion
104
224
  //#region src/compile.d.ts
105
225
  /**
@@ -117,7 +237,7 @@ interface CompileOptions {
117
237
  * 将 Proxy Expression 编译为可序列化的 JSON 结构
118
238
  *
119
239
  * @template TResult - 表达式结果类型
120
- * @param expression - Proxy Expression
240
+ * @param expression - Proxy Expression,或包含 Proxy 的对象/数组/原始值
121
241
  * @param variables - 所有使用的变量定义
122
242
  * @param options - 编译选项
123
243
  * @returns 编译后的数据结构 [变量名列表, 表达式1, 表达式2, ...]
@@ -134,7 +254,7 @@ interface CompileOptions {
134
254
  * // => [["x", "y"], "($0+$1)*$0"]
135
255
  * ```
136
256
  */
137
- declare function compile<TResult>(expression: ProxyExpression<TResult>, variables: Record<string, unknown>, options?: CompileOptions): CompiledData;
257
+ declare function compile<TResult>(expression: LambdaBodyResult<TResult>, variables: Record<string, unknown>, options?: CompileOptions): CompiledData;
138
258
  //#endregion
139
259
  //#region src/evaluate.d.ts
140
260
  /**
@@ -154,7 +274,7 @@ declare function compile<TResult>(expression: ProxyExpression<TResult>, variable
154
274
  * // => 6 (3 * 2)
155
275
  * ```
156
276
  */
157
- declare function evaluate<TResult>(data: CompiledData, values: Record<string, unknown>): TResult;
277
+ declare function evaluate<TResult = unknown>(data: CompiledData, values: Record<string, unknown>): TResult;
158
278
  //#endregion
159
279
  //#region src/type-parser.d.ts
160
280
  /** 去除字符串两端空白 */
@@ -359,6 +479,27 @@ type ExpressionType<Source extends string, TContext> = ValidateExpression<Source
359
479
  */
360
480
  declare function expr<TContext extends Record<string, unknown>>(context: TContext): <TSource extends string>(source: ValidateExpression<TSource, TContext> extends never ? never : TSource) => Proxify<InferExpressionResult<TSource, TContext>>;
361
481
  //#endregion
482
+ //#region src/lambda.d.ts
483
+ /**
484
+ * 创建类型安全的 lambda 表达式
485
+ *
486
+ * @template Args - 参数类型元组
487
+ * @template R - 返回值类型
488
+ * @param builder - Lambda 构建函数,接收参数代理,返回函数体表达式
489
+ * @returns Lambda 表达式代理
490
+ *
491
+ * @example
492
+ * ```ts
493
+ * const add = lambda<[number, number], number>(
494
+ * (a, b) => expr({ a, b })("a + b")
495
+ * );
496
+ *
497
+ * const numbers = variable<number[]>();
498
+ * const sum = numbers.reduce(add, 0);
499
+ * ```
500
+ */
501
+ declare function lambda<Args extends unknown[], R>(builder: LambdaBuilder<Args, R>): Lambda<Args, R>;
502
+ //#endregion
362
503
  //#region src/proxy-metadata.d.ts
363
504
  /**
364
505
  * 检查对象是否是 Proxy variable
@@ -409,5 +550,32 @@ declare function variable<T>(): Variable<T>;
409
550
  */
410
551
  declare function getVariableId(variable: unknown): symbol | undefined;
411
552
  //#endregion
412
- export { type BranchNode, type CompileContext, type CompileOptions, type CompiledData, type CompiledExpression, type ContextTypeMap, type ControlFlowNode, type ExprNode, type Expression, type ExpressionType, type ExtractType, type InferContextType, type InferExpressionResult, type InferExpressionType, type InferVariableType, type JumpNode, type ParseExpression, type PhiNode, type Proxify, type ProxyExpression, type ValidateExpression, type Variable, compile, evaluate, expr, getVariableId, isProxy, isProxyExpression, isProxyVariable, t, variable };
553
+ //#region src/wrap.d.ts
554
+ /**
555
+ * 将静态值包装为 Proxy Expression
556
+ * 返回的 Proxy 可以像 Variable 一样调用方法和访问属性
557
+ *
558
+ * @template T - 值的类型
559
+ * @param value - 要包装的静态值(支持原始值、对象、数组、Date、RegExp 等)
560
+ * @returns Proxy Expression,可以继续链式调用
561
+ *
562
+ * @example
563
+ * ```ts
564
+ * // 包装 RegExp
565
+ * const pattern = wrap(/^[a-z]+$/i);
566
+ * const input = variable<string>();
567
+ * const result = pattern.match(input);
568
+ *
569
+ * // 包装 Date
570
+ * const now = wrap(new Date());
571
+ * const year = now.getFullYear();
572
+ *
573
+ * // 包装数组
574
+ * const numbers = wrap([1, 2, 3, 4, 5]);
575
+ * const doubled = numbers.map((x) => x * 2);
576
+ * ```
577
+ */
578
+ declare function wrap<T>(value: T): Proxify<T>;
579
+ //#endregion
580
+ export { type BranchNode, type CompileContext, type CompileOptions, type CompiledData, type CompiledExpression, type ContextTypeMap, type ControlFlowNode, type ExprNode, type Expression, type ExpressionType, type ExtractType, type FilterCallback, type FindCallback, type InferContextType, type InferExpressionResult, type InferExpressionType, type InferLambdaArgs, type InferLambdaReturn, type InferVariableType, type JumpNode, type Lambda, type LambdaBuilder, type LambdaParam, type MapCallback, type ParseExpression, type PhiNode, type Proxify, type ProxyExpression, type ReduceCallback, type SortCallback, type ValidateExpression, type Variable, compile, evaluate, expr, getVariableId, isProxy, isProxyExpression, isProxyVariable, lambda, t, variable, wrap };
413
581
  //# sourceMappingURL=index.d.mts.map