Skip to content

Commit

Permalink
refactor(pattern): prototype to class syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Apr 27, 2020
1 parent 959cc2a commit ec14aed
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions lib/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ const escapeRegExp = require('./escape_regexp');

const rParam = /([:*])([\w?]*)?/g;

function Pattern(rule) {
if (rule instanceof Pattern) {
return rule;
} else if (typeof rule === 'function') {
this.match = rule;
} else if (rule instanceof RegExp) {
this.match = regexFilter(rule);
} else if (typeof rule === 'string') {
this.match = stringFilter(rule);
} else {
throw new TypeError('rule must be a function, a string or a regular expression.');
class Pattern {
constructor(rule) {
if (rule instanceof Pattern) {
return rule;
} else if (typeof rule === 'function') {
this.match = rule;
} else if (rule instanceof RegExp) {
this.match = regexFilter(rule);
} else if (typeof rule === 'string') {
this.match = stringFilter(rule);
} else {
throw new TypeError('rule must be a function, a string or a regular expression.');
}
}
}

Pattern.prototype.test = function(str) {
return Boolean(this.match(str));
};
test(str) {
return Boolean(this.match(str));
}
}

function regexFilter(rule) {
return str => str.match(rule);
Expand Down

0 comments on commit ec14aed

Please sign in to comment.