mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-03-16 22:20:23 +00:00
Added regex instance caching to resolver
This commit is contained in:
parent
2097c638ab
commit
3f1396cb35
3 changed files with 38 additions and 39 deletions
|
|
@ -203,27 +203,29 @@
|
|||
}
|
||||
};
|
||||
var bindingsResolver = {
|
||||
cache : { },
|
||||
cache : {},
|
||||
regex : {},
|
||||
|
||||
compare : function ( binding, topic ) {
|
||||
var result = (this.cache[topic] && this.cache[topic][binding]);
|
||||
var pattern, rgx, prev, result = (this.cache[topic] && this.cache[topic][binding]);
|
||||
if(typeof result !== "undefined") {
|
||||
return result;
|
||||
}
|
||||
var prev;
|
||||
var pattern = "^" + _.map(binding.split('.'), function(segment) {
|
||||
var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b";
|
||||
if(segment === "#") {
|
||||
res += "[A-Z,a-z,0-9,\\.]*"
|
||||
} else if (segment === "*") {
|
||||
res += "[A-Z,a-z,0-9]+"
|
||||
} else {
|
||||
res += segment;
|
||||
}
|
||||
prev = segment;
|
||||
return res;
|
||||
} ).join('') + "$";
|
||||
var rgx = new RegExp( pattern );
|
||||
if(!(rgx = this.regex[binding])) {
|
||||
pattern = "^" + _.map(binding.split('.'), function(segment) {
|
||||
var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b";
|
||||
if(segment === "#") {
|
||||
res += "[A-Z,a-z,0-9,\\.]*"
|
||||
} else if (segment === "*") {
|
||||
res += "[A-Z,a-z,0-9]+"
|
||||
} else {
|
||||
res += segment;
|
||||
}
|
||||
prev = segment;
|
||||
return res;
|
||||
} ).join('') + "$";
|
||||
rgx = this.regex[binding] = new RegExp( pattern );
|
||||
}
|
||||
this.cache[topic] = this.cache[topic] || {};
|
||||
this.cache[topic][binding] = result = rgx.test( topic );
|
||||
return result;
|
||||
|
|
|
|||
8
lib/postal.min.js
vendored
8
lib/postal.min.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -1,25 +1,27 @@
|
|||
var bindingsResolver = {
|
||||
cache : { },
|
||||
cache : {},
|
||||
regex : {},
|
||||
|
||||
compare : function ( binding, topic ) {
|
||||
var result = (this.cache[topic] && this.cache[topic][binding]);
|
||||
var pattern, rgx, prev, result = (this.cache[topic] && this.cache[topic][binding]);
|
||||
if(typeof result !== "undefined") {
|
||||
return result;
|
||||
}
|
||||
var prev;
|
||||
var pattern = "^" + _.map(binding.split('.'), function(segment) {
|
||||
var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b";
|
||||
if(segment === "#") {
|
||||
res += "[A-Z,a-z,0-9,\\.]*"
|
||||
} else if (segment === "*") {
|
||||
res += "[A-Z,a-z,0-9]+"
|
||||
} else {
|
||||
res += segment;
|
||||
}
|
||||
prev = segment;
|
||||
return res;
|
||||
} ).join('') + "$";
|
||||
var rgx = new RegExp( pattern );
|
||||
if(!(rgx = this.regex[binding])) {
|
||||
pattern = "^" + _.map(binding.split('.'), function(segment) {
|
||||
var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b";
|
||||
if(segment === "#") {
|
||||
res += "[A-Z,a-z,0-9,\\.]*"
|
||||
} else if (segment === "*") {
|
||||
res += "[A-Z,a-z,0-9]+"
|
||||
} else {
|
||||
res += segment;
|
||||
}
|
||||
prev = segment;
|
||||
return res;
|
||||
} ).join('') + "$";
|
||||
rgx = this.regex[binding] = new RegExp( pattern );
|
||||
}
|
||||
this.cache[topic] = this.cache[topic] || {};
|
||||
this.cache[topic][binding] = result = rgx.test( topic );
|
||||
return result;
|
||||
|
|
@ -27,5 +29,6 @@ var bindingsResolver = {
|
|||
|
||||
reset : function () {
|
||||
this.cache = {};
|
||||
this.regex = {};
|
||||
}
|
||||
};
|
||||
Loading…
Reference in a new issue