@halleyassist/rule-templater 0.0.15 → 0.0.16
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/rule-templater.browser.js +56 -2
- package/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/VariableValidate.js +55 -1
|
@@ -4311,6 +4311,7 @@ module.exports = TemplateFilters;
|
|
|
4311
4311
|
},{}],19:[function(require,module,exports){
|
|
4312
4312
|
const { Parser } = require('ebnf');
|
|
4313
4313
|
const TemplateGrammar = require('./RuleTemplate.ebnf');
|
|
4314
|
+
const TemplateFilters = require('./TemplateFilters');
|
|
4314
4315
|
const RuleParser = require('@halleyassist/rule-parser');
|
|
4315
4316
|
const RuleParserRules = RuleParser.ParserRules;
|
|
4316
4317
|
|
|
@@ -4357,7 +4358,17 @@ class VariableValidate {
|
|
|
4357
4358
|
};
|
|
4358
4359
|
}
|
|
4359
4360
|
|
|
4360
|
-
|
|
4361
|
+
let normalizedVarData;
|
|
4362
|
+
try {
|
|
4363
|
+
normalizedVarData = VariableValidate._normalizeVarData(variableData);
|
|
4364
|
+
} catch (error) {
|
|
4365
|
+
return {
|
|
4366
|
+
valid: false,
|
|
4367
|
+
error: error.message
|
|
4368
|
+
};
|
|
4369
|
+
}
|
|
4370
|
+
|
|
4371
|
+
return VariableValidate.validateValue(normalizedVarData.type, normalizedVarData.value);
|
|
4361
4372
|
}
|
|
4362
4373
|
|
|
4363
4374
|
static validateValue(type, value) {
|
|
@@ -4424,6 +4435,49 @@ class VariableValidate {
|
|
|
4424
4435
|
return ParserCache;
|
|
4425
4436
|
}
|
|
4426
4437
|
|
|
4438
|
+
static _normalizeVarData(variableData) {
|
|
4439
|
+
const normalizedVarData = VariableValidate._cloneVarData(variableData);
|
|
4440
|
+
|
|
4441
|
+
if (!Object.prototype.hasOwnProperty.call(normalizedVarData, 'value')) {
|
|
4442
|
+
throw new Error('Variable data must include a value property');
|
|
4443
|
+
}
|
|
4444
|
+
|
|
4445
|
+
if (!Object.prototype.hasOwnProperty.call(normalizedVarData, 'filters')) {
|
|
4446
|
+
return normalizedVarData;
|
|
4447
|
+
}
|
|
4448
|
+
|
|
4449
|
+
if (!Array.isArray(normalizedVarData.filters)) {
|
|
4450
|
+
throw new Error('Variable data filters must be an array');
|
|
4451
|
+
}
|
|
4452
|
+
|
|
4453
|
+
for (const filterName of normalizedVarData.filters) {
|
|
4454
|
+
if (!TemplateFilters[filterName]) {
|
|
4455
|
+
throw new Error(`Unknown filter '${filterName}'`);
|
|
4456
|
+
}
|
|
4457
|
+
|
|
4458
|
+
TemplateFilters[filterName](normalizedVarData);
|
|
4459
|
+
}
|
|
4460
|
+
|
|
4461
|
+
return normalizedVarData;
|
|
4462
|
+
}
|
|
4463
|
+
|
|
4464
|
+
static _cloneVarData(variableData) {
|
|
4465
|
+
const cloned = Object.assign({}, variableData);
|
|
4466
|
+
if (Array.isArray(cloned.filters)) {
|
|
4467
|
+
cloned.filters = cloned.filters.slice();
|
|
4468
|
+
}
|
|
4469
|
+
|
|
4470
|
+
if (cloned.value && typeof cloned.value === 'object') {
|
|
4471
|
+
if (Array.isArray(cloned.value)) {
|
|
4472
|
+
cloned.value = cloned.value.slice();
|
|
4473
|
+
} else {
|
|
4474
|
+
cloned.value = Object.assign({}, cloned.value);
|
|
4475
|
+
}
|
|
4476
|
+
}
|
|
4477
|
+
|
|
4478
|
+
return cloned;
|
|
4479
|
+
}
|
|
4480
|
+
|
|
4427
4481
|
static _serializeString(value) {
|
|
4428
4482
|
if (typeof value !== 'string') {
|
|
4429
4483
|
return null;
|
|
@@ -4654,5 +4708,5 @@ VariableValidate.validators = Object.freeze({
|
|
|
4654
4708
|
});
|
|
4655
4709
|
|
|
4656
4710
|
module.exports = VariableValidate;
|
|
4657
|
-
},{"./RuleTemplate.ebnf":15,"@halleyassist/rule-parser":2,"ebnf":13}]},{},[14])(14)
|
|
4711
|
+
},{"./RuleTemplate.ebnf":15,"./TemplateFilters":18,"@halleyassist/rule-parser":2,"ebnf":13}]},{},[14])(14)
|
|
4658
4712
|
});
|
package/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export interface VariableValue {
|
|
|
15
15
|
to: string;
|
|
16
16
|
ago?: [number, string];
|
|
17
17
|
} | Record<string, any> | string[] | number[] | boolean[] | Record<string, any>[];
|
|
18
|
+
filters?: string[];
|
|
18
19
|
type?: 'string' | 'number' | 'boolean' | 'object' | 'time period' | 'time period ago' | 'time value' | 'number time' | 'string array' | 'number array' | 'boolean array' | 'object array';
|
|
19
20
|
}
|
|
20
21
|
|
package/package.json
CHANGED
package/src/VariableValidate.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const { Parser } = require('ebnf');
|
|
2
2
|
const TemplateGrammar = require('./RuleTemplate.ebnf');
|
|
3
|
+
const TemplateFilters = require('./TemplateFilters');
|
|
3
4
|
const RuleParser = require('@halleyassist/rule-parser');
|
|
4
5
|
const RuleParserRules = RuleParser.ParserRules;
|
|
5
6
|
|
|
@@ -46,7 +47,17 @@ class VariableValidate {
|
|
|
46
47
|
};
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
|
|
50
|
+
let normalizedVarData;
|
|
51
|
+
try {
|
|
52
|
+
normalizedVarData = VariableValidate._normalizeVarData(variableData);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return {
|
|
55
|
+
valid: false,
|
|
56
|
+
error: error.message
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return VariableValidate.validateValue(normalizedVarData.type, normalizedVarData.value);
|
|
50
61
|
}
|
|
51
62
|
|
|
52
63
|
static validateValue(type, value) {
|
|
@@ -113,6 +124,49 @@ class VariableValidate {
|
|
|
113
124
|
return ParserCache;
|
|
114
125
|
}
|
|
115
126
|
|
|
127
|
+
static _normalizeVarData(variableData) {
|
|
128
|
+
const normalizedVarData = VariableValidate._cloneVarData(variableData);
|
|
129
|
+
|
|
130
|
+
if (!Object.prototype.hasOwnProperty.call(normalizedVarData, 'value')) {
|
|
131
|
+
throw new Error('Variable data must include a value property');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (!Object.prototype.hasOwnProperty.call(normalizedVarData, 'filters')) {
|
|
135
|
+
return normalizedVarData;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (!Array.isArray(normalizedVarData.filters)) {
|
|
139
|
+
throw new Error('Variable data filters must be an array');
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
for (const filterName of normalizedVarData.filters) {
|
|
143
|
+
if (!TemplateFilters[filterName]) {
|
|
144
|
+
throw new Error(`Unknown filter '${filterName}'`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
TemplateFilters[filterName](normalizedVarData);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return normalizedVarData;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static _cloneVarData(variableData) {
|
|
154
|
+
const cloned = Object.assign({}, variableData);
|
|
155
|
+
if (Array.isArray(cloned.filters)) {
|
|
156
|
+
cloned.filters = cloned.filters.slice();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (cloned.value && typeof cloned.value === 'object') {
|
|
160
|
+
if (Array.isArray(cloned.value)) {
|
|
161
|
+
cloned.value = cloned.value.slice();
|
|
162
|
+
} else {
|
|
163
|
+
cloned.value = Object.assign({}, cloned.value);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return cloned;
|
|
168
|
+
}
|
|
169
|
+
|
|
116
170
|
static _serializeString(value) {
|
|
117
171
|
if (typeof value !== 'string') {
|
|
118
172
|
return null;
|