@halleyassist/rule-templater 0.0.5 → 0.0.6
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/README.md +22 -0
- package/dist/rule-templater.browser.js +33 -0
- package/index.d.ts +6 -0
- package/package.json +1 -1
- package/src/RuleTemplater.js +33 -0
- package/src/RuleTemplater.production.js +33 -0
package/README.md
CHANGED
|
@@ -31,6 +31,11 @@ console.log(variables);
|
|
|
31
31
|
// { name: 'THRESHOLD', filters: [], positions: [{ start: 36, end: 48 }] }
|
|
32
32
|
// ]
|
|
33
33
|
|
|
34
|
+
// Extract function calls from the template
|
|
35
|
+
const functions = parsed.extractFunctions();
|
|
36
|
+
console.log(functions);
|
|
37
|
+
// ['EventIs', 'Value']
|
|
38
|
+
|
|
34
39
|
// Validate that variables are provided correctly
|
|
35
40
|
const validation = parsed.validate({
|
|
36
41
|
EVENT_TYPE: { value: 'sensor-update', type: 'string' },
|
|
@@ -160,6 +165,23 @@ Extracts all variables from the template using the AST.
|
|
|
160
165
|
|
|
161
166
|
Note: If a variable appears multiple times in the template, all occurrences will be recorded in the `positions` array.
|
|
162
167
|
|
|
168
|
+
### `ruleTemplate.extractFunctions()`
|
|
169
|
+
|
|
170
|
+
Extracts all function calls from the template using the AST.
|
|
171
|
+
|
|
172
|
+
**Returns:** Array of unique function names (sorted alphabetically) used in the template.
|
|
173
|
+
|
|
174
|
+
**Example:**
|
|
175
|
+
```javascript
|
|
176
|
+
const template = 'EventIs("test") && Value() > 10 && TimeLastTrueCheck("last_check") < 60';
|
|
177
|
+
const parsed = RuleTemplate.parse(template);
|
|
178
|
+
const functions = parsed.extractFunctions();
|
|
179
|
+
console.log(functions);
|
|
180
|
+
// ['EventIs', 'TimeLastTrueCheck', 'Value']
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
This is useful for comparing against a hub's list of available functions to ensure all functions used in the template are supported.
|
|
184
|
+
|
|
163
185
|
### `ruleTemplate.validate(variables)`
|
|
164
186
|
|
|
165
187
|
Validates that all required variables are provided and have valid types.
|
|
@@ -2165,6 +2165,39 @@ class RuleTemplate {
|
|
|
2165
2165
|
return variables;
|
|
2166
2166
|
}
|
|
2167
2167
|
|
|
2168
|
+
/**
|
|
2169
|
+
* Extract function calls from the template using the AST
|
|
2170
|
+
* @returns {Array} Array of unique function names used in the template
|
|
2171
|
+
*/
|
|
2172
|
+
extractFunctions(){
|
|
2173
|
+
const functions = new Set();
|
|
2174
|
+
|
|
2175
|
+
const traverse = (node) => {
|
|
2176
|
+
if (!node) return;
|
|
2177
|
+
|
|
2178
|
+
// Check if this is a function call node
|
|
2179
|
+
if (node.type === 'fcall') {
|
|
2180
|
+
// Find the function name in children
|
|
2181
|
+
const fnameNode = node.children?.find(c => c.type === 'fname');
|
|
2182
|
+
if (fnameNode && fnameNode.text) {
|
|
2183
|
+
functions.add(fnameNode.text.trim());
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
// Traverse children
|
|
2188
|
+
if (node.children) {
|
|
2189
|
+
for (const child of node.children) {
|
|
2190
|
+
traverse(child);
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2193
|
+
};
|
|
2194
|
+
|
|
2195
|
+
traverse(this.ast);
|
|
2196
|
+
|
|
2197
|
+
// Convert set to sorted array for consistent output
|
|
2198
|
+
return Array.from(functions).sort();
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2168
2201
|
/**
|
|
2169
2202
|
* Extract variable name and filters from a template_value AST node
|
|
2170
2203
|
* @private
|
package/index.d.ts
CHANGED
|
@@ -68,6 +68,12 @@ export default class RuleTemplate {
|
|
|
68
68
|
*/
|
|
69
69
|
extractVariables(): VariableInfo[];
|
|
70
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Extract function calls from the template using the AST
|
|
73
|
+
* @returns Array of unique function names used in the template
|
|
74
|
+
*/
|
|
75
|
+
extractFunctions(): string[];
|
|
76
|
+
|
|
71
77
|
/**
|
|
72
78
|
* Validate variable types against the AST
|
|
73
79
|
* @param variables Object mapping variable names to {value, type} objects
|
package/package.json
CHANGED
package/src/RuleTemplater.js
CHANGED
|
@@ -127,6 +127,39 @@ class RuleTemplate {
|
|
|
127
127
|
return variables;
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Extract function calls from the template using the AST
|
|
132
|
+
* @returns {Array} Array of unique function names used in the template
|
|
133
|
+
*/
|
|
134
|
+
extractFunctions(){
|
|
135
|
+
const functions = new Set();
|
|
136
|
+
|
|
137
|
+
const traverse = (node) => {
|
|
138
|
+
if (!node) return;
|
|
139
|
+
|
|
140
|
+
// Check if this is a function call node
|
|
141
|
+
if (node.type === 'fcall') {
|
|
142
|
+
// Find the function name in children
|
|
143
|
+
const fnameNode = node.children?.find(c => c.type === 'fname');
|
|
144
|
+
if (fnameNode && fnameNode.text) {
|
|
145
|
+
functions.add(fnameNode.text.trim());
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Traverse children
|
|
150
|
+
if (node.children) {
|
|
151
|
+
for (const child of node.children) {
|
|
152
|
+
traverse(child);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
traverse(this.ast);
|
|
158
|
+
|
|
159
|
+
// Convert set to sorted array for consistent output
|
|
160
|
+
return Array.from(functions).sort();
|
|
161
|
+
}
|
|
162
|
+
|
|
130
163
|
/**
|
|
131
164
|
* Extract variable name and filters from a template_value AST node
|
|
132
165
|
* @private
|
|
@@ -127,6 +127,39 @@ class RuleTemplate {
|
|
|
127
127
|
return variables;
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Extract function calls from the template using the AST
|
|
132
|
+
* @returns {Array} Array of unique function names used in the template
|
|
133
|
+
*/
|
|
134
|
+
extractFunctions(){
|
|
135
|
+
const functions = new Set();
|
|
136
|
+
|
|
137
|
+
const traverse = (node) => {
|
|
138
|
+
if (!node) return;
|
|
139
|
+
|
|
140
|
+
// Check if this is a function call node
|
|
141
|
+
if (node.type === 'fcall') {
|
|
142
|
+
// Find the function name in children
|
|
143
|
+
const fnameNode = node.children?.find(c => c.type === 'fname');
|
|
144
|
+
if (fnameNode && fnameNode.text) {
|
|
145
|
+
functions.add(fnameNode.text.trim());
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Traverse children
|
|
150
|
+
if (node.children) {
|
|
151
|
+
for (const child of node.children) {
|
|
152
|
+
traverse(child);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
traverse(this.ast);
|
|
158
|
+
|
|
159
|
+
// Convert set to sorted array for consistent output
|
|
160
|
+
return Array.from(functions).sort();
|
|
161
|
+
}
|
|
162
|
+
|
|
130
163
|
/**
|
|
131
164
|
* Extract variable name and filters from a template_value AST node
|
|
132
165
|
* @private
|