Skip to content

Commit

Permalink
Preventing this.rules from becoming bloated with dead duplicate rules
Browse files Browse the repository at this point in the history
  • Loading branch information
src-code committed Jul 13, 2015
1 parent 0275715 commit 4546052
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/atomizer.js
Expand Up @@ -35,24 +35,23 @@ function Atomizer(options/*:AtomizerOptions*/, rules/*:AtomizerRules*/) {
*/
Atomizer.prototype.addRules = function(rules/*:AtomizerRules*/)/*:void*/ {
rules.forEach(function (rule) {
if (
(rule.type === 'pattern'
&& this.rulesMap.hasOwnProperty(rule.matcher)
&& !_.isEqual(this.rules[this.rulesMap[rule.matcher]], rule)) ||
(rule.type === 'helper'
&& this.helpersMap.hasOwnProperty(rule.matcher)
&& !_.isEqual(this.rules[this.helpersMap[rule.matcher]], rule))
) {
var ruleFound = rule.type === 'pattern' && this.rulesMap.hasOwnProperty(rule.matcher);
var helperFound = rule.type === 'helper' && this.helpersMap.hasOwnProperty(rule.matcher);

if ((ruleFound && !_.isEqual(this.rules[this.rulesMap[rule.matcher]], rule)) ||
(helperFound && !_.isEqual(this.rules[this.helpersMap[rule.matcher]], rule))) {
throw new Error('Rule ' + rule.matcher + ' already exists with a different defintion.');
}

// push new rule to this.rules and update rulesMap
this.rules.push(rule);
if (!ruleFound && !helperFound) {
// push new rule to this.rules and update rulesMap
this.rules.push(rule);

if (rule.type === 'pattern') {
this.rulesMap[rule.matcher] = this.rules.length - 1;
} else {
this.helpersMap[rule.matcher] = this.rules.length - 1;
if (rule.type === 'pattern') {
this.rulesMap[rule.matcher] = this.rules.length - 1;
} else {
this.helpersMap[rule.matcher] = this.rules.length - 1;
}
}
}, this);

Expand Down

0 comments on commit 4546052

Please sign in to comment.