@@ -10,15 +10,17 @@ export default class Condition {
1010 Object . assign ( this , properties )
1111 if ( booleanOperator ) {
1212 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` )
1616 this . operator = booleanOperator
1717 // boolean conditions always have a priority; default 1
1818 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+ }
2224 } else {
2325 if ( ! Object . prototype . hasOwnProperty . call ( properties , 'fact' ) ) throw new Error ( 'Condition: constructor "fact" property required' )
2426 if ( ! Object . prototype . hasOwnProperty . call ( properties , 'operator' ) ) throw new Error ( 'Condition: constructor "operator" property required' )
@@ -44,7 +46,11 @@ export default class Condition {
4446 }
4547 const oper = Condition . booleanOperator ( this )
4648 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+ }
4854 } else {
4955 props . operator = this . operator
5056 props . value = this . value
@@ -110,27 +116,29 @@ export default class Condition {
110116 /**
111117 * Returns the boolean operator for the condition
112118 * 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 ' }
114120 */
115121 static booleanOperator ( condition ) {
116122 if ( Object . prototype . hasOwnProperty . call ( condition , 'any' ) ) {
117123 return 'any'
118124 } else if ( Object . prototype . hasOwnProperty . call ( condition , 'all' ) ) {
119125 return 'all'
126+ } else if ( Object . prototype . hasOwnProperty . call ( condition , 'not' ) ) {
127+ return 'not'
120128 }
121129 }
122130
123131 /**
124132 * Returns the condition's boolean operator
125133 * 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)
127135 */
128136 booleanOperator ( ) {
129137 return Condition . booleanOperator ( this )
130138 }
131139
132140 /**
133- * Whether the operator is boolean ('all', 'any')
141+ * Whether the operator is boolean ('all', 'any', 'not' )
134142 * @returns {Boolean }
135143 */
136144 isBooleanOperator ( ) {
0 commit comments