Skip to content

Commit eb20c07

Browse files
author
beatnory
committed
Improved code
1 parent 9cb1148 commit eb20c07

File tree

3 files changed

+18
-32
lines changed

3 files changed

+18
-32
lines changed

docs/engine.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ let rule = new Rule()
7474
engine.addRule(rule)
7575
```
7676

77-
### engine.removeRule(Rule instance | Object options)
77+
### engine.removeRule(Rule instance)
7878

7979
Removes a rule from the engine.
8080

src/engine.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,14 @@ class Engine extends EventEmitter {
5959

6060
/**
6161
* Remove a rule from the engine
62-
* @param {object|Rule} properties - rule definition. can be JSON representation, or instance of Rule
63-
* @param {Object} properties.conditions - conditions to evaluate when processing this rule
62+
* @param {object|Rule} rule - rule definition. Must be a instance of Rule
6463
*/
65-
removeRule (properties) {
66-
if (!properties) throw new Error('Engine: removeRule() requires options')
67-
if (!properties.hasOwnProperty('conditions')) throw new Error('Engine: removeRule() argument requires "conditions" property')
64+
removeRule (rule) {
65+
if ((rule instanceof Rule) === false) throw new Error('Engine: removeRule() rule must be a instance of Rule')
6866

69-
let index
70-
if (properties instanceof Rule) {
71-
index = this.rules.indexOf(properties)
72-
} else {
73-
index = this.rules.indexOf(r => r.conditions === properties.conditions)
74-
}
75-
76-
if (index === -1) throw new Error('Engine: removeRule() Rule was not found')
77-
this.rules.splice(index, 1)
67+
let index = this.rules.indexOf(rule)
68+
if (index === -1) return false
69+
return Boolean(this.rules.splice(index, 1).length)
7870
}
7971

8072
/**
@@ -106,8 +98,7 @@ class Engine extends EventEmitter {
10698
operatorName = operatorOrName
10799
}
108100

109-
if (!this.operators.has(operatorName)) throw new Error('Engine: removeOperator() Operator was not found')
110-
this.operators.delete(operatorName)
101+
return this.operators.delete(operatorName)
111102
}
112103

113104
/**
@@ -141,8 +132,8 @@ class Engine extends EventEmitter {
141132
} else {
142133
factId = factOrId.id
143134
}
144-
if (!this.facts.has(factId)) throw new Error('Engine: removeFact() Fact was not found')
145-
this.facts.delete(factId)
135+
136+
return this.facts.delete(factId)
146137
}
147138

148139
/**

test/engine.test.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,17 @@ describe('Engine', () => {
8787

8888
describe('required fields', () => {
8989
it('.conditions', () => {
90-
let rule = factories.rule()
91-
delete rule.conditions
9290
expect(() => {
93-
engine.removeRule(rule)
94-
}).to.throw(/Engine: removeRule\(\) argument requires "conditions" property/)
91+
engine.removeRule([])
92+
}).to.throw(/Engine: removeRule\(\) rule must be a instance of Rule/)
9593
})
9694
})
9795

9896
it('can only remove added rules', () => {
9997
expect(engine.rules.length).to.equal(0)
10098
let rule = new Rule(factories.rule())
101-
expect(() => {
102-
engine.removeRule(rule)
103-
}).to.throw(/Engine: removeRule\(\) Rule was not found/)
99+
const isRemoved = engine.removeRule(rule)
100+
expect(isRemoved).to.equal(false)
104101
})
105102
})
106103

@@ -137,9 +134,8 @@ describe('Engine', () => {
137134

138135
it('can only remove added operators', () => {
139136
expect(engine.operators.size).to.equal(10)
140-
expect(() => {
141-
engine.removeOperator('nonExisting')
142-
}).to.throw(/Engine: removeOperator\(\) Operator was not found/)
137+
const isRemoved = engine.removeOperator('nonExisting')
138+
expect(isRemoved).to.equal(false)
143139
})
144140
})
145141

@@ -206,9 +202,8 @@ describe('Engine', () => {
206202

207203
it('can only remove added facts', () => {
208204
expect(engine.facts.size).to.equal(0)
209-
expect(() => {
210-
engine.removeFact('newFact')
211-
}).to.throw(/Engine: removeFact\(\) Fact was not found/)
205+
const isRemoved = engine.removeFact('newFact')
206+
expect(isRemoved).to.equal(false)
212207
})
213208
})
214209

0 commit comments

Comments
 (0)