-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
71 lines (52 loc) · 1.58 KB
/
command.js
File metadata and controls
71 lines (52 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { ulid } from 'ulidx'
import jsonata from 'jsonata'
import { ExpectationEvaluationError } from './errors.js'
export class Command {
constructor( command, opts = {} ) {
if (!command) {
throw new Error('the command argument must be defined')
}
this._command = command
this._id = opts.id || ( opts?.genId instanceof Function ? opts.genId() : ulid() )
this._name = opts.name
this._description = opts.description
this._strict = ( opts.strict !== undefined && opts.strict !== null ) ? opts.strict : true
// evaluate expect
if ( opts.expect !== undefined && opts.expect !== null && opts.expect.trim().length > 0 ) {
try {
jsonata(opts.expect)
} catch ( err ) {
throw new ExpectationEvaluationError( `expectation could not be evaluated: ${err.message}` )
}
this._expect = opts.expect
this._expectationDescription = opts.expectationDescription
this._onExpectationFailure = opts.onExpectationFailure || 'throw'
} else {
this._onExpectationFailure = 'throw' //shouldnt be necessary but put here just in case
}
}
get id() {
return this._id
}
get name() {
return this._name
}
get description() {
return this._description
}
get strict() {
return this._strict
}
get command() {
return this._command
}
get expect() {
return this._expect
}
get onExpectationFailure() {
return this._onExpectationFailure
}
execute( results ) {
throw new Error('abstract method execute( results ) not implemented in base command class');
}
}