File tree Expand file tree Collapse file tree 3 files changed +10
-7
lines changed Expand file tree Collapse file tree 3 files changed +10
-7
lines changed Original file line number Diff line number Diff line change @@ -50,7 +50,6 @@ class Engine extends EventEmitter {
50
50
rule = new Rule ( properties )
51
51
}
52
52
rule . setEngine ( this )
53
- if ( ! rule . id ) rule . id = '_' + Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 )
54
53
this . rules . push ( rule )
55
54
this . prioritizedRules = null
56
55
return this
Original file line number Diff line number Diff line change @@ -36,9 +36,7 @@ class Rule extends EventEmitter {
36
36
this . setName ( options . name )
37
37
}
38
38
39
- if ( options && ( options . id ) ) {
40
- this . setId ( options . id )
41
- }
39
+ this . setId ( options ? options . id : null )
42
40
43
41
const priority = ( options && options . priority ) || 1
44
42
this . setPriority ( priority )
@@ -63,10 +61,11 @@ class Rule extends EventEmitter {
63
61
* @param {any } id - any truthy input
64
62
*/
65
63
setId ( id ) {
66
- if ( ! id && id !== 0 ) {
67
- throw new Error ( 'Rule "id" must be defined' )
64
+ if ( ! id ) {
65
+ this . id = '_' + Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 )
66
+ } else {
67
+ this . id = id
68
68
}
69
- this . id = id
70
69
return this
71
70
}
72
71
Original file line number Diff line number Diff line change @@ -93,6 +93,11 @@ describe('Engine', () => {
93
93
engine . updateRule ( rule )
94
94
expect ( engine . rules [ 0 ] . conditions . all . length ) . to . equal ( 0 )
95
95
} )
96
+ it ( 'should generate id for rule if not provided' , ( ) => {
97
+ const rule = new Rule ( factories . rule ( ) )
98
+ expect ( rule . id ) . to . not . equal ( null )
99
+ expect ( rule . id ) . to . not . equal ( undefined )
100
+ } )
96
101
it ( 'should throw error if rule not found' , ( ) => {
97
102
const rule1 = new Rule ( factories . rule ( ) )
98
103
engine . addRule ( rule1 )
You can’t perform that action at this time.
0 commit comments