hexo-util
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -5,22 +5,21 @@ 'use strict'; | ||
function CacheStream() { | ||
Transform.call(this); | ||
class CacheStream extends Transform { | ||
constructor() { | ||
super(); | ||
this._cache = []; | ||
} | ||
this._cache = []; | ||
} | ||
require('util').inherits(CacheStream, Transform); | ||
_transform(chunk, enc, callback) { | ||
const buf = chunk instanceof Buffer ? chunk : Buffer.from(chunk, enc); | ||
this._cache.push(buf); | ||
this.push(buf); | ||
callback(); | ||
} | ||
CacheStream.prototype._transform = function(chunk, enc, callback) { | ||
const buf = chunk instanceof Buffer ? chunk : Buffer.from(chunk, enc); | ||
getCache() { | ||
return Buffer.concat(this._cache); | ||
} | ||
} | ||
this._cache.push(buf); | ||
this.push(buf); | ||
callback(); | ||
}; | ||
CacheStream.prototype.getCache = function() { | ||
return Buffer.concat(this._cache); | ||
}; | ||
module.exports = CacheStream; |
'use strict'; | ||
const { Transform } = require('stream'); | ||
const crypto = require('crypto'); | ||
@@ -12,23 +11,2 @@ | ||
/** | ||
* @deprecated | ||
* createHash() is stream class. | ||
*/ | ||
function HashStream() { | ||
Transform.call(this); | ||
this._hash = createSha1Hash(); | ||
} | ||
require('util').inherits(HashStream, Transform); | ||
HashStream.prototype._transform = function(chunk, enc, callback) { | ||
this._hash.update(chunk); | ||
callback(); | ||
}; | ||
HashStream.prototype._flush = function(callback) { | ||
this.push(this._hash.digest()); | ||
callback(); | ||
}; | ||
exports.hash = content => { | ||
@@ -40,3 +18,2 @@ const hash = createSha1Hash(); | ||
exports.HashStream = HashStream; | ||
exports.createSha1Hash = createSha1Hash; |
@@ -7,20 +7,22 @@ 'use strict'; | ||
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.'); | ||
} | ||
} | ||
test(str) { | ||
return Boolean(this.match(str)); | ||
} | ||
} | ||
Pattern.prototype.test = function(str) { | ||
return Boolean(this.match(str)); | ||
}; | ||
function regexFilter(rule) { | ||
@@ -27,0 +29,0 @@ return str => str.match(rule); |
@@ -7,53 +7,46 @@ 'use strict'; | ||
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; | ||
} | ||
test(str) { | ||
return this.regex.test(str); | ||
} | ||
return '(.+?)'; | ||
}); | ||
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; | ||
} | ||
this.rule = rule; | ||
this.regex = new RegExp(`^${regex}$`); | ||
this.params = params; | ||
stringify(data) { | ||
return this.rule.replace(rParam, (match, name) => data[name]); | ||
} | ||
} | ||
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; | ||
for (let i = 1, len = match.length; i < len; i++) { | ||
result[params[i - 1]] = match[i]; | ||
} | ||
return result; | ||
}; | ||
Permalink.prototype.stringify = function(data) { | ||
return this.rule.replace(rParam, (match, name) => data[name]); | ||
}; | ||
module.exports = Permalink; |
{ | ||
"name": "hexo-util", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Utilities for Hexo.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index", |
@@ -262,17 +262,2 @@ # hexo-util | ||
### HashStream() | ||
**\[deprecated\]** use [`createSha1Hash()`](#createsha1hash). | ||
Generates SHA1 hash with a transform stream. | ||
``` js | ||
var stream = new HashStream(); | ||
fs.createReadStream('/path/to/file') | ||
.pipe(stream) | ||
.on('finish', function(){ | ||
console.log(stream.read()); | ||
}); | ||
``` | ||
### highlight(str, [options]) | ||
@@ -279,0 +264,0 @@ |
79018
1327
725