1 | # restream
|
2 |
|
3 | [![npm version](https://badge.fury.io/js/restream.svg)](https://badge.fury.io/js/restream)
|
4 |
|
5 | ```
|
6 | yarn add -E restream
|
7 | ```
|
8 |
|
9 | Regular expression detection implemented as a transform Steam and regex-based buffer replacement stream to replace streamed data on-the-fly.
|
10 |
|
11 | ```js
|
12 | import restream, { replaceStream } from 'restream'
|
13 | ```
|
14 |
|
15 | ## restream(regex: RegExp) => Transform
|
16 |
|
17 | Create a transform stream which will buffer incoming data and push regex results
|
18 | when matches can be made, i.e. when `regex.exec` returns non-null value. You will probably want to
|
19 | set the `g` flag on regexes most of the time.
|
20 |
|
21 | ```js
|
22 | /** yarn examples/restream.js **/
|
23 | import restream from 'restream'
|
24 | import { 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 |
|
58 | Creates a `Transform` stream which will make data available
|
59 | when an incoming chunk has been updated according to the `regex` input, which can
|
60 | be either a single regex object (`{ re: /regex/, replacement: 'hello-world' }`),
|
61 | or an array of such objects. A `replacement` can be either a string, or a function,
|
62 | which will be called by `str.replace`
|
63 |
|
64 | ```js
|
65 | /** yarn examples/replace-stream.js */
|
66 | import Catchment from 'catchment'
|
67 | import { replaceStream } from 'restream'
|
68 | import { 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 |
|
96 | Output:
|
97 |
|
98 | ```fs
|
99 | Hello Fred, your username is fred and you have 5 stars
|
100 | ```
|
101 |
|
102 | ## replaceStream({re:RegExp, replacement: function(match, ...params) }[]) => Transform
|
103 |
|
104 | You can replace matches using a function. See [MDN][2] for more documentation.
|
105 |
|
106 | ```js
|
107 | /** yarn examples/replace-function.js */
|
108 | import { replaceStream } from '../src'
|
109 | import Catchment from 'catchment'
|
110 | import { 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 |
|
135 | Output:
|
136 |
|
137 | ```fs
|
138 | Hello <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
|