@wmfs/asl-choice-processor 1.26.0 → 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/.circleci/config.yml +7 -0
- package/CHANGELOG.md +19 -0
- package/README.md +115 -25
- package/lib/Input-value-cache.js +29 -24
- package/lib/compile.js +151 -0
- package/lib/index.js +24 -32
- package/lib/operators/comparisons.js +51 -0
- package/lib/operators/extensions.js +12 -0
- package/lib/operators/guards.js +24 -0
- package/lib/operators/index.js +19 -22
- package/lib/operators/type-tests.js +19 -0
- package/lib/string-matches.js +26 -0
- package/lib/timestamp.js +52 -0
- package/package.json +1 -1
- package/test/data-test-expr-tests.js +3 -4
- package/test/spec-compliance-tests.js +343 -0
- package/lib/execute-operator.js +0 -20
- package/lib/find-operator.js +0 -27
- package/lib/operators/boolean-equals.js +0 -9
- package/lib/operators/includes.js +0 -9
- package/lib/operators/is-boolean.js +0 -11
- package/lib/operators/is-null.js +0 -13
- package/lib/operators/is-present.js +0 -6
- package/lib/operators/is-string.js +0 -13
- package/lib/operators/is-undefined.js +0 -13
- package/lib/operators/numeric-equals.js +0 -9
- package/lib/operators/numeric-greater-than-equals.js +0 -9
- package/lib/operators/numeric-greater-than.js +0 -9
- package/lib/operators/numeric-less-than-equals.js +0 -9
- package/lib/operators/numeric-less-than.js +0 -9
- package/lib/operators/string-equals.js +0 -9
- package/lib/operators/string-greater-than-equals.js +0 -9
- package/lib/operators/string-greater-than.js +0 -9
- package/lib/operators/string-less-than-equals.js +0 -9
- package/lib/operators/string-less-than.js +0 -9
- package/lib/operators/timestamp-equals.js +0 -9
- package/lib/operators/timestamp-greater-than-equals.js +0 -9
- package/lib/operators/timestamp-greater-than.js +0 -9
- package/lib/operators/timestamp-less-than-equals.js +0 -9
- package/lib/operators/timestamp-less-than.js +0 -9
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
// StringMatches patterns: "*" matches zero or more characters. "\*" is a
|
|
4
|
+
// literal asterisk and "\\" a literal backslash (written "\\*" and "\\\\" in
|
|
5
|
+
// JSON text). Everything else matches itself.
|
|
6
|
+
// See https://states-language.net/spec.html#choice-state
|
|
7
|
+
function compilePattern (pattern) {
|
|
8
|
+
let source = ''
|
|
9
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
10
|
+
const ch = pattern[i]
|
|
11
|
+
if (ch === '\\' && (pattern[i + 1] === '*' || pattern[i + 1] === '\\')) {
|
|
12
|
+
source += escapeRegExp(pattern[++i])
|
|
13
|
+
} else if (ch === '*') {
|
|
14
|
+
source += '[\\s\\S]*'
|
|
15
|
+
} else {
|
|
16
|
+
source += escapeRegExp(ch)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return new RegExp(`^${source}$`, 'u')
|
|
20
|
+
} // compilePattern
|
|
21
|
+
|
|
22
|
+
function escapeRegExp (s) {
|
|
23
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = compilePattern
|
package/lib/timestamp.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
// Timestamps in the States Language are RFC3339 strings, with an uppercase
|
|
4
|
+
// "T" between date and time and an uppercase "Z" when there's no numeric
|
|
5
|
+
// offset, e.g. "2016-03-14T01:59:00Z" or "2016-03-14T02:59:00.25+01:00".
|
|
6
|
+
// See https://states-language.net/spec.html#timestamps
|
|
7
|
+
const RFC3339 = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?(?:Z|([+-])(\d{2}):(\d{2}))$/
|
|
8
|
+
|
|
9
|
+
// Parses a timestamp into { seconds, fraction } where seconds is whole
|
|
10
|
+
// seconds since the epoch (UTC) and fraction is the fractional-second digits,
|
|
11
|
+
// kept as a string so precision beyond milliseconds isn't lost.
|
|
12
|
+
// Returns null if the value isn't a valid timestamp.
|
|
13
|
+
function parseTimestamp (value) {
|
|
14
|
+
if (typeof value !== 'string') return null
|
|
15
|
+
|
|
16
|
+
const m = RFC3339.exec(value)
|
|
17
|
+
if (!m) return null
|
|
18
|
+
|
|
19
|
+
const [year, month, day, hour, minute, second] = m.slice(1, 7).map(Number)
|
|
20
|
+
const fraction = (m[7] || '').replace(/0+$/, '')
|
|
21
|
+
const sign = m[8] === '-' ? -1 : 1
|
|
22
|
+
const offsetHours = m[9] ? Number(m[9]) : 0
|
|
23
|
+
const offsetMinutes = m[10] ? Number(m[10]) : 0
|
|
24
|
+
|
|
25
|
+
if (month < 1 || month > 12) return null
|
|
26
|
+
if (hour > 23 || minute > 59 || second > 59) return null
|
|
27
|
+
if (offsetHours > 23 || offsetMinutes > 59) return null
|
|
28
|
+
|
|
29
|
+
const date = new Date(0)
|
|
30
|
+
date.setUTCFullYear(year, month - 1, day)
|
|
31
|
+
date.setUTCHours(hour, minute, second, 0)
|
|
32
|
+
// Reject dates that rolled over, e.g. 2021-02-30
|
|
33
|
+
if (date.getUTCFullYear() !== year || date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day) return null
|
|
34
|
+
|
|
35
|
+
const offsetSeconds = sign * (offsetHours * 3600 + offsetMinutes * 60)
|
|
36
|
+
return {
|
|
37
|
+
seconds: date.getTime() / 1000 - offsetSeconds,
|
|
38
|
+
fraction
|
|
39
|
+
}
|
|
40
|
+
} // parseTimestamp
|
|
41
|
+
|
|
42
|
+
// Compares two parsed timestamps: negative, zero or positive
|
|
43
|
+
function compareTimestamps (a, b) {
|
|
44
|
+
if (a.seconds !== b.seconds) return a.seconds < b.seconds ? -1 : 1
|
|
45
|
+
const length = Math.max(a.fraction.length, b.fraction.length)
|
|
46
|
+
const fa = a.fraction.padEnd(length, '0')
|
|
47
|
+
const fb = b.fraction.padEnd(length, '0')
|
|
48
|
+
if (fa === fb) return 0
|
|
49
|
+
return fa < fb ? -1 : 1
|
|
50
|
+
} // compareTimestamps
|
|
51
|
+
|
|
52
|
+
module.exports = { parseTimestamp, compareTimestamps }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wmfs/asl-choice-processor",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "For determining the next state given an Amazon States Language 'Choices' definition and a set of values.",
|
|
5
5
|
"author": "West Midlands Fire Service",
|
|
6
6
|
"homepage": "https://github.com/wmfs/asl-choice-processor#readme",
|
|
@@ -57,12 +57,11 @@ const tests = {
|
|
|
57
57
|
],
|
|
58
58
|
StringEquals: [
|
|
59
59
|
['Hello World', 'Hello World', 'NextState'],
|
|
60
|
-
[null, null, 'NextState'],
|
|
61
|
-
[undefined, undefined, 'NextState'],
|
|
62
|
-
[1, 1, 'NextState'],
|
|
63
60
|
|
|
64
61
|
['Hello World', 'Hello WORLD', 'DefaultState'],
|
|
65
|
-
[
|
|
62
|
+
[1, '1', 'DefaultState'],
|
|
63
|
+
[null, 'null', 'DefaultState']
|
|
64
|
+
// Non-string comparison values are rejected - see spec-compliance-tests.js
|
|
66
65
|
],
|
|
67
66
|
IsNull: [
|
|
68
67
|
[null, true, 'NextState'],
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
'use strict'
|
|
4
|
+
|
|
5
|
+
// Choice state behaviour from https://states-language.net/spec.html#choice-state
|
|
6
|
+
|
|
7
|
+
const chai = require('chai')
|
|
8
|
+
const expect = chai.expect
|
|
9
|
+
|
|
10
|
+
const choiceProcessor = require('./../lib')
|
|
11
|
+
const { ChoiceDefinitionError } = choiceProcessor
|
|
12
|
+
|
|
13
|
+
function nextState (rule, input, context) {
|
|
14
|
+
return choiceProcessor({
|
|
15
|
+
Choices: [{ ...rule, Next: 'Match' }],
|
|
16
|
+
Default: 'Default'
|
|
17
|
+
})(input, context)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function expectMatches (rule, cases) {
|
|
21
|
+
for (const [input, expected] of cases) {
|
|
22
|
+
it(`${JSON.stringify(rule)} with ${JSON.stringify(input)} -> ${expected}`, () => {
|
|
23
|
+
expect(nextState(rule, input)).to.equal(expected)
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('Spec compliance', () => {
|
|
29
|
+
describe('IsNumeric', () => {
|
|
30
|
+
expectMatches({ Variable: '$.x', IsNumeric: true }, [
|
|
31
|
+
[{ x: 1 }, 'Match'],
|
|
32
|
+
[{ x: -1.5 }, 'Match'],
|
|
33
|
+
[{ x: 0 }, 'Match'],
|
|
34
|
+
[{ x: '1' }, 'Default'],
|
|
35
|
+
[{ x: null }, 'Default'],
|
|
36
|
+
[{ x: true }, 'Default'],
|
|
37
|
+
[{}, 'Default']
|
|
38
|
+
])
|
|
39
|
+
expectMatches({ Variable: '$.x', IsNumeric: false }, [
|
|
40
|
+
[{ x: 1 }, 'Default'],
|
|
41
|
+
[{ x: '1' }, 'Match'],
|
|
42
|
+
[{}, 'Match']
|
|
43
|
+
])
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
describe('IsTimestamp', () => {
|
|
47
|
+
expectMatches({ Variable: '$.x', IsTimestamp: true }, [
|
|
48
|
+
[{ x: '2016-03-14T01:59:00Z' }, 'Match'],
|
|
49
|
+
[{ x: '2016-03-14T01:59:00.123456789Z' }, 'Match'],
|
|
50
|
+
[{ x: '2016-03-14T01:59:00+05:30' }, 'Match'],
|
|
51
|
+
[{ x: '2016-03-14T01:59:00-08:00' }, 'Match'],
|
|
52
|
+
[{ x: '2024-02-29T00:00:00Z' }, 'Match'],
|
|
53
|
+
[{ x: '2016-03-14t01:59:00Z' }, 'Default'], // lowercase t
|
|
54
|
+
[{ x: '2016-03-14T01:59:00z' }, 'Default'], // lowercase z
|
|
55
|
+
[{ x: '2016-03-14T01:59:00' }, 'Default'], // no offset
|
|
56
|
+
[{ x: '2016-03-14 01:59:00Z' }, 'Default'], // space separator
|
|
57
|
+
[{ x: '2016-03-14' }, 'Default'],
|
|
58
|
+
[{ x: '2023-02-29T00:00:00Z' }, 'Default'], // not a leap year
|
|
59
|
+
[{ x: '2016-13-01T00:00:00Z' }, 'Default'],
|
|
60
|
+
[{ x: '2016-03-14T24:00:00Z' }, 'Default'],
|
|
61
|
+
[{ x: '2016-03-14T01:59:00+24:00' }, 'Default'],
|
|
62
|
+
[{ x: 1457920740 }, 'Default'],
|
|
63
|
+
[{}, 'Default']
|
|
64
|
+
])
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
describe('StringMatches', () => {
|
|
68
|
+
expectMatches({ Variable: '$.x', StringMatches: 'log-*.txt' }, [
|
|
69
|
+
[{ x: 'log-1.txt' }, 'Match'],
|
|
70
|
+
[{ x: 'log-.txt' }, 'Match'],
|
|
71
|
+
[{ x: 'log-2021-01-01.txt' }, 'Match'],
|
|
72
|
+
[{ x: 'log-1.txt.bak' }, 'Default'],
|
|
73
|
+
[{ x: 'xlog-1.txt' }, 'Default'],
|
|
74
|
+
[{ x: 'log-1xtxt' }, 'Default'], // "." is literal
|
|
75
|
+
[{ x: 1 }, 'Default'],
|
|
76
|
+
[{}, 'Default']
|
|
77
|
+
])
|
|
78
|
+
expectMatches({ Variable: '$.x', StringMatches: '*' }, [
|
|
79
|
+
[{ x: '' }, 'Match'],
|
|
80
|
+
[{ x: 'anything\nat all' }, 'Match']
|
|
81
|
+
])
|
|
82
|
+
expectMatches({ Variable: '$.x', StringMatches: 'a\\*b' }, [
|
|
83
|
+
[{ x: 'a*b' }, 'Match'],
|
|
84
|
+
[{ x: 'axb' }, 'Default']
|
|
85
|
+
])
|
|
86
|
+
expectMatches({ Variable: '$.x', StringMatches: 'a\\\\*' }, [
|
|
87
|
+
[{ x: 'a\\' }, 'Match'],
|
|
88
|
+
[{ x: 'a\\anything' }, 'Match'],
|
|
89
|
+
[{ x: 'a' }, 'Default']
|
|
90
|
+
])
|
|
91
|
+
expectMatches({ Variable: '$.x', StringMatches: '(^$)[x]+?' }, [
|
|
92
|
+
[{ x: '(^$)[x]+?' }, 'Match'],
|
|
93
|
+
[{ x: 'x' }, 'Default']
|
|
94
|
+
])
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
describe('Comparisons do not coerce types', () => {
|
|
98
|
+
const wrongTypes = [
|
|
99
|
+
[{ Variable: '$.x', NumericEquals: 1 }, ['1', true, null, [1]]],
|
|
100
|
+
[{ Variable: '$.x', NumericGreaterThan: 5 }, ['10', true, [10]]],
|
|
101
|
+
[{ Variable: '$.x', NumericLessThan: 5 }, [null, false, '', []]],
|
|
102
|
+
[{ Variable: '$.x', NumericLessThanEquals: 0 }, [null, false, '', []]],
|
|
103
|
+
[{ Variable: '$.x', StringEquals: '1' }, [1, ['1']]],
|
|
104
|
+
[{ Variable: '$.x', StringLessThan: 'b' }, [1, null, false, ['a']]],
|
|
105
|
+
[{ Variable: '$.x', StringGreaterThanEquals: '' }, [0, null, false]],
|
|
106
|
+
[{ Variable: '$.x', BooleanEquals: false }, [0, '', null, 'false']],
|
|
107
|
+
[{ Variable: '$.x', BooleanEquals: true }, [1, 'true']]
|
|
108
|
+
]
|
|
109
|
+
for (const [rule, inputs] of wrongTypes) {
|
|
110
|
+
expectMatches(rule, inputs.map(x => [{ x }, 'Default']))
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
describe('String comparisons use Unicode code points', () => {
|
|
115
|
+
// U+1F600 is stored as a surrogate pair (0xD83D 0xDE00), which sorts
|
|
116
|
+
// below U+FF5E by UTF-16 code unit but above it by code point
|
|
117
|
+
expectMatches({ Variable: '$.x', StringGreaterThan: '~' }, [
|
|
118
|
+
[{ x: '\u{1F600}' }, 'Match']
|
|
119
|
+
])
|
|
120
|
+
expectMatches({ Variable: '$.x', StringLessThan: 'abd' }, [
|
|
121
|
+
[{ x: 'abc' }, 'Match'],
|
|
122
|
+
[{ x: 'ab' }, 'Match'],
|
|
123
|
+
[{ x: 'abd' }, 'Default'],
|
|
124
|
+
[{ x: 'abda' }, 'Default']
|
|
125
|
+
])
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
describe('Timestamps compare instants', () => {
|
|
129
|
+
const t = '2016-03-14T01:59:00Z'
|
|
130
|
+
expectMatches({ Variable: '$.x', TimestampEquals: t }, [
|
|
131
|
+
[{ x: t }, 'Match'],
|
|
132
|
+
[{ x: '2016-03-14T02:59:00+01:00' }, 'Match'],
|
|
133
|
+
[{ x: '2016-03-13T20:59:00-05:00' }, 'Match'],
|
|
134
|
+
[{ x: '2016-03-14T01:59:00.000Z' }, 'Match'],
|
|
135
|
+
[{ x: '2016-03-14T01:59:00.001Z' }, 'Default'],
|
|
136
|
+
[{ x: 'not a timestamp' }, 'Default'],
|
|
137
|
+
[{}, 'Default']
|
|
138
|
+
])
|
|
139
|
+
expectMatches({ Variable: '$.x', TimestampLessThan: '2016-03-14T01:00:00Z' }, [
|
|
140
|
+
[{ x: '2016-03-14T01:30:00+01:00' }, 'Match'],
|
|
141
|
+
[{ x: '2016-03-14T00:59:59.999999999Z' }, 'Match'],
|
|
142
|
+
[{ x: '2016-03-14T01:00:00.000000001Z' }, 'Default']
|
|
143
|
+
])
|
|
144
|
+
expectMatches({ Variable: '$.x', TimestampGreaterThan: t }, [
|
|
145
|
+
[{ x: '2017-01-01T00:00:00Z' }, 'Match'],
|
|
146
|
+
[{ x: 'zzz' }, 'Default']
|
|
147
|
+
])
|
|
148
|
+
expectMatches({ Variable: '$.x', TimestampGreaterThanEquals: t }, [
|
|
149
|
+
[{ x: t }, 'Match'],
|
|
150
|
+
[{ x: '2016-03-14T01:58:59Z' }, 'Default']
|
|
151
|
+
])
|
|
152
|
+
expectMatches({ Variable: '$.x', TimestampLessThanEquals: t }, [
|
|
153
|
+
[{ x: t }, 'Match'],
|
|
154
|
+
[{ x: '2016-03-14T01:59:01Z' }, 'Default']
|
|
155
|
+
])
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
describe('Path variants', () => {
|
|
159
|
+
expectMatches({ Variable: '$.x', TimestampLessThanPath: '$.y' }, [
|
|
160
|
+
[{ x: '2016-03-14T01:30:00+01:00', y: '2016-03-14T01:00:00Z' }, 'Match'],
|
|
161
|
+
[{ x: '2016-03-14T01:30:00Z', y: 'nope' }, 'Default']
|
|
162
|
+
])
|
|
163
|
+
expectMatches({ Variable: '$.x', StringEqualsPath: '$.y' }, [
|
|
164
|
+
[{ x: 'a', y: 'a' }, 'Match'],
|
|
165
|
+
[{ x: null, y: null }, 'Default'],
|
|
166
|
+
[{ x: 1, y: 1 }, 'Default'],
|
|
167
|
+
[{}, 'Default']
|
|
168
|
+
])
|
|
169
|
+
expectMatches({ Variable: '$.x', BooleanEqualsPath: '$.y' }, [
|
|
170
|
+
[{ x: false, y: false }, 'Match'],
|
|
171
|
+
[{ x: 0, y: 0 }, 'Default']
|
|
172
|
+
])
|
|
173
|
+
expectMatches({ Variable: '$.x', NumericGreaterThanPath: '$.limits.max' }, [
|
|
174
|
+
[{ x: 11, limits: { max: 10 } }, 'Match'],
|
|
175
|
+
[{ x: 11, limits: {} }, 'Default']
|
|
176
|
+
])
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
describe('Nested boolean expressions', () => {
|
|
180
|
+
const inTwenties = {
|
|
181
|
+
And: [
|
|
182
|
+
{ Variable: '$.n', NumericGreaterThanEquals: 20 },
|
|
183
|
+
{ Variable: '$.n', NumericLessThan: 30 }
|
|
184
|
+
]
|
|
185
|
+
}
|
|
186
|
+
expectMatches({ Or: [inTwenties, { Variable: '$.vip', BooleanEquals: true }] }, [
|
|
187
|
+
[{ n: 25, vip: false }, 'Match'],
|
|
188
|
+
[{ n: 5, vip: true }, 'Match'],
|
|
189
|
+
[{ n: 5, vip: false }, 'Default']
|
|
190
|
+
])
|
|
191
|
+
expectMatches({ Not: inTwenties }, [
|
|
192
|
+
[{ n: 25 }, 'Default'],
|
|
193
|
+
[{ n: 35 }, 'Match']
|
|
194
|
+
])
|
|
195
|
+
expectMatches({ And: [{ Not: { Variable: '$.a', StringEquals: 'x' } }, { Not: { Not: { Variable: '$.b', IsPresent: true } } }] }, [
|
|
196
|
+
[{ a: 'y', b: 1 }, 'Match'],
|
|
197
|
+
[{ a: 'x', b: 1 }, 'Default'],
|
|
198
|
+
[{ a: 'y' }, 'Default']
|
|
199
|
+
])
|
|
200
|
+
expectMatches({ Or: [{ And: [{ Or: [{ Not: { Variable: '$.a', NumericEquals: 1 } }] }] }] }, [
|
|
201
|
+
[{ a: 2 }, 'Match'],
|
|
202
|
+
[{ a: 1 }, 'Default']
|
|
203
|
+
])
|
|
204
|
+
|
|
205
|
+
it('short-circuits And', () => {
|
|
206
|
+
const rule = { And: [{ Variable: '$.a', IsPresent: true }, { Variable: '$.a.b', StringEquals: 'x' }] }
|
|
207
|
+
expect(nextState(rule, {})).to.equal('Default')
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it('ignores Next on nested rules', () => {
|
|
211
|
+
const rule = { And: [{ Variable: '$.a', NumericEquals: 1, Next: 'Ignored' }] }
|
|
212
|
+
expect(nextState(rule, { a: 1 })).to.equal('Match')
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
describe('Missing and non-object input', () => {
|
|
217
|
+
for (const input of [undefined, null, 42, 'text', true]) {
|
|
218
|
+
it(`IsPresent/IsNull on $.x with input ${JSON.stringify(input)} don't match`, () => {
|
|
219
|
+
expect(nextState({ Variable: '$.x', IsPresent: true }, input)).to.equal('Default')
|
|
220
|
+
expect(nextState({ Variable: '$.x', IsNull: true }, input)).to.equal('Default')
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
it('"$" selects the whole input, even when it is not an object', () => {
|
|
225
|
+
expect(nextState({ Variable: '$', NumericEquals: 42 }, 42)).to.equal('Match')
|
|
226
|
+
expect(nextState({ Variable: '$', StringEquals: 'text' }, 'text')).to.equal('Match')
|
|
227
|
+
expect(nextState({ Variable: '$', IsNull: true }, null)).to.equal('Match')
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
it('an explicit null is present', () => {
|
|
231
|
+
expect(nextState({ Variable: '$.x', IsPresent: true }, { x: null })).to.equal('Match')
|
|
232
|
+
expect(nextState({ Variable: '$.x', IsNull: true }, { x: null })).to.equal('Match')
|
|
233
|
+
})
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
describe('Context object ($$) paths', () => {
|
|
237
|
+
const context = { Execution: { Name: 'exec-1', Input: { retries: 3 } } }
|
|
238
|
+
|
|
239
|
+
it('reads Variable from the context object', () => {
|
|
240
|
+
expect(nextState({ Variable: '$$.Execution.Name', StringEquals: 'exec-1' }, {}, context)).to.equal('Match')
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
it('reads ...Path comparison values from the context object', () => {
|
|
244
|
+
expect(nextState({ Variable: '$.retries', NumericLessThanPath: '$$.Execution.Input.retries' }, { retries: 2 }, context)).to.equal('Match')
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
it('treats $$ paths as missing when no context object is given', () => {
|
|
248
|
+
expect(nextState({ Variable: '$$.Execution.Name', IsPresent: true }, {})).to.equal('Default')
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
it('does not mix up $ and $$ paths in the cache', () => {
|
|
252
|
+
const calculate = choiceProcessor({
|
|
253
|
+
Choices: [{ And: [{ Variable: '$.Execution.Name', StringEquals: 'input' }, { Variable: '$$.Execution.Name', StringEquals: 'exec-1' }], Next: 'Match' }],
|
|
254
|
+
Default: 'Default'
|
|
255
|
+
})
|
|
256
|
+
expect(calculate({ Execution: { Name: 'input' } }, context)).to.equal('Match')
|
|
257
|
+
})
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
describe('Results', () => {
|
|
261
|
+
it('returns the first matching rule in order', () => {
|
|
262
|
+
const calculate = choiceProcessor({
|
|
263
|
+
Choices: [
|
|
264
|
+
{ Variable: '$.n', NumericGreaterThan: 10, Next: 'Big' },
|
|
265
|
+
{ Variable: '$.n', NumericGreaterThan: 0, Next: 'Positive' }
|
|
266
|
+
],
|
|
267
|
+
Default: 'Other'
|
|
268
|
+
})
|
|
269
|
+
expect(calculate({ n: 20 })).to.equal('Big')
|
|
270
|
+
expect(calculate({ n: 5 })).to.equal('Positive')
|
|
271
|
+
expect(calculate({ n: -1 })).to.equal('Other')
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
it('returns null when nothing matches and there is no Default', () => {
|
|
275
|
+
const calculate = choiceProcessor({ Choices: [{ Variable: '$.n', NumericEquals: 1, Next: 'One' }] })
|
|
276
|
+
expect(calculate({ n: 2 })).to.equal(null)
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
it('does not mutate the definition', () => {
|
|
280
|
+
const definition = {
|
|
281
|
+
Choices: [{ Variable: '$.x', NumericEqualsPath: '$.y', Next: 'Match' }, { Not: { Variable: '$.x', IsNull: true }, Next: 'NotNull' }],
|
|
282
|
+
Default: 'Default'
|
|
283
|
+
}
|
|
284
|
+
const copy = JSON.parse(JSON.stringify(definition))
|
|
285
|
+
const calculate = choiceProcessor(definition)
|
|
286
|
+
calculate({ x: 1, y: 1 })
|
|
287
|
+
calculate({ x: 2, y: 1 })
|
|
288
|
+
expect(definition).to.eql(copy)
|
|
289
|
+
})
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
describe('Tymly extensions', () => {
|
|
293
|
+
expectMatches({ Variable: '$.x', Includes: { id: 2 } }, [
|
|
294
|
+
[{ x: [{ id: 1 }, { id: 2 }] }, 'Match'],
|
|
295
|
+
[{ x: [{ id: 1 }] }, 'Default'],
|
|
296
|
+
[{ x: { id: 2 } }, 'Default']
|
|
297
|
+
])
|
|
298
|
+
expectMatches({ Variable: '$.x', IsUndefined: true }, [
|
|
299
|
+
[{}, 'Match'],
|
|
300
|
+
[{ x: null }, 'Default']
|
|
301
|
+
])
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
describe('Invalid definitions are rejected when the processor is created', () => {
|
|
305
|
+
const invalid = [
|
|
306
|
+
['no definition', undefined, /definition must be an object/],
|
|
307
|
+
['no Choices', {}, /Choices: must be a non-empty array/],
|
|
308
|
+
['empty Choices', { Choices: [] }, /Choices: must be a non-empty array/],
|
|
309
|
+
['bad Default', { Choices: [{ Variable: '$.x', IsNull: true, Next: 'A' }], Default: 42 }, /Default: must be a state name/],
|
|
310
|
+
['rule not an object', { Choices: ['nope'] }, /Choices\[0\]: a Choice Rule must be an object/],
|
|
311
|
+
['missing Next', { Choices: [{ Variable: '$.x', IsNull: true }] }, /Choices\[0\]: top-level Choice Rules must have a Next/],
|
|
312
|
+
['misspelt operator', { Choices: [{ Variable: '$.x', NumericEqual: 1, Next: 'A' }] }, /unrecognised: NumericEqual/],
|
|
313
|
+
['no operator', { Choices: [{ Variable: '$.x', Next: 'A' }] }, /no comparison or boolean operator found$/],
|
|
314
|
+
['two operators', { Choices: [{ Variable: '$.x', NumericEquals: 1, IsNull: false, Next: 'A' }] }, /only one operator is allowed per rule, found NumericEquals, IsNull/],
|
|
315
|
+
['missing Variable', { Choices: [{ NumericEquals: 1, Next: 'A' }] }, /Variable must be a Path/],
|
|
316
|
+
['Variable not a Path', { Choices: [{ Variable: 'x', NumericEquals: 1, Next: 'A' }] }, /Variable must be a Path/],
|
|
317
|
+
['Numeric with a string', { Choices: [{ Variable: '$.x', NumericEquals: '1', Next: 'A' }] }, /NumericEquals must be a number, got "1"/],
|
|
318
|
+
['String with a number', { Choices: [{ Variable: '$.x', StringEquals: 1, Next: 'A' }] }, /StringEquals must be a string, got 1/],
|
|
319
|
+
['String with null', { Choices: [{ Variable: '$.x', StringEquals: null, Next: 'A' }] }, /StringEquals must be a string, got null/],
|
|
320
|
+
['StringMatches with a number', { Choices: [{ Variable: '$.x', StringMatches: 1, Next: 'A' }] }, /StringMatches must be a string/],
|
|
321
|
+
['bad timestamp', { Choices: [{ Variable: '$.x', TimestampEquals: '2016-03-14', Next: 'A' }] }, /TimestampEquals must be an RFC3339 timestamp/],
|
|
322
|
+
['BooleanEquals with a string', { Choices: [{ Variable: '$.x', BooleanEquals: 'true', Next: 'A' }] }, /BooleanEquals must be a boolean/],
|
|
323
|
+
['IsPresent with a string', { Choices: [{ Variable: '$.x', IsPresent: 'true', Next: 'A' }] }, /IsPresent must be a boolean/],
|
|
324
|
+
['Path variant without a Path', { Choices: [{ Variable: '$.x', NumericEqualsPath: 'y', Next: 'A' }] }, /NumericEqualsPath must be a Path/],
|
|
325
|
+
['StringMatchesPath', { Choices: [{ Variable: '$.x', StringMatchesPath: '$.y', Next: 'A' }] }, /unrecognised: StringMatchesPath/],
|
|
326
|
+
['empty And', { Choices: [{ And: [], Next: 'A' }] }, /Choices\[0\].And: must be a non-empty array/],
|
|
327
|
+
['Or not an array', { Choices: [{ Or: { Variable: '$.x', IsNull: true }, Next: 'A' }] }, /Choices\[0\].Or: must be a non-empty array/],
|
|
328
|
+
['bad nested rule', { Choices: [{ Or: [{ Variable: '$.x', IsNull: true }, { Not: { Variable: '$.y', NumericEqual: 1 } }], Next: 'A' }] }, /Choices\[0\].Or\[1\].Not: no comparison or boolean operator found/],
|
|
329
|
+
['JSONata state', { QueryLanguage: 'JSONata', Choices: [{ Condition: '{% true %}', Next: 'A' }] }, /JSONata Choice states/],
|
|
330
|
+
['JSONata Condition rule', { Choices: [{ Condition: '{% true %}', Next: 'A' }] }, /JSONata Condition rules are not supported/]
|
|
331
|
+
]
|
|
332
|
+
|
|
333
|
+
for (const [name, definition, message] of invalid) {
|
|
334
|
+
it(name, () => {
|
|
335
|
+
expect(() => choiceProcessor(definition)).to.throw(ChoiceDefinitionError, message)
|
|
336
|
+
})
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
it('allows Comment on rules', () => {
|
|
340
|
+
expect(nextState({ Variable: '$.x', IsNull: true, Comment: 'why' }, { x: null })).to.equal('Match')
|
|
341
|
+
})
|
|
342
|
+
})
|
|
343
|
+
})
|
package/lib/execute-operator.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const findOperator = require('./find-operator')
|
|
2
|
-
|
|
3
|
-
const isJSONPath = p => (p && typeof p.valueOf() === 'string') && p.length !== 0 && p[0] === '$'
|
|
4
|
-
|
|
5
|
-
module.exports = function executeOperator (choice, inputCache, values) {
|
|
6
|
-
const operator = findOperator(choice)
|
|
7
|
-
|
|
8
|
-
const inputValue = inputCache.get(choice.Variable, values)
|
|
9
|
-
|
|
10
|
-
if (operator.isPath && isJSONPath(operator.value)) {
|
|
11
|
-
operator.value = inputCache.get(operator.value, values)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return operator.fn(
|
|
15
|
-
inputValue,
|
|
16
|
-
operator.value,
|
|
17
|
-
choice.Next,
|
|
18
|
-
inputCache
|
|
19
|
-
)
|
|
20
|
-
}
|
package/lib/find-operator.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
const operators = require('./operators')
|
|
2
|
-
|
|
3
|
-
module.exports = function findOperator (choice) {
|
|
4
|
-
let fn
|
|
5
|
-
let value
|
|
6
|
-
let isPath = false
|
|
7
|
-
|
|
8
|
-
Object.entries(choice).forEach(([k, v]) => {
|
|
9
|
-
if (fn === undefined) {
|
|
10
|
-
if (k.endsWith('Path')) {
|
|
11
|
-
k = k.split('Path')[0]
|
|
12
|
-
isPath = true
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (operators[k]) {
|
|
16
|
-
fn = operators[k]
|
|
17
|
-
value = v
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
fn,
|
|
24
|
-
value,
|
|
25
|
-
isPath
|
|
26
|
-
}
|
|
27
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
module.exports = function includesOperator (inputValue, comparisonValue, candidateStateName, cache) {
|
|
4
|
-
let nextState
|
|
5
|
-
if (Array.isArray(inputValue) && inputValue.includes(comparisonValue)) {
|
|
6
|
-
nextState = candidateStateName
|
|
7
|
-
}
|
|
8
|
-
return nextState
|
|
9
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
module.exports = function isBooleanOperator (inputValue, comparisonValue, candidateStateName, cache) {
|
|
2
|
-
const isBoolean = typeof inputValue === 'boolean'
|
|
3
|
-
|
|
4
|
-
if (isBoolean && comparisonValue === true) {
|
|
5
|
-
return candidateStateName
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
if (!isBoolean && comparisonValue !== true) {
|
|
9
|
-
return candidateStateName
|
|
10
|
-
}
|
|
11
|
-
}
|
package/lib/operators/is-null.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
module.exports = function isNullOperator (inputValue, comparisonValue, candidateStateName, cache) {
|
|
4
|
-
const isNull = inputValue === null
|
|
5
|
-
|
|
6
|
-
if (isNull && comparisonValue === true) {
|
|
7
|
-
return candidateStateName
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (!isNull && comparisonValue !== true) {
|
|
11
|
-
return candidateStateName
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
module.exports = function isStringOperator (inputValue, comparisonValue, candidateStateName, cache) {
|
|
4
|
-
const isString = typeof inputValue === 'string'
|
|
5
|
-
|
|
6
|
-
if (isString && comparisonValue === true) {
|
|
7
|
-
return candidateStateName
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (!isString && comparisonValue !== true) {
|
|
11
|
-
return candidateStateName
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
module.exports = function isUndefinedOperator (inputValue, comparisonValue, candidateStateName, cache) {
|
|
4
|
-
const isUndefined = inputValue === undefined
|
|
5
|
-
|
|
6
|
-
if (isUndefined && comparisonValue === true) {
|
|
7
|
-
return candidateStateName
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (!isUndefined && comparisonValue !== true) {
|
|
11
|
-
return candidateStateName
|
|
12
|
-
}
|
|
13
|
-
}
|