From 959cc2a57cc35233772523d45507d045045b3b09 Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Mon, 27 Apr 2020 09:29:04 +0100 Subject: [PATCH 1/3] refactor(cache_stream): prototype to class syntax --- lib/cache_stream.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/cache_stream.js b/lib/cache_stream.js index 54a38bc0..ca133aa0 100644 --- a/lib/cache_stream.js +++ b/lib/cache_stream.js @@ -2,24 +2,23 @@ const { Transform } = require('stream'); -function CacheStream() { - Transform.call(this); - - this._cache = []; +class CacheStream extends Transform { + constructor() { + super(); + + this._cache = []; + } + + _transform(chunk, enc, callback) { + const buf = chunk instanceof Buffer ? chunk : Buffer.from(chunk, enc); + this._cache.push(buf); + this.push(buf); + callback(); + } + + getCache() { + return Buffer.concat(this._cache); + } } -require('util').inherits(CacheStream, Transform); - -CacheStream.prototype._transform = function(chunk, enc, callback) { - const buf = chunk instanceof Buffer ? chunk : Buffer.from(chunk, enc); - - this._cache.push(buf); - this.push(buf); - callback(); -}; - -CacheStream.prototype.getCache = function() { - return Buffer.concat(this._cache); -}; - module.exports = CacheStream; 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 2/3] 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); From 4de28ea7204ceee8f2a6148a279f6d129f4b2d59 Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Mon, 27 Apr 2020 09:36:51 +0100 Subject: [PATCH 3/3] refactor(permalink): prototype to class syntax --- lib/permalink.js | 81 ++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 44 deletions(-) diff --git a/lib/permalink.js b/lib/permalink.js index ad6f997b..70617605 100644 --- a/lib/permalink.js +++ b/lib/permalink.js @@ -4,55 +4,48 @@ const escapeRegExp = require('./escape_regexp'); const rParam = /:(\w*[^_\W])/g; -function Permalink(rule, options) { - if (!rule) throw new TypeError('rule is required!'); - options = options || {}; - - const segments = options.segments || {}; - const params = []; - - const regex = escapeRegExp(rule) - .replace(rParam, (match, name) => { - params.push(name); - - if (Object.prototype.hasOwnProperty.call(segments, name)) { - const segment = segments[name]; - - if (segment instanceof RegExp) { - return segment.source; +class Permalink { + constructor(rule, options) { + if (!rule) { throw new TypeError('rule is required!'); } + options = options || {}; + const segments = options.segments || {}; + const params = []; + const regex = escapeRegExp(rule) + .replace(rParam, (match, name) => { + params.push(name); + if (Object.prototype.hasOwnProperty.call(segments, name)) { + const segment = segments[name]; + if (segment instanceof RegExp) { + return segment.source; + } + return segment; } + return '(.+?)'; + }); + this.rule = rule; + this.regex = new RegExp(`^${regex}$`); + this.params = params; + } - return segment; - } - - return '(.+?)'; - }); - - this.rule = rule; - this.regex = new RegExp(`^${regex}$`); - this.params = params; -} - -Permalink.prototype.test = function(str) { - return this.regex.test(str); -}; - -Permalink.prototype.parse = function(str) { - const match = str.match(this.regex); - const { params } = this; - const result = {}; - - if (!match) return; + test(str) { + return this.regex.test(str); + } - for (let i = 1, len = match.length; i < len; i++) { - result[params[i - 1]] = match[i]; + parse(str) { + const match = str.match(this.regex); + const { params } = this; + const result = {}; + if (!match) { return; } + for (let i = 1, len = match.length; i < len; i++) { + result[params[i - 1]] = match[i]; + } + return result; } - return result; -}; + stringify(data) { + return this.rule.replace(rParam, (match, name) => data[name]); + } +} -Permalink.prototype.stringify = function(data) { - return this.rule.replace(rParam, (match, name) => data[name]); -}; module.exports = Permalink;