@@ -10,15 +10,17 @@ export default class Condition {
10
10
Object . assign ( this , properties )
11
11
if ( booleanOperator ) {
12
12
const subConditions = properties [ booleanOperator ]
13
- if ( ! ( Array . isArray ( subConditions ) ) ) {
14
- throw new Error ( `"${ booleanOperator } " must be an array` )
15
- }
13
+ const subConditionsIsArray = Array . isArray ( subConditions )
14
+ if ( booleanOperator !== 'not' && ! subConditionsIsArray ) throw new Error ( `"${ booleanOperator } " must be an array` )
15
+ if ( booleanOperator === 'not' && subConditionsIsArray ) throw new Error ( `" ${ booleanOperator } " cannot be an array` )
16
16
this . operator = booleanOperator
17
17
// boolean conditions always have a priority; default 1
18
18
this . priority = parseInt ( properties . priority , 10 ) || 1
19
- this [ booleanOperator ] = subConditions . map ( ( c ) => {
20
- return new Condition ( c )
21
- } )
19
+ if ( subConditionsIsArray ) {
20
+ this [ booleanOperator ] = subConditions . map ( ( c ) => new Condition ( c ) )
21
+ } else {
22
+ this [ booleanOperator ] = new Condition ( subConditions )
23
+ }
22
24
} else {
23
25
if ( ! Object . prototype . hasOwnProperty . call ( properties , 'fact' ) ) throw new Error ( 'Condition: constructor "fact" property required' )
24
26
if ( ! Object . prototype . hasOwnProperty . call ( properties , 'operator' ) ) throw new Error ( 'Condition: constructor "operator" property required' )
@@ -44,7 +46,11 @@ export default class Condition {
44
46
}
45
47
const oper = Condition . booleanOperator ( this )
46
48
if ( oper ) {
47
- props [ oper ] = this [ oper ] . map ( ( c ) => c . toJSON ( stringify ) )
49
+ if ( Array . isArray ( this [ oper ] ) ) {
50
+ props [ oper ] = this [ oper ] . map ( ( c ) => c . toJSON ( false ) )
51
+ } else {
52
+ props [ oper ] = this [ oper ] . toJSON ( false )
53
+ }
48
54
} else {
49
55
props . operator = this . operator
50
56
props . value = this . value
@@ -110,27 +116,29 @@ export default class Condition {
110
116
/**
111
117
* Returns the boolean operator for the condition
112
118
* If the condition is not a boolean condition, the result will be 'undefined'
113
- * @return {string 'all' or 'any ' }
119
+ * @return {string 'all', 'any', or 'not ' }
114
120
*/
115
121
static booleanOperator ( condition ) {
116
122
if ( Object . prototype . hasOwnProperty . call ( condition , 'any' ) ) {
117
123
return 'any'
118
124
} else if ( Object . prototype . hasOwnProperty . call ( condition , 'all' ) ) {
119
125
return 'all'
126
+ } else if ( Object . prototype . hasOwnProperty . call ( condition , 'not' ) ) {
127
+ return 'not'
120
128
}
121
129
}
122
130
123
131
/**
124
132
* Returns the condition's boolean operator
125
133
* Instance version of Condition.isBooleanOperator
126
- * @returns {string,undefined } - 'any', 'all', or undefined (if not a boolean condition)
134
+ * @returns {string,undefined } - 'any', 'all', 'not' or undefined (if not a boolean condition)
127
135
*/
128
136
booleanOperator ( ) {
129
137
return Condition . booleanOperator ( this )
130
138
}
131
139
132
140
/**
133
- * Whether the operator is boolean ('all', 'any')
141
+ * Whether the operator is boolean ('all', 'any', 'not' )
134
142
* @returns {Boolean }
135
143
*/
136
144
isBooleanOperator ( ) {
0 commit comments