Skip to content

Commit d46ff3a

Browse files
authored
feat(operator): pass factory and constructor options
feat(operator): validation options 1. Allow consumers to disable ArgumentErrors in the 1.1. Operator.constructor 1.2. Operator.factory method 2. Add StringVariable methods to the list of validOperators 3. Expose operatorType to the public API. 4. Fix dependency versions #76
1 parent fb2ab19 commit d46ff3a

File tree

5 files changed

+223
-142
lines changed

5 files changed

+223
-142
lines changed

lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const factory = require('./factory/factory')
88
const MapVariable = require('./variable/map-variable')
99
const NumberVariable = require('./variable/number-variable')
1010
const ObjectVariable = require('./variable/object-variable')
11+
const operatorType = require('./operator/operator-type')
1112
const Operator = require('./operator/operator')
1213
const Proposition = require('./proposition/proposition')
1314
const Rule = require('./rule/rule')
@@ -34,6 +35,7 @@ const archetypesRules = {
3435
MapVariable,
3536
NumberVariable,
3637
ObjectVariable,
38+
operatorType,
3739
Operator,
3840
Proposition,
3941
Rule,

lib/operator/__tests__/operator.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ describe('archetypes.rules.Operator', () => {
99
expect(operator.name).toBe('AND')
1010
})
1111

12+
describe('constructor', () => {
13+
it('can ignore "valid" operator types', () => {
14+
const operator = new Operator('POO_POO_PLATER', {
15+
throws: false
16+
})
17+
expect(operator.name).toBe('POO_POO_PLATER')
18+
})
19+
})
20+
1221
describe('toOperationName()', () => {
1322
it('returns the operation method name to be invoked', () => {
1423
const operator = new Operator('CONFORMS_TO')
@@ -43,5 +52,14 @@ describe('archetypes.rules.Operator', () => {
4352
}).toThrow()
4453
})
4554
})
55+
56+
describe('factory', () => {
57+
it('creates a new Operator', () => {
58+
const operator = Operator.factory('POOP', {
59+
throws: false
60+
})
61+
expect(operator.name).toBe('POOP')
62+
})
63+
})
4664
})
4765
})

lib/operator/operator.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ const ow = require('ow')
55
const typeInspector = require('../type-inspector/type-inspector')
66
const validOperators = require('./valid-operators.json')
77

8+
const defaultOptions = {
9+
throws: true
10+
}
11+
812
class Operator {
9-
constructor (name) {
10-
Operator.isValid(name, {
11-
'throws': true
12-
})
13+
constructor (name, options = defaultOptions) {
14+
Operator.isValid(name, options)
1315
this.name = name
1416
assignTypeTo(this)
1517
}
@@ -22,9 +24,9 @@ class Operator {
2224
return this.name
2325
}
2426

25-
static factory (nameOrOperator) {
27+
static factory (nameOrOperator, options = defaultOptions) {
2628
if (is.string(nameOrOperator)) {
27-
return new Operator(nameOrOperator)
29+
return new Operator(nameOrOperator, options)
2830
}
2931
return nameOrOperator
3032
}
@@ -33,9 +35,12 @@ class Operator {
3335
return ruleElement.type === typeInspector.OPERATOR
3436
}
3537

36-
static isValid (name, options = {
37-
'throws': false
38-
}) {
38+
static isValid (
39+
name,
40+
options = {
41+
throws: false
42+
}
43+
) {
3944
if (options.throws) {
4045
ow(name, ow.string.oneOf(validOperators))
4146
}

lib/operator/valid-operators.json

+9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
[
2+
"AFTER",
23
"AND",
4+
"BEFORE",
35
"CONFORMS_TO",
46
"ENDS_WITH",
57
"EQ",
68
"EQUAL_TO",
79
"EVERY",
10+
"EXCEEDS_MAX_LENGTH",
811
"GREATER_THAN_OR_EQUAL_TO",
912
"GREATER_THAN",
1013
"GT",
@@ -13,10 +16,16 @@
1316
"HAS",
1417
"INCLUDES",
1518
"INCLUDES_ANY",
19+
"IS_EMPTY",
20+
"IS_NOT_EMPTY",
21+
"IS_ONE_OF",
22+
"LACKS_MIN_LENGTH",
1623
"LESS_THAN_OR_EQUAL_TO",
1724
"LESS_THAN",
1825
"LT",
1926
"LTE",
27+
"MATCH",
28+
"MATCHES",
2029
"NEQ",
2130
"NOT_EQUAL_TO",
2231
"NOT",

0 commit comments

Comments
 (0)