Skip to content

Commit

Permalink
Merge pull request #7 from StevenACoffman/master
Browse files Browse the repository at this point in the history
Minor cleaning, avoid confusing with Function.prototype.call()
  • Loading branch information
FGRibreau committed Dec 31, 2015
2 parents eb309b3 + 08314a4 commit 5932fbe
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions match.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const EXTRACT_PATTERN_AND_FLAGS = /\/(.*)\/(.*)/;
function MissingCatchAllPattern() {
Error.call(this, 'Missing when() catch-all pattern as last match argument, add [when()]: void 0');
if (!('stack' in this)){
this.stack = (new Error).stack;
this.stack = (new Error()).stack;
}
}

Expand Down Expand Up @@ -46,12 +46,8 @@ function match(/* args... */){
matchers.push(when.unserialize(_catchAllSymbol, obj[_catchAllSymbol]));

const calculateResult = function(input){
for (let i = 0, iM = matchers.length; i < iM; i++) { // old school #perf
const matcher = matchers[i];
if(matcher.match(input)){
return typeof matcher.call === 'function' ? matcher.call(input): matcher.call;
}
}
const matched = matchers.find((matcher) => matcher.match(input));
return typeof matched.result === 'function' ? matched.result(input) : matched.result;
};

return args.length === 2 ? calculateResult(args[0]) : calculateResult;
Expand Down Expand Up @@ -161,23 +157,22 @@ when.unserialize = function(serializedKey, value){
if(serializedKey === _catchAllSymbol){
return {
match: _true,
call: value,
result: value,
position: Infinity
};
}


// const {position, matcherConfiguration} = _unserialize(serializedKey);
const deserialized = _unserialize(serializedKey);
const matcherConfiguration = deserialized[1];
const position = deserialized[0];

return {
match: _match(matcherConfiguration),
call: value,
result: value,
position: position
};
}
};

module.exports = {
match,
Expand Down

0 comments on commit 5932fbe

Please sign in to comment.