From ec14aed6164c27349d8f0fb1d3d83bbfbe38d741 Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Mon, 27 Apr 2020 09:34:18 +0100 Subject: [PATCH] refactor(pattern): prototype to class syntax --- lib/pattern.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/pattern.js b/lib/pattern.js index c2eecf8c..1b611e0a 100644 --- a/lib/pattern.js +++ b/lib/pattern.js @@ -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);