Skip to content

Commit f5d510a

Browse files
authored
Merge pull request #199 from curbengh/cache-class
refactor: prototype to class syntax
2 parents 20ab0be + 4de28ea commit f5d510a

File tree

3 files changed

+71
-77
lines changed

3 files changed

+71
-77
lines changed

lib/cache_stream.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22

33
const { Transform } = require('stream');
44

5-
function CacheStream() {
6-
Transform.call(this);
7-
8-
this._cache = [];
5+
class CacheStream extends Transform {
6+
constructor() {
7+
super();
8+
9+
this._cache = [];
10+
}
11+
12+
_transform(chunk, enc, callback) {
13+
const buf = chunk instanceof Buffer ? chunk : Buffer.from(chunk, enc);
14+
this._cache.push(buf);
15+
this.push(buf);
16+
callback();
17+
}
18+
19+
getCache() {
20+
return Buffer.concat(this._cache);
21+
}
922
}
1023

11-
require('util').inherits(CacheStream, Transform);
12-
13-
CacheStream.prototype._transform = function(chunk, enc, callback) {
14-
const buf = chunk instanceof Buffer ? chunk : Buffer.from(chunk, enc);
15-
16-
this._cache.push(buf);
17-
this.push(buf);
18-
callback();
19-
};
20-
21-
CacheStream.prototype.getCache = function() {
22-
return Buffer.concat(this._cache);
23-
};
24-
2524
module.exports = CacheStream;

lib/pattern.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@ const escapeRegExp = require('./escape_regexp');
44

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

7-
function Pattern(rule) {
8-
if (rule instanceof Pattern) {
9-
return rule;
10-
} else if (typeof rule === 'function') {
11-
this.match = rule;
12-
} else if (rule instanceof RegExp) {
13-
this.match = regexFilter(rule);
14-
} else if (typeof rule === 'string') {
15-
this.match = stringFilter(rule);
16-
} else {
17-
throw new TypeError('rule must be a function, a string or a regular expression.');
7+
class Pattern {
8+
constructor(rule) {
9+
if (rule instanceof Pattern) {
10+
return rule;
11+
} else if (typeof rule === 'function') {
12+
this.match = rule;
13+
} else if (rule instanceof RegExp) {
14+
this.match = regexFilter(rule);
15+
} else if (typeof rule === 'string') {
16+
this.match = stringFilter(rule);
17+
} else {
18+
throw new TypeError('rule must be a function, a string or a regular expression.');
19+
}
1820
}
19-
}
2021

21-
Pattern.prototype.test = function(str) {
22-
return Boolean(this.match(str));
23-
};
22+
test(str) {
23+
return Boolean(this.match(str));
24+
}
25+
}
2426

2527
function regexFilter(rule) {
2628
return str => str.match(rule);

lib/permalink.js

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,48 @@ const escapeRegExp = require('./escape_regexp');
44

55
const rParam = /:(\w*[^_\W])/g;
66

7-
function Permalink(rule, options) {
8-
if (!rule) throw new TypeError('rule is required!');
9-
options = options || {};
10-
11-
const segments = options.segments || {};
12-
const params = [];
13-
14-
const regex = escapeRegExp(rule)
15-
.replace(rParam, (match, name) => {
16-
params.push(name);
17-
18-
if (Object.prototype.hasOwnProperty.call(segments, name)) {
19-
const segment = segments[name];
20-
21-
if (segment instanceof RegExp) {
22-
return segment.source;
7+
class Permalink {
8+
constructor(rule, options) {
9+
if (!rule) { throw new TypeError('rule is required!'); }
10+
options = options || {};
11+
const segments = options.segments || {};
12+
const params = [];
13+
const regex = escapeRegExp(rule)
14+
.replace(rParam, (match, name) => {
15+
params.push(name);
16+
if (Object.prototype.hasOwnProperty.call(segments, name)) {
17+
const segment = segments[name];
18+
if (segment instanceof RegExp) {
19+
return segment.source;
20+
}
21+
return segment;
2322
}
23+
return '(.+?)';
24+
});
25+
this.rule = rule;
26+
this.regex = new RegExp(`^${regex}$`);
27+
this.params = params;
28+
}
2429

25-
return segment;
26-
}
27-
28-
return '(.+?)';
29-
});
30-
31-
this.rule = rule;
32-
this.regex = new RegExp(`^${regex}$`);
33-
this.params = params;
34-
}
35-
36-
Permalink.prototype.test = function(str) {
37-
return this.regex.test(str);
38-
};
39-
40-
Permalink.prototype.parse = function(str) {
41-
const match = str.match(this.regex);
42-
const { params } = this;
43-
const result = {};
44-
45-
if (!match) return;
30+
test(str) {
31+
return this.regex.test(str);
32+
}
4633

47-
for (let i = 1, len = match.length; i < len; i++) {
48-
result[params[i - 1]] = match[i];
34+
parse(str) {
35+
const match = str.match(this.regex);
36+
const { params } = this;
37+
const result = {};
38+
if (!match) { return; }
39+
for (let i = 1, len = match.length; i < len; i++) {
40+
result[params[i - 1]] = match[i];
41+
}
42+
return result;
4943
}
5044

51-
return result;
52-
};
45+
stringify(data) {
46+
return this.rule.replace(rParam, (match, name) => data[name]);
47+
}
48+
}
5349

54-
Permalink.prototype.stringify = function(data) {
55-
return this.rule.replace(rParam, (match, name) => data[name]);
56-
};
5750

5851
module.exports = Permalink;

0 commit comments

Comments
 (0)