Package Exports
- restream
- restream/build
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (restream) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
restream
Regular expression detection implemented as a Transform steam; and regex-based buffer replacement stream to replace incoming data on-the-fly.
yarn add -E restreamTable of Contents
API
The package contains the default restream and Replaceable functions.
import restream, { Replaceable } from 'restream'restream(
regex: RegExp,
): Transform
Create a Transform stream which will buffer incoming data and push regex results when matches can be made, i.e. when regex.exec returns non-null value. When the g flag is added to the regex, multiple matches will be detected.
/** yarn examples/restream.js **/
import restream from 'restream'
import { createReadable, createWritable } from './lib'
(async () => {
try {
const rs = createReadable('test-string-{12345}-{67890}')
const stream = restream(/{(\d+)}/g) // create a transform stream
rs.pipe(stream)
const { data, ws } = createWritable()
stream.pipe(ws)
ws.once('finish', () => {
console.log(data)
})
} catch (err) {
console.error(err)
}
})()[ [ '{12345}',
'12345',
index: 12,
input: 'test-string-{12345}-{67890}' ],
[ '{67890}',
'67890',
index: 20,
input: 'test-string-{12345}-{67890}' ] ]Replaceable
A Replaceable transform stream can be used to transform data according to a single or multiple rules.
Rule Type
Replaceable uses rules to determine how to transform data. Below is the description of the Rule type.
| Property | Type | Description |
|---|---|---|
| re | RegExp |
A regular expression. |
| replacement (1) | string |
Replacement as a string. Here and below, it will be passed to the string.replace(re, replacement) native JavaScript method. |
| replacement (2) | function |
Replacement as a sync function. See MDN for more documentation on how the replacer function should be implemented. |
| replacement (3) | async function |
An asynchronous function to get replacements. The stream won't push any data until the replacer's promise is resolved. Due to implementation details, the regex will have to be run against incoming chunks twice, therefore it might be not ideal for heavy-load applications with many matches. |
constructor(
rule: Rule|Rules[],
): Replaceable
Create a Transform stream which will make data available when an incoming chunk has been updated according to the specified rule or rules.
Matches can be replaced using a string, function or async function. When multiple rules are passed as an array, the string will be replaced multiple times if the latter rules also modify the data.
/** yarn examples/Replaceable.js */
import Catchment from 'catchment'
import { Replaceable } from 'restream'
import { createReadable } from './lib'
(async () => {
try {
const dateRule = {
re: /%DATE%/g,
replacement: new Date().toLocaleString(),
}
const emRule = {
re: /__(.+?)__/g,
replacement(match, p1) {
return `<em>${p1}</em>`
},
}
const authorRule = {
re: /^%AUTHOR: (.+?)%$/mg,
async replacement(match, p1) {
await new Promise(r => setTimeout(r,100))
return `Author: <strong>${p1}</strong>`
},
}
const replaceable = new Replaceable([
dateRule,
emRule,
authorRule,
])
const rs = createReadable(`
Hello __Fred__, your username is __fred__ and you have __5__ stars.
%AUTHOR: John%
on __%DATE%__
`)
rs.pipe(replaceable)
const { promise } = new Catchment({
rs: replaceable,
})
const res = await promise
console.log(res)
} catch (err) {
console.error(err)
}
})()Output:
Hello <em>Fred</em>, your username is <em>fred</em> and you have <em>5</em> stars.
Author: <strong>John</strong>
on <em>2018-6-20 19:59:18</em>DEPRECATED_replaceStream(
rule: Rule|Rule[],
): Transform
Used to create a Replaceable stream. Deprecated in favour of the class constructor (see above).
/** yarn examples/replace-stream.js */
import Catchment from 'catchment'
import { replaceStream } from 'restream'
import { createReadable } from './lib'
(async () => {
try {
const stream = replaceStream([{
re: /{{ user }}/,
replacement: 'fred',
}, {
re: /{{ name }}/g,
replacement: 'Fred',
}, {
re: /{{ stars }}/,
replacement: '5',
}])
const rs = createReadable('Hello {{ name }}, your username is {{ user }} and you have {{ stars }} stars')
rs.pipe(stream)
const { promise } = new Catchment({
rs: stream,
})
const res = await promise
console.log(res)
} catch (err) {
console.error(err)
}
})()Output:
Hello Fred, your username is fred and you have 5 stars(c) Art Deco Code 2018