@halleyassist/rule-templater 0.0.13 → 0.0.14
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
CHANGED
|
@@ -321,6 +321,75 @@ The following variable types are supported:
|
|
|
321
321
|
- `boolean array`
|
|
322
322
|
- `object array`
|
|
323
323
|
|
|
324
|
+
### Time Type Formats
|
|
325
|
+
|
|
326
|
+
The time-related variable types use the following structures when passed to `prepare()` or `validate()`:
|
|
327
|
+
|
|
328
|
+
#### `time value`
|
|
329
|
+
|
|
330
|
+
Use a time-of-day string such as `08:00`.
|
|
331
|
+
|
|
332
|
+
```javascript
|
|
333
|
+
{
|
|
334
|
+
value: '08:00',
|
|
335
|
+
type: 'time value'
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
This is useful when a rule expects a single time value, for example:
|
|
340
|
+
|
|
341
|
+
```javascript
|
|
342
|
+
const prepared = parsed.prepare({
|
|
343
|
+
START_TIME: { value: '08:00', type: 'time value' }
|
|
344
|
+
});
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
#### `time period`
|
|
348
|
+
|
|
349
|
+
Use an object with `from` and `to` properties, both using the same time-of-day string format as `time value`.
|
|
350
|
+
|
|
351
|
+
```javascript
|
|
352
|
+
{
|
|
353
|
+
value: {
|
|
354
|
+
from: '08:00',
|
|
355
|
+
to: '12:00'
|
|
356
|
+
},
|
|
357
|
+
type: 'time period'
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
When rendered into a rule template, this becomes:
|
|
362
|
+
|
|
363
|
+
```text
|
|
364
|
+
08:00 TO 12:00
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
#### `time period ago`
|
|
368
|
+
|
|
369
|
+
Use the same structure as `time period`, plus an `ago` tuple containing:
|
|
370
|
+
|
|
371
|
+
- the numeric offset
|
|
372
|
+
- the rule time unit token, for example `HOURS`
|
|
373
|
+
|
|
374
|
+
```javascript
|
|
375
|
+
{
|
|
376
|
+
value: {
|
|
377
|
+
from: '08:00',
|
|
378
|
+
to: '12:00',
|
|
379
|
+
ago: [2, 'HOURS']
|
|
380
|
+
},
|
|
381
|
+
type: 'time period ago'
|
|
382
|
+
}
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
When rendered into a rule template, this becomes:
|
|
386
|
+
|
|
387
|
+
```text
|
|
388
|
+
08:00 TO 12:00 AGO 2 HOURS
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
The `time_start` and `time_end` filters can extract `from` and `to` from either `time period` or `time period ago` and convert them to `time value`.
|
|
392
|
+
|
|
324
393
|
## License
|
|
325
394
|
|
|
326
395
|
ISC
|
|
@@ -3665,6 +3665,7 @@ const VariableTypes = [
|
|
|
3665
3665
|
'time period',
|
|
3666
3666
|
'time period ago',
|
|
3667
3667
|
'time value',
|
|
3668
|
+
'number time',
|
|
3668
3669
|
'string array',
|
|
3669
3670
|
'number array',
|
|
3670
3671
|
'boolean array',
|
|
@@ -3678,6 +3679,7 @@ const AllowedTypeMapping = {
|
|
|
3678
3679
|
'time period': ['time_period_atom'],
|
|
3679
3680
|
'time period ago': ['time_period_atom'],
|
|
3680
3681
|
'time value': ['time_value_atom', 'tod_atom'],
|
|
3682
|
+
'number time': ['number_atom'],
|
|
3681
3683
|
'string array': ['string_array'],
|
|
3682
3684
|
'number array': ['number_array'],
|
|
3683
3685
|
'boolean array': ['boolean_array'],
|
|
@@ -4129,7 +4131,9 @@ const TemplateFilters = {
|
|
|
4129
4131
|
number: varData => {
|
|
4130
4132
|
varData.value = Number(varData.value);
|
|
4131
4133
|
varData.type = 'number';
|
|
4132
|
-
|
|
4134
|
+
if(isNaN(varData.value)){
|
|
4135
|
+
throw new Error(`Value "${varData.value}" cannot be converted to a number`);
|
|
4136
|
+
}
|
|
4133
4137
|
},
|
|
4134
4138
|
|
|
4135
4139
|
// Convert to boolean
|
package/package.json
CHANGED
package/src/RuleTemplate.js
CHANGED
|
@@ -15,6 +15,7 @@ const VariableTypes = [
|
|
|
15
15
|
'time period',
|
|
16
16
|
'time period ago',
|
|
17
17
|
'time value',
|
|
18
|
+
'number time',
|
|
18
19
|
'string array',
|
|
19
20
|
'number array',
|
|
20
21
|
'boolean array',
|
|
@@ -28,6 +29,7 @@ const AllowedTypeMapping = {
|
|
|
28
29
|
'time period': ['time_period_atom'],
|
|
29
30
|
'time period ago': ['time_period_atom'],
|
|
30
31
|
'time value': ['time_value_atom', 'tod_atom'],
|
|
32
|
+
'number time': ['number_atom'],
|
|
31
33
|
'string array': ['string_array'],
|
|
32
34
|
'number array': ['number_array'],
|
|
33
35
|
'boolean array': ['boolean_array'],
|
|
@@ -15,6 +15,7 @@ const VariableTypes = [
|
|
|
15
15
|
'time period',
|
|
16
16
|
'time period ago',
|
|
17
17
|
'time value',
|
|
18
|
+
'number time',
|
|
18
19
|
'string array',
|
|
19
20
|
'number array',
|
|
20
21
|
'boolean array',
|
|
@@ -28,6 +29,7 @@ const AllowedTypeMapping = {
|
|
|
28
29
|
'time period': ['time_period_atom'],
|
|
29
30
|
'time period ago': ['time_period_atom'],
|
|
30
31
|
'time value': ['time_value_atom', 'tod_atom'],
|
|
32
|
+
'number time': ['number_atom'],
|
|
31
33
|
'string array': ['string_array'],
|
|
32
34
|
'number array': ['number_array'],
|
|
33
35
|
'boolean array': ['boolean_array'],
|
package/src/TemplateFilters.js
CHANGED
|
@@ -52,7 +52,9 @@ const TemplateFilters = {
|
|
|
52
52
|
number: varData => {
|
|
53
53
|
varData.value = Number(varData.value);
|
|
54
54
|
varData.type = 'number';
|
|
55
|
-
|
|
55
|
+
if(isNaN(varData.value)){
|
|
56
|
+
throw new Error(`Value "${varData.value}" cannot be converted to a number`);
|
|
57
|
+
}
|
|
56
58
|
},
|
|
57
59
|
|
|
58
60
|
// Convert to boolean
|