Skip to content

Commit

Permalink
Merge pull request #199 from curbengh/cache-class
Browse files Browse the repository at this point in the history
refactor: prototype to class syntax
  • Loading branch information
SukkaW committed Apr 27, 2020
2 parents 20ab0be + 4de28ea commit f5d510a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 77 deletions.
35 changes: 17 additions & 18 deletions lib/cache_stream.js
Expand Up @@ -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;
32 changes: 17 additions & 15 deletions lib/pattern.js
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
81 changes: 37 additions & 44 deletions lib/permalink.js
Expand Up @@ -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;

0 comments on commit f5d510a

Please sign in to comment.