@a2ui-sdk/utils 0.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.
@@ -0,0 +1 @@
1
+ export { interpolate } from './interpolation/index.js';
@@ -0,0 +1 @@
1
+ export { interpolate } from './interpolation/index.js';
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Evaluator for interpolation AST nodes.
3
+ *
4
+ * Traverses the AST and resolves values from the data model,
5
+ * producing the final interpolated string.
6
+ */
7
+ import type { ASTNode, EvaluationContext } from './types.js';
8
+ /**
9
+ * Evaluates an AST node and returns the result as a string.
10
+ *
11
+ * @param ast - The AST node to evaluate
12
+ * @param context - The evaluation context with data model and functions
13
+ * @returns The evaluated string result
14
+ */
15
+ export declare function evaluate(ast: ASTNode, context: EvaluationContext): string;
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Evaluator for interpolation AST nodes.
3
+ *
4
+ * Traverses the AST and resolves values from the data model,
5
+ * producing the final interpolated string.
6
+ */
7
+ import { getValueByPath, resolvePath } from '../pathUtils.js';
8
+ /**
9
+ * Evaluates an AST node and returns the result as a string.
10
+ *
11
+ * @param ast - The AST node to evaluate
12
+ * @param context - The evaluation context with data model and functions
13
+ * @returns The evaluated string result
14
+ */
15
+ export function evaluate(ast, context) {
16
+ const value = evaluateNode(ast, context);
17
+ return stringifyValue(value);
18
+ }
19
+ /**
20
+ * Evaluates an AST node and returns the raw value.
21
+ */
22
+ function evaluateNode(node, context) {
23
+ switch (node.type) {
24
+ case 'literal':
25
+ return node.value;
26
+ case 'path':
27
+ return evaluatePath(node.path, node.absolute, context);
28
+ case 'functionCall':
29
+ return evaluateFunctionCall(node.name, node.args, context);
30
+ case 'interpolatedString':
31
+ return evaluateInterpolatedString(node, context);
32
+ default:
33
+ return '';
34
+ }
35
+ }
36
+ /**
37
+ * Evaluates a path and resolves it against the data model.
38
+ * Note: JSON Pointer escape sequences (~0, ~1) are decoded by getValueByPath
39
+ * via parseJsonPointer in pathUtils.ts.
40
+ */
41
+ function evaluatePath(path, absolute, context) {
42
+ // Resolve relative paths
43
+ let resolvedPath;
44
+ if (absolute) {
45
+ resolvedPath = path;
46
+ }
47
+ else {
48
+ resolvedPath = resolvePath(path, context.basePath);
49
+ }
50
+ return getValueByPath(context.dataModel, resolvedPath);
51
+ }
52
+ /**
53
+ * Evaluates a function call with its arguments.
54
+ */
55
+ function evaluateFunctionCall(name, args, context) {
56
+ // Look up function in custom registry
57
+ const fn = context.functions?.[name];
58
+ if (!fn) {
59
+ console.warn(`[A2UI] Unknown function: ${name}`);
60
+ return '';
61
+ }
62
+ // Evaluate all arguments
63
+ const evaluatedArgs = args.map((arg) => evaluateNode(arg, context));
64
+ // Call the function
65
+ try {
66
+ return fn(...evaluatedArgs);
67
+ }
68
+ catch (error) {
69
+ console.warn(`[A2UI] Function error in ${name}:`, error);
70
+ return '';
71
+ }
72
+ }
73
+ /**
74
+ * Evaluates an interpolated string by concatenating all parts.
75
+ */
76
+ function evaluateInterpolatedString(node, context) {
77
+ return node.parts
78
+ .map((part) => stringifyValue(evaluateNode(part, context)))
79
+ .join('');
80
+ }
81
+ /**
82
+ * Converts a value to string for output.
83
+ */
84
+ function stringifyValue(value) {
85
+ if (value === undefined || value === null) {
86
+ return '';
87
+ }
88
+ if (typeof value === 'string') {
89
+ return value;
90
+ }
91
+ if (typeof value === 'object') {
92
+ return JSON.stringify(value);
93
+ }
94
+ return String(value);
95
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Tests for the evaluator.
3
+ */
4
+ export {};