@object-ui/core 0.3.1 → 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 +4 -0
- 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 +2 -1
- package/dist/actions/index.js +2 -1
- 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/ExpressionCache.d.ts +101 -0
- package/dist/evaluator/ExpressionCache.js +135 -0
- package/dist/evaluator/ExpressionEvaluator.d.ts +30 -2
- package/dist/evaluator/ExpressionEvaluator.js +60 -16
- package/dist/evaluator/FormulaFunctions.d.ts +58 -0
- package/dist/evaluator/FormulaFunctions.js +350 -0
- package/dist/evaluator/index.d.ts +4 -2
- package/dist/evaluator/index.js +4 -2
- package/dist/index.d.ts +14 -7
- package/dist/index.js +13 -9
- package/dist/query/index.d.ts +6 -0
- package/dist/query/index.js +6 -0
- package/dist/query/query-ast.d.ts +32 -0
- package/dist/query/query-ast.js +268 -0
- package/dist/registry/PluginScopeImpl.d.ts +80 -0
- package/dist/registry/PluginScopeImpl.js +243 -0
- package/dist/registry/PluginSystem.d.ts +66 -0
- package/dist/registry/PluginSystem.js +142 -0
- package/dist/registry/Registry.d.ts +83 -4
- package/dist/registry/Registry.js +113 -7
- 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 +9 -0
- package/dist/validation/index.js +9 -0
- package/dist/validation/validation-engine.d.ts +88 -0
- package/dist/validation/validation-engine.js +428 -0
- package/dist/validation/validators/index.d.ts +16 -0
- package/dist/validation/validators/index.js +16 -0
- package/dist/validation/validators/object-validation-engine.d.ts +118 -0
- package/dist/validation/validators/object-validation-engine.js +538 -0
- package/package.json +14 -5
- 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 +2 -1
- 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/ExpressionCache.ts +192 -0
- package/src/evaluator/ExpressionEvaluator.ts +61 -16
- package/src/evaluator/FormulaFunctions.ts +398 -0
- package/src/evaluator/__tests__/ExpressionCache.test.ts +135 -0
- package/src/evaluator/__tests__/ExpressionContext.test.ts +110 -0
- package/src/evaluator/__tests__/FormulaFunctions.test.ts +447 -0
- package/src/evaluator/index.ts +4 -2
- package/src/index.ts +14 -10
- package/src/query/__tests__/query-ast.test.ts +211 -0
- package/src/query/__tests__/window-functions.test.ts +275 -0
- package/src/query/index.ts +7 -0
- package/src/query/query-ast.ts +341 -0
- package/src/registry/PluginScopeImpl.ts +259 -0
- package/src/registry/PluginSystem.ts +161 -0
- package/src/registry/Registry.ts +136 -8
- package/src/registry/WidgetRegistry.ts +316 -0
- package/src/registry/__tests__/PluginSystem.test.ts +226 -0
- package/src/registry/__tests__/Registry.test.ts +293 -0
- package/src/registry/__tests__/WidgetRegistry.test.ts +321 -0
- package/src/registry/__tests__/plugin-scope-integration.test.ts +283 -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 +567 -0
- package/src/validation/__tests__/schema-validator.test.ts +118 -0
- package/src/validation/__tests__/validation-engine.test.ts +102 -0
- package/src/validation/index.ts +10 -0
- package/src/validation/validation-engine.ts +520 -0
- package/src/validation/validators/index.ts +25 -0
- package/src/validation/validators/object-validation-engine.ts +722 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +2 -0
- package/src/adapters/index.d.ts +0 -8
- package/src/adapters/index.js +0 -10
- package/src/builder/schema-builder.d.ts +0 -294
- package/src/builder/schema-builder.js +0 -503
- package/src/index.d.ts +0 -13
- package/src/index.js +0 -16
- package/src/registry/Registry.d.ts +0 -56
- package/src/registry/Registry.js +0 -43
- package/src/types/index.d.ts +0 -19
- package/src/types/index.js +0 -8
- package/src/utils/filter-converter.d.ts +0 -57
- package/src/utils/filter-converter.js +0 -100
- package/src/validation/schema-validator.d.ts +0 -94
- package/src/validation/schema-validator.js +0 -278
|
@@ -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,135 @@
|
|
|
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
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
10
|
+
import { ExpressionCache } from '../ExpressionCache';
|
|
11
|
+
|
|
12
|
+
describe('ExpressionCache', () => {
|
|
13
|
+
let cache: ExpressionCache;
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
cache = new ExpressionCache();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should compile and cache an expression', () => {
|
|
20
|
+
const expr = 'data.amount > 1000';
|
|
21
|
+
const varNames = ['data'];
|
|
22
|
+
|
|
23
|
+
const compiled = cache.compile(expr, varNames);
|
|
24
|
+
|
|
25
|
+
expect(compiled).toBeDefined();
|
|
26
|
+
expect(compiled.expression).toBe(expr);
|
|
27
|
+
expect(compiled.varNames).toEqual(varNames);
|
|
28
|
+
expect(compiled.hitCount).toBe(1);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should return cached expression on second call', () => {
|
|
32
|
+
const expr = 'data.amount > 1000';
|
|
33
|
+
const varNames = ['data'];
|
|
34
|
+
|
|
35
|
+
const first = cache.compile(expr, varNames);
|
|
36
|
+
const second = cache.compile(expr, varNames);
|
|
37
|
+
|
|
38
|
+
expect(first).toBe(second);
|
|
39
|
+
expect(second.hitCount).toBe(2);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should execute compiled expression correctly', () => {
|
|
43
|
+
const expr = 'data.amount > 1000';
|
|
44
|
+
const varNames = ['data'];
|
|
45
|
+
|
|
46
|
+
const compiled = cache.compile(expr, varNames);
|
|
47
|
+
const result = compiled.fn({ amount: 1500 });
|
|
48
|
+
|
|
49
|
+
expect(result).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should handle multiple expressions', () => {
|
|
53
|
+
const expr1 = 'data.amount > 1000';
|
|
54
|
+
const expr2 = 'data.name === "John"';
|
|
55
|
+
const varNames = ['data'];
|
|
56
|
+
|
|
57
|
+
const compiled1 = cache.compile(expr1, varNames);
|
|
58
|
+
const compiled2 = cache.compile(expr2, varNames);
|
|
59
|
+
|
|
60
|
+
expect(compiled1).not.toBe(compiled2);
|
|
61
|
+
expect(compiled1.fn({ amount: 1500 })).toBe(true);
|
|
62
|
+
expect(compiled2.fn({ name: 'John' })).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should differentiate between different variable contexts', () => {
|
|
66
|
+
const expr = 'x + y';
|
|
67
|
+
|
|
68
|
+
const compiled1 = cache.compile(expr, ['x', 'y']);
|
|
69
|
+
const compiled2 = cache.compile(expr, ['x', 'y', 'z']);
|
|
70
|
+
|
|
71
|
+
// Different variable contexts should create different cache entries
|
|
72
|
+
expect(compiled1).not.toBe(compiled2);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should provide cache statistics', () => {
|
|
76
|
+
cache.compile('data.amount > 1000', ['data']);
|
|
77
|
+
cache.compile('data.amount > 1000', ['data']); // Second call, increment hit
|
|
78
|
+
cache.compile('data.name === "John"', ['data']);
|
|
79
|
+
|
|
80
|
+
const stats = cache.getStats();
|
|
81
|
+
|
|
82
|
+
expect(stats.size).toBe(2);
|
|
83
|
+
expect(stats.totalHits).toBe(3);
|
|
84
|
+
expect(stats.entries).toHaveLength(2);
|
|
85
|
+
expect(stats.entries[0].hitCount).toBe(2); // Most frequently used
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should evict LRU when cache is full', () => {
|
|
89
|
+
const smallCache = new ExpressionCache(3);
|
|
90
|
+
|
|
91
|
+
smallCache.compile('expr1', ['x']);
|
|
92
|
+
smallCache.compile('expr2', ['x']);
|
|
93
|
+
smallCache.compile('expr3', ['x']);
|
|
94
|
+
|
|
95
|
+
// Access expr1 multiple times to increase hit count
|
|
96
|
+
smallCache.compile('expr1', ['x']);
|
|
97
|
+
smallCache.compile('expr1', ['x']);
|
|
98
|
+
|
|
99
|
+
// Add a 4th expression, should evict least used (expr2 or expr3)
|
|
100
|
+
smallCache.compile('expr4', ['x']);
|
|
101
|
+
|
|
102
|
+
const stats = smallCache.getStats();
|
|
103
|
+
expect(stats.size).toBe(3);
|
|
104
|
+
|
|
105
|
+
// expr1 should still be cached (highest hit count)
|
|
106
|
+
expect(smallCache.has('expr1', ['x'])).toBe(true);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should clear cache', () => {
|
|
110
|
+
cache.compile('data.amount > 1000', ['data']);
|
|
111
|
+
cache.compile('data.name === "John"', ['data']);
|
|
112
|
+
|
|
113
|
+
expect(cache.getStats().size).toBe(2);
|
|
114
|
+
|
|
115
|
+
cache.clear();
|
|
116
|
+
|
|
117
|
+
expect(cache.getStats().size).toBe(0);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should handle complex expressions', () => {
|
|
121
|
+
const expr = 'data.items.filter(item => item.price > 100).length';
|
|
122
|
+
const varNames = ['data'];
|
|
123
|
+
|
|
124
|
+
const compiled = cache.compile(expr, varNames);
|
|
125
|
+
const result = compiled.fn({
|
|
126
|
+
items: [
|
|
127
|
+
{ price: 50 },
|
|
128
|
+
{ price: 150 },
|
|
129
|
+
{ price: 200 },
|
|
130
|
+
],
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
expect(result).toBe(2);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
@@ -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
|
+
});
|