UNPKG

3.54 kBMarkdownView Raw
1# restream
2
3[![npm version](https://badge.fury.io/js/restream.svg)](https://badge.fury.io/js/restream)
4
5```
6yarn add -E restream
7```
8
9Regular expression detection implemented as a transform Steam and regex-based buffer replacement stream to replace streamed data on-the-fly.
10
11```js
12import restream, { replaceStream } from 'restream'
13```
14
15## restream(regex: RegExp) => Transform
16
17Create a transform stream which will buffer incoming data and push regex results
18when matches can be made, i.e. when `regex.exec` returns non-null value. You will probably want to
19set the `g` flag on regexes most of the time.
20
21```js
22/** yarn examples/restream.js **/
23import restream from 'restream'
24import { createReadable, createWritable } from './lib'
25
26(async () => {
27 try {
28 const rs = createReadable('test-string-{12345}-{67890}')
29
30 const stream = restream(/{(\d+)}/g) // create a transform stream
31 rs.pipe(stream)
32
33 const { data, ws } = createWritable()
34 stream.pipe(ws)
35
36 ws.once('finish', () => {
37 console.log(data)
38 })
39 } catch (err) {
40 console.error(err)
41 }
42})()
43```
44
45```js
46[ [ '{12345}',
47 '12345',
48 index: 12,
49 input: 'test-string-{12345}-{67890}' ],
50 [ '{67890}',
51 '67890',
52 index: 20,
53 input: 'test-string-{12345}-{67890}' ] ]
54```
55
56## replaceStream({re:RegExp, replacement:string }[]) => Transform
57
58Creates a `Transform` stream which will make data available
59when an incoming chunk has been updated according to the `regex` input, which can
60be either a single regex object (`{ re: /regex/, replacement: 'hello-world' }`),
61or an array of such objects. A `replacement` can be either a string, or a function,
62which will be called by `str.replace`
63
64```js
65/** yarn examples/replace-stream.js */
66import Catchment from 'catchment'
67import { replaceStream } from 'restream'
68import { createReadable } from './lib'
69
70(async () => {
71 try {
72 const stream = replaceStream([{
73 re: /{{ user }}/,
74 replacement: 'fred',
75 }, {
76 re: /{{ name }}/g,
77 replacement: 'Fred',
78 }, {
79 re: /{{ stars }}/,
80 replacement: '5',
81 }])
82
83 const rs = createReadable('Hello {{ name }}, your username is {{ user }} and you have {{ stars }} stars')
84 rs.pipe(stream)
85 const { promise } = new Catchment({
86 rs: stream,
87 })
88 const res = await promise
89 console.log(res)
90 } catch (err) {
91 console.error(err)
92 }
93})()
94```
95
96Output:
97
98```fs
99Hello Fred, your username is fred and you have 5 stars
100```
101
102## replaceStream({re:RegExp, replacement: function(match, ...params) }[]) => Transform
103
104You can replace matches using a function. See [MDN][2] for more documentation.
105
106```js
107/** yarn examples/replace-function.js */
108import { replaceStream } from '../src'
109import Catchment from 'catchment'
110import { createReadable } from './lib'
111
112(async () => {
113 try {
114 const stream = replaceStream([{
115 re: /__(\S+)__/g,
116 replacement(match, p1) {
117 return `<em>${p1}</em>`
118 },
119 }])
120 const rs = createReadable('Hello __Fred__, your username is __fred__ and you have __5__ stars.')
121 rs.pipe(stream)
122
123 const { promise } = new Catchment({
124 rs: stream,
125 })
126 const res = await promise
127
128 console.log(res)
129 } catch (err) {
130 console.error(err)
131 }
132})()
133```
134
135Output:
136
137```fs
138Hello <em>Fred</em>, your username is <em>fred</em> and you have <em>5</em> stars
139```
140---
141
142(c) [Art Deco Code][1] 2018
143
144[1]: https://artdeco.bz
145[2]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter