Comparing version 2.0.2 to 2.0.3
@@ -1,13 +0,19 @@ | ||
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = createRegexTransformStream;exports.replaceStream = replaceStream;var _stream = require("stream"); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = createRegexTransformStream; | ||
exports.replaceStream = replaceStream; | ||
var _stream = require("stream"); | ||
var _lib = require("./lib"); | ||
/** | ||
* Create a Transform stream which will maintain a buffer with data received from a Readable stream and write data when the buffer can be matched against the regex. | ||
* It will push the whole match object (or objects when the g flag is used) returned by `/regex/.exec(buffer)`. | ||
* @param {RegExp} regex Regular expression to execute | ||
* @return {Transform} A transform stream. | ||
*/ | ||
* Create a Transform stream which will maintain a buffer with data received from a Readable stream and write data when the buffer can be matched against the regex. It will push the whole match object (or objects when the g flag is used) returned by `/regex/.exec(buffer)`. | ||
* @param {RegExp} regex Regular expression to execute | ||
*/ | ||
function createRegexTransformStream(regex) { | ||
let buffer = ''; | ||
const ts = new _stream.Transform({ | ||
@@ -17,7 +23,6 @@ transform(chunk, encoding, next) { | ||
let match; | ||
buffer += chunk.toString(); | ||
// thx stream-snitch | ||
buffer += chunk.toString(); // thx stream-snitch | ||
// https://github.com/dmotz/stream-snitch/blob/master/index.js#L52 | ||
// eslint-disable-next-line no-cond-assign | ||
while (match = regex.exec(buffer)) { | ||
@@ -28,33 +33,39 @@ ts.push(match); | ||
} | ||
if (lastMatch) { | ||
buffer = buffer.slice(lastMatch.index + lastMatch[0].length); | ||
} | ||
next(); | ||
}, | ||
objectMode: true }); | ||
objectMode: true | ||
}); | ||
return ts; | ||
} | ||
/** | ||
* @typedef {Object} RegexObject | ||
* @property {RegExp} re regular expression to match against | ||
* @property {string} replacement a replacement string | ||
*/ | ||
* Create a replacement stream, which will push data when it's done replacing each incoming chunk. The return is a transform stream which writes strings. If the replacement is passed as a function, it will work in the same way as the replacer for `string.replace` method (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace), taking the `match` as the first argument, and matched `p1`, `p2`, _etc_ parameters as following arguments. | ||
* @param {RegexObject|RegexObject[]} rules A single replacement rule, or multiple rules. | ||
* @example | ||
* | ||
* // markdown __ to html emphasise implementation | ||
* const stream = replaceStream({ | ||
* re: /__(\S+)__/g, | ||
* replacement(match, p1) { | ||
* return `<em>${p1}</em>` | ||
* }, | ||
* }) | ||
*/ | ||
/** | ||
* Create a replacement stream, which will push data when it's done replacing each incoming chunk. | ||
* @param {RegexObject|RegexObject[]} regex An object or array of objects | ||
* @returns {Transform} A transform stream, which writes strings | ||
*/ | ||
function replaceStream(regex) { | ||
const re = Array.isArray(regex) ? regex : [regex]; | ||
function replaceStream(rules) { | ||
const re = Array.isArray(rules) ? rules : [rules]; | ||
const fr = re.filter(_lib.checkRegexObject); | ||
const ts = new _stream.Transform({ | ||
transform(chunk, _, next) { | ||
let string = `${chunk}`; | ||
fr.forEach(({ re, replacement }) => { | ||
fr.forEach(({ | ||
re, | ||
replacement | ||
}) => { | ||
string = string.replace(re, replacement); | ||
@@ -64,6 +75,13 @@ }); | ||
next(); | ||
} }); | ||
} | ||
}); | ||
return ts; | ||
} | ||
} | ||
/** | ||
* @typedef {(substring: string, ...args: any[]) => string} Replacer | ||
* | ||
* @typedef {Object} RegexObject | ||
* @property {RegExp} re Regular expression to match against | ||
* @property {string|Replacer} replacement A replacement string, or a replacement string function. | ||
*/ |
@@ -1,5 +0,13 @@ | ||
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.checkRegexObject = checkRegexObject;function checkRegexObject(reObject) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.checkRegexObject = checkRegexObject; | ||
function checkRegexObject(reObject) { | ||
if (typeof reObject != 'object') { | ||
return false; | ||
} | ||
const hasRe = reObject.re instanceof RegExp; | ||
@@ -6,0 +14,0 @@ const type = ['string', 'function'].indexOf(typeof reObject.replacement) != -1; |
## 15 June 2018 | ||
### 2.0.3 | ||
- [package] `test-build` script | ||
- [test] move `test-examples` to its own folder | ||
- [build] build without `retainLines` | ||
### 2.0.2 | ||
@@ -4,0 +10,0 @@ |
{ | ||
"name": "restream", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Regular Expression Detection & Replacement streams", | ||
"main": "build", | ||
"scripts": { | ||
"t": "zoroaster -b", | ||
"test": "zoroaster test/spec -b", | ||
"t": "zoroaster --babel", | ||
"test-build": "BABEL_ENV=test-build yarn test", | ||
"test-examples/": "ZOROASTER_TIMEOUT=5000 zoroaster test-examples/spec -b", | ||
"build": "babel src --out-dir build", | ||
@@ -46,2 +48,3 @@ "e": "node examples/run", | ||
"@babel/register": "7.0.0-beta.51", | ||
"babel-plugin-transform-rename-import": "2.2.0", | ||
"catchment": "2.0.1", | ||
@@ -48,0 +51,0 @@ "documentary": "1.0.1", |
14370
85
11