import Immutable from "immutable" ; import is from "is" ; import Rule from "./rule" ; const inherits = require ( "util" ) . inherits ; const RulesSetRecord = Immutable . Record ( { rules : Immutable . List ( ) , } ) ; function RulesSet ( rules ) { if ( ! ( this instanceof RulesSet ) ) { return new RulesSet ( rules ) ; } rules = new RulesList ( rules ) ; RulesSetRecord . call ( this , { rules : rules , } ) ; } inherits ( RulesSet , RulesSetRecord ) ; RulesSet . prototype . getRules = function ( ) { return this . get ( "rules" ) ; } ; param { Rule | RulesSet | Array } rules return { RulesSet } RulesSet . prototype . add = function ( newRules ) { let rules = this . getRules ( ) ; newRules = new RulesList ( newRules ) ; rules = rules . concat ( newRules ) ; return this . set ( "rules" , rules ) ; } ; param { Rule | RulesSet | Array } rules return { RulesSet } RulesSet . prototype . unshift = function ( newRules ) { let rules = this . getRules ( ) ; newRules = new RulesList ( newRules ) ; rules = rules . unshift . apply ( rules , newRules . toArray ( ) ) ; return this . set ( "rules" , rules ) ; } ; param { string } ruleType return { RulesSet } RulesSet . prototype . del = function ( ruleType ) { let rules = this . getRules ( ) ; rules = rules . filterNot ( ( rule ) => { return rule . getType ( ) == ruleType ; } ) ; return this . set ( "rules" , rules ) ; } ; param { Rule } rule return { RulesSet } RulesSet . prototype . replace = function ( rule ) { return this . del ( rule . getType ( ) ) . add ( rule ) ; } ; param { string } ruleType return { Rule } RulesSet . prototype . getRule = function ( ruleType ) { const rules = this . getRules ( ) ; return rules . find ( ( rule ) => { return rule . getType ( ) == ruleType ; } ) ; } ; param { Rule | RulesSet | Array } rules return { List < Rule > } function RulesList ( rules ) { if ( rules instanceof Rule ) { return Immutable . List ( [ rules ] ) ; } if ( is . array ( rules ) ) { return Immutable . List ( rules ) ; } if ( rules instanceof RulesSet ) { return rules . getRules ( ) ; } return rules || Immutable . List ( ) ; } export default RulesSet ;