Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: prototype to class syntax #199

Merged
merged 3 commits into from Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;