@object-ui/core 0.5.0 → 2.0.0
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +11 -0
- package/dist/actions/ActionRunner.d.ts +228 -4
- package/dist/actions/ActionRunner.js +397 -45
- package/dist/actions/TransactionManager.d.ts +193 -0
- package/dist/actions/TransactionManager.js +410 -0
- package/dist/actions/index.d.ts +1 -0
- package/dist/actions/index.js +1 -0
- package/dist/adapters/ApiDataSource.d.ts +69 -0
- package/dist/adapters/ApiDataSource.js +293 -0
- package/dist/adapters/ValueDataSource.d.ts +55 -0
- package/dist/adapters/ValueDataSource.js +287 -0
- package/dist/adapters/index.d.ts +3 -0
- package/dist/adapters/index.js +5 -2
- package/dist/adapters/resolveDataSource.d.ts +40 -0
- package/dist/adapters/resolveDataSource.js +59 -0
- package/dist/data-scope/DataScopeManager.d.ts +127 -0
- package/dist/data-scope/DataScopeManager.js +229 -0
- package/dist/data-scope/index.d.ts +10 -0
- package/dist/data-scope/index.js +10 -0
- package/dist/evaluator/ExpressionEvaluator.d.ts +11 -1
- package/dist/evaluator/ExpressionEvaluator.js +32 -8
- package/dist/evaluator/FormulaFunctions.d.ts +58 -0
- package/dist/evaluator/FormulaFunctions.js +350 -0
- package/dist/evaluator/index.d.ts +1 -0
- package/dist/evaluator/index.js +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -2
- package/dist/query/query-ast.d.ts +2 -2
- package/dist/query/query-ast.js +3 -3
- package/dist/registry/Registry.d.ts +10 -0
- package/dist/registry/Registry.js +2 -1
- package/dist/registry/WidgetRegistry.d.ts +120 -0
- package/dist/registry/WidgetRegistry.js +275 -0
- package/dist/theme/ThemeEngine.d.ts +82 -0
- package/dist/theme/ThemeEngine.js +400 -0
- package/dist/theme/index.d.ts +8 -0
- package/dist/theme/index.js +8 -0
- package/dist/validation/index.d.ts +1 -1
- package/dist/validation/index.js +1 -1
- package/dist/validation/validation-engine.d.ts +19 -1
- package/dist/validation/validation-engine.js +67 -2
- package/dist/validation/validators/index.d.ts +1 -1
- package/dist/validation/validators/index.js +1 -1
- package/dist/validation/validators/object-validation-engine.d.ts +2 -2
- package/dist/validation/validators/object-validation-engine.js +1 -1
- package/package.json +4 -3
- package/src/actions/ActionRunner.ts +577 -55
- package/src/actions/TransactionManager.ts +521 -0
- package/src/actions/__tests__/ActionRunner.params.test.ts +134 -0
- package/src/actions/__tests__/ActionRunner.test.ts +711 -0
- package/src/actions/__tests__/TransactionManager.test.ts +447 -0
- package/src/actions/index.ts +1 -0
- package/src/adapters/ApiDataSource.ts +349 -0
- package/src/adapters/ValueDataSource.ts +332 -0
- package/src/adapters/__tests__/ApiDataSource.test.ts +418 -0
- package/src/adapters/__tests__/ValueDataSource.test.ts +325 -0
- package/src/adapters/__tests__/resolveDataSource.test.ts +144 -0
- package/src/adapters/index.ts +6 -1
- package/src/adapters/resolveDataSource.ts +79 -0
- package/src/builder/__tests__/schema-builder.test.ts +235 -0
- package/src/data-scope/DataScopeManager.ts +269 -0
- package/src/data-scope/__tests__/DataScopeManager.test.ts +211 -0
- package/src/data-scope/index.ts +16 -0
- package/src/evaluator/ExpressionEvaluator.ts +34 -8
- package/src/evaluator/FormulaFunctions.ts +398 -0
- package/src/evaluator/__tests__/ExpressionContext.test.ts +110 -0
- package/src/evaluator/__tests__/FormulaFunctions.test.ts +447 -0
- package/src/evaluator/index.ts +1 -0
- package/src/index.ts +4 -3
- package/src/query/__tests__/window-functions.test.ts +1 -1
- package/src/query/query-ast.ts +3 -3
- package/src/registry/Registry.ts +12 -1
- package/src/registry/WidgetRegistry.ts +316 -0
- package/src/registry/__tests__/WidgetRegistry.test.ts +321 -0
- package/src/theme/ThemeEngine.ts +452 -0
- package/src/theme/__tests__/ThemeEngine.test.ts +606 -0
- package/src/theme/index.ts +22 -0
- package/src/validation/__tests__/object-validation-engine.test.ts +1 -1
- package/src/validation/__tests__/schema-validator.test.ts +118 -0
- package/src/validation/index.ts +1 -1
- package/src/validation/validation-engine.ts +61 -2
- package/src/validation/validators/index.ts +1 -1
- package/src/validation/validators/object-validation-engine.ts +2 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @object-ui/core - DataScope Module
|
|
3
|
+
*
|
|
4
|
+
* Runtime data scope management for row-level security and
|
|
5
|
+
* reactive data state within the UI component tree.
|
|
6
|
+
*
|
|
7
|
+
* @module data-scope
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
DataScopeManager,
|
|
13
|
+
defaultDataScopeManager,
|
|
14
|
+
type RowLevelFilter,
|
|
15
|
+
type DataScopeConfig,
|
|
16
|
+
} from './DataScopeManager.js';
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
|
|
19
19
|
import { ExpressionContext } from './ExpressionContext.js';
|
|
20
20
|
import { ExpressionCache } from './ExpressionCache.js';
|
|
21
|
+
import { FormulaFunctions } from './FormulaFunctions.js';
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* Options for expression evaluation
|
|
@@ -47,8 +48,13 @@ export interface EvaluationOptions {
|
|
|
47
48
|
export class ExpressionEvaluator {
|
|
48
49
|
private context: ExpressionContext;
|
|
49
50
|
private cache: ExpressionCache;
|
|
51
|
+
private formulas: FormulaFunctions;
|
|
50
52
|
|
|
51
|
-
constructor(
|
|
53
|
+
constructor(
|
|
54
|
+
context?: ExpressionContext | Record<string, any>,
|
|
55
|
+
cache?: ExpressionCache,
|
|
56
|
+
formulas?: FormulaFunctions,
|
|
57
|
+
) {
|
|
52
58
|
if (context instanceof ExpressionContext) {
|
|
53
59
|
this.context = context;
|
|
54
60
|
} else {
|
|
@@ -57,6 +63,7 @@ export class ExpressionEvaluator {
|
|
|
57
63
|
|
|
58
64
|
// Use provided cache or create a new one
|
|
59
65
|
this.cache = cache || new ExpressionCache();
|
|
66
|
+
this.formulas = formulas || new FormulaFunctions();
|
|
60
67
|
}
|
|
61
68
|
|
|
62
69
|
/**
|
|
@@ -139,9 +146,13 @@ export class ExpressionEvaluator {
|
|
|
139
146
|
// Create a safe evaluation function
|
|
140
147
|
const contextObj = this.context.toObject();
|
|
141
148
|
|
|
149
|
+
// Inject formula functions into the evaluation context
|
|
150
|
+
const formulaObj = this.formulas.toObject();
|
|
151
|
+
const mergedContext = { ...formulaObj, ...contextObj };
|
|
152
|
+
|
|
142
153
|
// Build safe function with context variables
|
|
143
|
-
const varNames = Object.keys(
|
|
144
|
-
const varValues = Object.values(
|
|
154
|
+
const varNames = Object.keys(mergedContext);
|
|
155
|
+
const varValues = Object.values(mergedContext);
|
|
145
156
|
|
|
146
157
|
// Use cached compilation
|
|
147
158
|
const compiled = this.cache.compile(expression, varNames);
|
|
@@ -219,8 +230,8 @@ export class ExpressionEvaluator {
|
|
|
219
230
|
* Create a new evaluator with additional context data
|
|
220
231
|
*/
|
|
221
232
|
withContext(data: Record<string, any>): ExpressionEvaluator {
|
|
222
|
-
// Share the cache with the new evaluator for maximum efficiency
|
|
223
|
-
return new ExpressionEvaluator(this.context.createChild(data), this.cache);
|
|
233
|
+
// Share the cache and formulas with the new evaluator for maximum efficiency
|
|
234
|
+
return new ExpressionEvaluator(this.context.createChild(data), this.cache, this.formulas);
|
|
224
235
|
}
|
|
225
236
|
|
|
226
237
|
/**
|
|
@@ -236,12 +247,27 @@ export class ExpressionEvaluator {
|
|
|
236
247
|
clearCache(): void {
|
|
237
248
|
this.cache.clear();
|
|
238
249
|
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Get the formula functions registry
|
|
253
|
+
*/
|
|
254
|
+
getFormulas(): FormulaFunctions {
|
|
255
|
+
return this.formulas;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Register a custom formula function
|
|
260
|
+
*/
|
|
261
|
+
registerFunction(name: string, fn: (...args: any[]) => any): void {
|
|
262
|
+
this.formulas.register(name, fn);
|
|
263
|
+
}
|
|
239
264
|
}
|
|
240
265
|
|
|
241
266
|
/**
|
|
242
|
-
* Shared global cache for convenience functions
|
|
267
|
+
* Shared global cache and formulas for convenience functions
|
|
243
268
|
*/
|
|
244
269
|
const globalCache = new ExpressionCache();
|
|
270
|
+
const globalFormulas = new FormulaFunctions();
|
|
245
271
|
|
|
246
272
|
/**
|
|
247
273
|
* Convenience function to quickly evaluate an expression
|
|
@@ -251,7 +277,7 @@ export function evaluateExpression(
|
|
|
251
277
|
context: Record<string, any> = {},
|
|
252
278
|
options: EvaluationOptions = {}
|
|
253
279
|
): any {
|
|
254
|
-
const evaluator = new ExpressionEvaluator(context, globalCache);
|
|
280
|
+
const evaluator = new ExpressionEvaluator(context, globalCache, globalFormulas);
|
|
255
281
|
return evaluator.evaluate(expression, options);
|
|
256
282
|
}
|
|
257
283
|
|
|
@@ -262,6 +288,6 @@ export function evaluateCondition(
|
|
|
262
288
|
condition: string | boolean | undefined,
|
|
263
289
|
context: Record<string, any> = {}
|
|
264
290
|
): boolean {
|
|
265
|
-
const evaluator = new ExpressionEvaluator(context, globalCache);
|
|
291
|
+
const evaluator = new ExpressionEvaluator(context, globalCache, globalFormulas);
|
|
266
292
|
return evaluator.evaluateCondition(condition);
|
|
267
293
|
}
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @object-ui/core - Formula Functions
|
|
11
|
+
*
|
|
12
|
+
* Built-in formula functions for the expression engine.
|
|
13
|
+
* Provides aggregation, date, logic, and string functions
|
|
14
|
+
* compatible with low-code platform expression evaluation.
|
|
15
|
+
*
|
|
16
|
+
* @module evaluator
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A formula function that can be registered with the expression evaluator
|
|
22
|
+
*/
|
|
23
|
+
export type FormulaFunction = (...args: any[]) => any;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Registry of built-in formula functions
|
|
27
|
+
*/
|
|
28
|
+
export class FormulaFunctions {
|
|
29
|
+
private functions = new Map<string, FormulaFunction>();
|
|
30
|
+
|
|
31
|
+
constructor() {
|
|
32
|
+
this.registerDefaults();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Register a custom formula function
|
|
37
|
+
*/
|
|
38
|
+
register(name: string, fn: FormulaFunction): void {
|
|
39
|
+
this.functions.set(name.toUpperCase(), fn);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get a formula function by name
|
|
44
|
+
*/
|
|
45
|
+
get(name: string): FormulaFunction | undefined {
|
|
46
|
+
return this.functions.get(name.toUpperCase());
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Check if a function is registered
|
|
51
|
+
*/
|
|
52
|
+
has(name: string): boolean {
|
|
53
|
+
return this.functions.has(name.toUpperCase());
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Get all registered function names
|
|
58
|
+
*/
|
|
59
|
+
getNames(): string[] {
|
|
60
|
+
return Array.from(this.functions.keys());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Get all functions as a plain object (for injection into expression context)
|
|
65
|
+
*/
|
|
66
|
+
toObject(): Record<string, FormulaFunction> {
|
|
67
|
+
const result: Record<string, FormulaFunction> = {};
|
|
68
|
+
for (const [name, fn] of this.functions) {
|
|
69
|
+
result[name] = fn;
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Register all default built-in functions
|
|
76
|
+
*/
|
|
77
|
+
private registerDefaults(): void {
|
|
78
|
+
this.registerAggregationFunctions();
|
|
79
|
+
this.registerDateFunctions();
|
|
80
|
+
this.registerLogicFunctions();
|
|
81
|
+
this.registerStringFunctions();
|
|
82
|
+
this.registerStringSearchFunctions();
|
|
83
|
+
this.registerStatisticalFunctions();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ==========================================================================
|
|
87
|
+
// Aggregation Functions
|
|
88
|
+
// ==========================================================================
|
|
89
|
+
|
|
90
|
+
private registerAggregationFunctions(): void {
|
|
91
|
+
this.register('SUM', (...args: any[]): number => {
|
|
92
|
+
const values = flattenNumericArgs(args);
|
|
93
|
+
return values.reduce((sum, v) => sum + v, 0);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
this.register('AVG', (...args: any[]): number => {
|
|
97
|
+
const values = flattenNumericArgs(args);
|
|
98
|
+
if (values.length === 0) return 0;
|
|
99
|
+
return values.reduce((sum, v) => sum + v, 0) / values.length;
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
this.register('COUNT', (...args: any[]): number => {
|
|
103
|
+
const values = flattenArgs(args);
|
|
104
|
+
return values.filter(v => v != null).length;
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
this.register('MIN', (...args: any[]): number => {
|
|
108
|
+
const values = flattenNumericArgs(args);
|
|
109
|
+
if (values.length === 0) return 0;
|
|
110
|
+
return Math.min(...values);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
this.register('MAX', (...args: any[]): number => {
|
|
114
|
+
const values = flattenNumericArgs(args);
|
|
115
|
+
if (values.length === 0) return 0;
|
|
116
|
+
return Math.max(...values);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ==========================================================================
|
|
121
|
+
// Date Functions
|
|
122
|
+
// ==========================================================================
|
|
123
|
+
|
|
124
|
+
private registerDateFunctions(): void {
|
|
125
|
+
this.register('TODAY', (): string => {
|
|
126
|
+
const now = new Date();
|
|
127
|
+
return now.toISOString().split('T')[0];
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
this.register('NOW', (): string => {
|
|
131
|
+
return new Date().toISOString();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
this.register('DATEADD', (dateStr: string, amount: number, unit: string): string => {
|
|
135
|
+
const date = new Date(dateStr);
|
|
136
|
+
if (isNaN(date.getTime())) {
|
|
137
|
+
throw new Error(`DATEADD: Invalid date "${dateStr}"`);
|
|
138
|
+
}
|
|
139
|
+
const normalizedUnit = String(unit).toLowerCase();
|
|
140
|
+
switch (normalizedUnit) {
|
|
141
|
+
case 'day':
|
|
142
|
+
case 'days':
|
|
143
|
+
date.setDate(date.getDate() + amount);
|
|
144
|
+
break;
|
|
145
|
+
case 'month':
|
|
146
|
+
case 'months':
|
|
147
|
+
date.setMonth(date.getMonth() + amount);
|
|
148
|
+
break;
|
|
149
|
+
case 'year':
|
|
150
|
+
case 'years':
|
|
151
|
+
date.setFullYear(date.getFullYear() + amount);
|
|
152
|
+
break;
|
|
153
|
+
case 'hour':
|
|
154
|
+
case 'hours':
|
|
155
|
+
date.setHours(date.getHours() + amount);
|
|
156
|
+
break;
|
|
157
|
+
case 'minute':
|
|
158
|
+
case 'minutes':
|
|
159
|
+
date.setMinutes(date.getMinutes() + amount);
|
|
160
|
+
break;
|
|
161
|
+
default:
|
|
162
|
+
throw new Error(`DATEADD: Unsupported unit "${unit}"`);
|
|
163
|
+
}
|
|
164
|
+
return date.toISOString();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
this.register('DATEDIFF', (dateStr1: string, dateStr2: string, unit: string): number => {
|
|
168
|
+
const date1 = new Date(dateStr1);
|
|
169
|
+
const date2 = new Date(dateStr2);
|
|
170
|
+
if (isNaN(date1.getTime())) {
|
|
171
|
+
throw new Error(`DATEDIFF: Invalid date "${dateStr1}"`);
|
|
172
|
+
}
|
|
173
|
+
if (isNaN(date2.getTime())) {
|
|
174
|
+
throw new Error(`DATEDIFF: Invalid date "${dateStr2}"`);
|
|
175
|
+
}
|
|
176
|
+
const diffMs = date2.getTime() - date1.getTime();
|
|
177
|
+
const normalizedUnit = String(unit).toLowerCase();
|
|
178
|
+
switch (normalizedUnit) {
|
|
179
|
+
case 'day':
|
|
180
|
+
case 'days':
|
|
181
|
+
return Math.floor(diffMs / (1000 * 60 * 60 * 24));
|
|
182
|
+
case 'month':
|
|
183
|
+
case 'months':
|
|
184
|
+
return (date2.getFullYear() - date1.getFullYear()) * 12 + (date2.getMonth() - date1.getMonth());
|
|
185
|
+
case 'year':
|
|
186
|
+
case 'years':
|
|
187
|
+
return date2.getFullYear() - date1.getFullYear();
|
|
188
|
+
case 'hour':
|
|
189
|
+
case 'hours':
|
|
190
|
+
return Math.floor(diffMs / (1000 * 60 * 60));
|
|
191
|
+
case 'minute':
|
|
192
|
+
case 'minutes':
|
|
193
|
+
return Math.floor(diffMs / (1000 * 60));
|
|
194
|
+
default:
|
|
195
|
+
throw new Error(`DATEDIFF: Unsupported unit "${unit}"`);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
this.register('DATEFORMAT', (dateStr: string, format: string): string => {
|
|
200
|
+
const date = new Date(dateStr);
|
|
201
|
+
if (isNaN(date.getTime())) {
|
|
202
|
+
throw new Error(`DATEFORMAT: Invalid date "${dateStr}"`);
|
|
203
|
+
}
|
|
204
|
+
const pad = (n: number, len = 2) => String(n).padStart(len, '0');
|
|
205
|
+
return format
|
|
206
|
+
.replace('YYYY', String(date.getFullYear()))
|
|
207
|
+
.replace('YY', String(date.getFullYear()).slice(-2))
|
|
208
|
+
.replace('MM', pad(date.getMonth() + 1))
|
|
209
|
+
.replace('DD', pad(date.getDate()))
|
|
210
|
+
.replace('HH', pad(date.getHours()))
|
|
211
|
+
.replace('mm', pad(date.getMinutes()))
|
|
212
|
+
.replace('ss', pad(date.getSeconds()));
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ==========================================================================
|
|
217
|
+
// Logic Functions
|
|
218
|
+
// ==========================================================================
|
|
219
|
+
|
|
220
|
+
private registerLogicFunctions(): void {
|
|
221
|
+
this.register('IF', (condition: any, trueValue: any, falseValue: any): any => {
|
|
222
|
+
return condition ? trueValue : falseValue;
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
this.register('AND', (...args: any[]): boolean => {
|
|
226
|
+
return args.every(Boolean);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
this.register('OR', (...args: any[]): boolean => {
|
|
230
|
+
return args.some(Boolean);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
this.register('NOT', (value: any): boolean => {
|
|
234
|
+
return !value;
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
this.register('SWITCH', (expr: any, ...cases: any[]): any => {
|
|
238
|
+
// SWITCH(expr, val1, result1, val2, result2, ..., defaultResult)
|
|
239
|
+
for (let i = 0; i < cases.length - 1; i += 2) {
|
|
240
|
+
if (expr === cases[i]) {
|
|
241
|
+
return cases[i + 1];
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
// Return default value if odd number of case args
|
|
245
|
+
if (cases.length % 2 === 1) {
|
|
246
|
+
return cases[cases.length - 1];
|
|
247
|
+
}
|
|
248
|
+
return undefined;
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ==========================================================================
|
|
253
|
+
// String Functions
|
|
254
|
+
// ==========================================================================
|
|
255
|
+
|
|
256
|
+
private registerStringFunctions(): void {
|
|
257
|
+
this.register('CONCAT', (...args: any[]): string => {
|
|
258
|
+
return args.map(a => String(a ?? '')).join('');
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
this.register('LEFT', (text: string, count: number): string => {
|
|
262
|
+
return String(text ?? '').substring(0, count);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
this.register('RIGHT', (text: string, count: number): string => {
|
|
266
|
+
const str = String(text ?? '');
|
|
267
|
+
return str.substring(Math.max(0, str.length - count));
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
this.register('TRIM', (text: string): string => {
|
|
271
|
+
return String(text ?? '').trim();
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
this.register('UPPER', (text: string): string => {
|
|
275
|
+
return String(text ?? '').toUpperCase();
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
this.register('LOWER', (text: string): string => {
|
|
279
|
+
return String(text ?? '').toLowerCase();
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// ==========================================================================
|
|
284
|
+
// String Search Functions
|
|
285
|
+
// ==========================================================================
|
|
286
|
+
|
|
287
|
+
private registerStringSearchFunctions(): void {
|
|
288
|
+
this.register('FIND', (search: string, text: string, startPos?: number): number => {
|
|
289
|
+
const str = String(text ?? '');
|
|
290
|
+
const idx = str.indexOf(String(search ?? ''), startPos ?? 0);
|
|
291
|
+
return idx;
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
this.register('REPLACE', (text: string, search: string, replacement: string): string => {
|
|
295
|
+
const str = String(text ?? '');
|
|
296
|
+
return str.split(String(search ?? '')).join(String(replacement ?? ''));
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
this.register('SUBSTRING', (text: string, start: number, length?: number): string => {
|
|
300
|
+
const str = String(text ?? '');
|
|
301
|
+
if (length !== undefined) {
|
|
302
|
+
return str.substring(start, start + length);
|
|
303
|
+
}
|
|
304
|
+
return str.substring(start);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
this.register('REGEX', (text: string, pattern: string, flags?: string): boolean => {
|
|
308
|
+
const str = String(text ?? '');
|
|
309
|
+
const regex = new RegExp(pattern, flags);
|
|
310
|
+
return regex.test(str);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
this.register('LEN', (text: string): number => {
|
|
314
|
+
return String(text ?? '').length;
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// ==========================================================================
|
|
319
|
+
// Statistical Functions
|
|
320
|
+
// ==========================================================================
|
|
321
|
+
|
|
322
|
+
private registerStatisticalFunctions(): void {
|
|
323
|
+
this.register('MEDIAN', (...args: any[]): number => {
|
|
324
|
+
const values = flattenNumericArgs(args).sort((a, b) => a - b);
|
|
325
|
+
if (values.length === 0) return 0;
|
|
326
|
+
const mid = Math.floor(values.length / 2);
|
|
327
|
+
return values.length % 2 !== 0
|
|
328
|
+
? values[mid]
|
|
329
|
+
: (values[mid - 1] + values[mid]) / 2;
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
this.register('STDEV', (...args: any[]): number => {
|
|
333
|
+
const values = flattenNumericArgs(args);
|
|
334
|
+
if (values.length < 2) return 0;
|
|
335
|
+
const mean = values.reduce((sum, v) => sum + v, 0) / values.length;
|
|
336
|
+
const squaredDiffs = values.map(v => (v - mean) ** 2);
|
|
337
|
+
const variance = squaredDiffs.reduce((sum, v) => sum + v, 0) / (values.length - 1);
|
|
338
|
+
return Math.sqrt(variance);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
this.register('VARIANCE', (...args: any[]): number => {
|
|
342
|
+
const values = flattenNumericArgs(args);
|
|
343
|
+
if (values.length < 2) return 0;
|
|
344
|
+
const mean = values.reduce((sum, v) => sum + v, 0) / values.length;
|
|
345
|
+
const squaredDiffs = values.map(v => (v - mean) ** 2);
|
|
346
|
+
return squaredDiffs.reduce((sum, v) => sum + v, 0) / (values.length - 1);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
this.register('PERCENTILE', (percentile: number, ...args: any[]): number => {
|
|
350
|
+
const values = flattenNumericArgs(args).sort((a, b) => a - b);
|
|
351
|
+
if (values.length === 0) return 0;
|
|
352
|
+
const p = Math.max(0, Math.min(100, percentile)) / 100;
|
|
353
|
+
const index = p * (values.length - 1);
|
|
354
|
+
const lower = Math.floor(index);
|
|
355
|
+
const upper = Math.ceil(index);
|
|
356
|
+
if (lower === upper) return values[lower];
|
|
357
|
+
const fraction = index - lower;
|
|
358
|
+
return values[lower] + fraction * (values[upper] - values[lower]);
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// ==========================================================================
|
|
364
|
+
// Helpers
|
|
365
|
+
// ==========================================================================
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Flatten nested arrays and extract numeric values
|
|
369
|
+
*/
|
|
370
|
+
function flattenNumericArgs(args: any[]): number[] {
|
|
371
|
+
const result: number[] = [];
|
|
372
|
+
for (const arg of args) {
|
|
373
|
+
if (Array.isArray(arg)) {
|
|
374
|
+
result.push(...flattenNumericArgs(arg));
|
|
375
|
+
} else {
|
|
376
|
+
const num = Number(arg);
|
|
377
|
+
if (!isNaN(num)) {
|
|
378
|
+
result.push(num);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return result;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Flatten nested arrays
|
|
387
|
+
*/
|
|
388
|
+
function flattenArgs(args: any[]): any[] {
|
|
389
|
+
const result: any[] = [];
|
|
390
|
+
for (const arg of args) {
|
|
391
|
+
if (Array.isArray(arg)) {
|
|
392
|
+
result.push(...flattenArgs(arg));
|
|
393
|
+
} else {
|
|
394
|
+
result.push(arg);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return result;
|
|
398
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { ExpressionContext } from '../../evaluator/ExpressionContext';
|
|
3
|
+
|
|
4
|
+
describe('ExpressionContext', () => {
|
|
5
|
+
it('creates a context with initial data', () => {
|
|
6
|
+
const ctx = new ExpressionContext({ name: 'Alice', age: 30 });
|
|
7
|
+
expect(ctx.get('name')).toBe('Alice');
|
|
8
|
+
expect(ctx.get('age')).toBe(30);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('creates an empty context', () => {
|
|
12
|
+
const ctx = new ExpressionContext();
|
|
13
|
+
expect(ctx.get('anything')).toBeUndefined();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('sets and gets values', () => {
|
|
17
|
+
const ctx = new ExpressionContext();
|
|
18
|
+
ctx.set('key', 'value');
|
|
19
|
+
expect(ctx.get('key')).toBe('value');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('checks existence with has()', () => {
|
|
23
|
+
const ctx = new ExpressionContext({ x: 1 });
|
|
24
|
+
expect(ctx.has('x')).toBe(true);
|
|
25
|
+
expect(ctx.has('y')).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('supports dot notation for nested access', () => {
|
|
29
|
+
const ctx = new ExpressionContext({ user: { name: 'Bob', address: { city: 'NYC' } } });
|
|
30
|
+
expect(ctx.get('user.name')).toBe('Bob');
|
|
31
|
+
expect(ctx.get('user.address.city')).toBe('NYC');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('returns undefined for non-existent nested paths', () => {
|
|
35
|
+
const ctx = new ExpressionContext({ user: { name: 'Bob' } });
|
|
36
|
+
expect(ctx.get('user.email')).toBeUndefined();
|
|
37
|
+
expect(ctx.get('user.address.city')).toBeUndefined();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('scope management', () => {
|
|
41
|
+
it('pushScope adds a new scope', () => {
|
|
42
|
+
const ctx = new ExpressionContext({ base: 'value' });
|
|
43
|
+
ctx.pushScope({ scoped: 'data' });
|
|
44
|
+
expect(ctx.get('scoped')).toBe('data');
|
|
45
|
+
expect(ctx.get('base')).toBe('value');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('popScope removes the latest scope', () => {
|
|
49
|
+
const ctx = new ExpressionContext({ base: 'value' });
|
|
50
|
+
ctx.pushScope({ temp: 'data' });
|
|
51
|
+
expect(ctx.get('temp')).toBe('data');
|
|
52
|
+
ctx.popScope();
|
|
53
|
+
expect(ctx.get('temp')).toBeUndefined();
|
|
54
|
+
expect(ctx.get('base')).toBe('value');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('inner scope shadows outer scope', () => {
|
|
58
|
+
const ctx = new ExpressionContext({ name: 'outer' });
|
|
59
|
+
ctx.pushScope({ name: 'inner' });
|
|
60
|
+
expect(ctx.get('name')).toBe('inner');
|
|
61
|
+
ctx.popScope();
|
|
62
|
+
expect(ctx.get('name')).toBe('outer');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('supports multiple nested scopes', () => {
|
|
66
|
+
const ctx = new ExpressionContext({ level: 0 });
|
|
67
|
+
ctx.pushScope({ level: 1 });
|
|
68
|
+
ctx.pushScope({ level: 2 });
|
|
69
|
+
expect(ctx.get('level')).toBe(2);
|
|
70
|
+
ctx.popScope();
|
|
71
|
+
expect(ctx.get('level')).toBe(1);
|
|
72
|
+
ctx.popScope();
|
|
73
|
+
expect(ctx.get('level')).toBe(0);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('toObject', () => {
|
|
78
|
+
it('flattens all scopes into one object', () => {
|
|
79
|
+
const ctx = new ExpressionContext({ a: 1 });
|
|
80
|
+
ctx.pushScope({ b: 2 });
|
|
81
|
+
const obj = ctx.toObject();
|
|
82
|
+
expect(obj.a).toBe(1);
|
|
83
|
+
expect(obj.b).toBe(2);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('inner scope values take precedence in flattened object', () => {
|
|
87
|
+
const ctx = new ExpressionContext({ x: 'outer' });
|
|
88
|
+
ctx.pushScope({ x: 'inner' });
|
|
89
|
+
const obj = ctx.toObject();
|
|
90
|
+
expect(obj.x).toBe('inner');
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('createChild', () => {
|
|
95
|
+
it('creates a child context with additional data', () => {
|
|
96
|
+
const parent = new ExpressionContext({ parent: 'value' });
|
|
97
|
+
const child = parent.createChild({ child: 'data' });
|
|
98
|
+
expect(child.get('parent')).toBe('value');
|
|
99
|
+
expect(child.get('child')).toBe('data');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('child modifications do not affect parent', () => {
|
|
103
|
+
const parent = new ExpressionContext({ shared: 'original' });
|
|
104
|
+
const child = parent.createChild({});
|
|
105
|
+
child.set('shared', 'modified');
|
|
106
|
+
expect(child.get('shared')).toBe('modified');
|
|
107
|
+
expect(parent.get('shared')).toBe('original');
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
});
|