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 {
5050 rule = new Rule ( properties )
5151 }
5252 rule . setEngine ( this )
53- if ( ! rule . id ) rule . id = '_' + Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 )
5453 this . rules . push ( rule )
5554 this . prioritizedRules = null
5655 return this
Original file line number Diff line number Diff line change @@ -36,9 +36,7 @@ class Rule extends EventEmitter {
3636 this . setName ( options . name )
3737 }
3838
39- if ( options && ( options . id ) ) {
40- this . setId ( options . id )
41- }
39+ this . setId ( options ? options . id : null )
4240
4341 const priority = ( options && options . priority ) || 1
4442 this . setPriority ( priority )
@@ -63,10 +61,11 @@ class Rule extends EventEmitter {
6361 * @param {any } id - any truthy input
6462 */
6563 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
6868 }
69- this . id = id
7069 return this
7170 }
7271
Original file line number Diff line number Diff line change @@ -93,6 +93,11 @@ describe('Engine', () => {
9393 engine . updateRule ( rule )
9494 expect ( engine . rules [ 0 ] . conditions . all . length ) . to . equal ( 0 )
9595 } )
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+ } )
96101 it ( 'should throw error if rule not found' , ( ) => {
97102 const rule1 = new Rule ( factories . rule ( ) )
98103 engine . addRule ( rule1 )
You can’t perform that action at this time.
0 commit comments