|
|
@ -9,9 +9,7 @@ |
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function Ruler(compileFn) { |
|
|
|
this.compile = compileFn; // callback to call after each change
|
|
|
|
|
|
|
|
function Ruler() { |
|
|
|
// List of added rules. Each element is:
|
|
|
|
//
|
|
|
|
// {
|
|
|
@ -22,6 +20,13 @@ function Ruler(compileFn) { |
|
|
|
// }
|
|
|
|
//
|
|
|
|
this.rules = []; |
|
|
|
|
|
|
|
// Cached rule chains.
|
|
|
|
//
|
|
|
|
// First level - chain name, '' for default.
|
|
|
|
// Second level - diginal anchor for fast filtering by charcodes.
|
|
|
|
//
|
|
|
|
this.cache = null; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -47,7 +52,7 @@ Ruler.prototype.at = function (name, fn, options) { |
|
|
|
|
|
|
|
this.rules[index].fn = fn; |
|
|
|
this.rules[index].alt = opt.alt || []; |
|
|
|
this.compile(); |
|
|
|
this.cache = null; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -66,7 +71,7 @@ Ruler.prototype.before = function (beforeName, ruleName, fn, options) { |
|
|
|
alt: opt.alt || [] |
|
|
|
}); |
|
|
|
|
|
|
|
this.compile(); |
|
|
|
this.cache = null; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -85,7 +90,7 @@ Ruler.prototype.after = function (afterName, ruleName, fn, options) { |
|
|
|
alt: opt.alt || [] |
|
|
|
}); |
|
|
|
|
|
|
|
this.compile(); |
|
|
|
this.cache = null; |
|
|
|
}; |
|
|
|
|
|
|
|
// Add rule to the end of chain.
|
|
|
@ -100,30 +105,7 @@ Ruler.prototype.push = function (ruleName, fn, options) { |
|
|
|
alt: opt.alt || [] |
|
|
|
}); |
|
|
|
|
|
|
|
this.compile(); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Get rules list as array of functions. By default returns main chain
|
|
|
|
//
|
|
|
|
Ruler.prototype.getRules = function (chainName) { |
|
|
|
var result = []; |
|
|
|
|
|
|
|
if (!chainName) { |
|
|
|
this.rules.forEach(function (rule) { |
|
|
|
if (rule.enabled) { |
|
|
|
result.push(rule.fn); |
|
|
|
} |
|
|
|
}); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
this.rules.forEach(function (rule) { |
|
|
|
if (rule.alt.indexOf(chainName) >= 0 && rule.enabled) { |
|
|
|
result.push(rule.fn); |
|
|
|
} |
|
|
|
}); |
|
|
|
return result; |
|
|
|
this.cache = null; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -151,7 +133,7 @@ Ruler.prototype.enable = function (list, strict) { |
|
|
|
|
|
|
|
}, this); |
|
|
|
|
|
|
|
this.compile(); |
|
|
|
this.cache = null; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -171,8 +153,50 @@ Ruler.prototype.disable = function (list) { |
|
|
|
|
|
|
|
}, this); |
|
|
|
|
|
|
|
this.compile(); |
|
|
|
this.cache = null; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Build rules lookup cache
|
|
|
|
//
|
|
|
|
Ruler.prototype.compile = function () { |
|
|
|
var self = this; |
|
|
|
var chains = [ '' ]; |
|
|
|
|
|
|
|
// collect unique names
|
|
|
|
self.rules.forEach(function (rule) { |
|
|
|
if (!rule.enabled) { return; } |
|
|
|
|
|
|
|
rule.alt.forEach(function (altName) { |
|
|
|
if (chains.indexOf(altName) < 0) { |
|
|
|
chains.push(altName); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
self.cache = {}; |
|
|
|
|
|
|
|
chains.forEach(function (chain) { |
|
|
|
self.cache[chain] = []; |
|
|
|
self.rules.forEach(function (rule) { |
|
|
|
if (!rule.enabled) { return; } |
|
|
|
|
|
|
|
if (chain && rule.alt.indexOf(chain) < 0) { return; } |
|
|
|
|
|
|
|
self.cache[chain].push(rule.fn); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Get rules list as array of functions.
|
|
|
|
//
|
|
|
|
Ruler.prototype.getRules = function (chainName) { |
|
|
|
if (this.cache === null) { |
|
|
|
this.compile(); |
|
|
|
} |
|
|
|
|
|
|
|
return this.cache[chainName]; |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = Ruler; |
|
|
|