(properties)
| 4 | |
| 5 | export default class Condition { |
| 6 | constructor (properties) { |
| 7 | if (!properties) throw new Error('Condition: constructor options required') |
| 8 | const booleanOperator = Condition.booleanOperator(properties) |
| 9 | Object.assign(this, properties) |
| 10 | if (booleanOperator) { |
| 11 | const subConditions = properties[booleanOperator] |
| 12 | const subConditionsIsArray = Array.isArray(subConditions) |
| 13 | if (booleanOperator !== 'not' && !subConditionsIsArray) { throw new Error(`"${booleanOperator}" must be an array`) } |
| 14 | if (booleanOperator === 'not' && subConditionsIsArray) { throw new Error(`"${booleanOperator}" cannot be an array`) } |
| 15 | this.operator = booleanOperator |
| 16 | // boolean conditions always have a priority; default 1 |
| 17 | this.priority = parseInt(properties.priority, 10) || 1 |
| 18 | if (subConditionsIsArray) { |
| 19 | this[booleanOperator] = subConditions.map((c) => new Condition(c)) |
| 20 | } else { |
| 21 | this[booleanOperator] = new Condition(subConditions) |
| 22 | } |
| 23 | } else if (!Object.prototype.hasOwnProperty.call(properties, 'condition')) { |
| 24 | if (!Object.prototype.hasOwnProperty.call(properties, 'fact')) { throw new Error('Condition: constructor "fact" property required') } |
| 25 | if (!Object.prototype.hasOwnProperty.call(properties, 'operator')) { throw new Error('Condition: constructor "operator" property required') } |
| 26 | if (!Object.prototype.hasOwnProperty.call(properties, 'value')) { throw new Error('Condition: constructor "value" property required') } |
| 27 | |
| 28 | // a non-boolean condition does not have a priority by default. this allows |
| 29 | // priority to be dictated by the fact definition |
| 30 | if (Object.prototype.hasOwnProperty.call(properties, 'priority')) { |
| 31 | properties.priority = parseInt(properties.priority, 10) |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Converts the condition into a json-friendly structure |
nothing calls this directly
no test coverage detected