1.0.1 • Published 8 years ago
@nlib/replace-stream v1.0.1
@nlib/replace-stream
Transform streams.
Install
npm install @nlib/replace-streamUsage
const {PassThrough} = require('stream');
const {ReplaceStream} = require('@nlib/replace-stream');
const stream = new PassThrough();
const chunks = [];
stream.pipe(new ReplaceStream([
  {
    pattern: 'foo',
    replacement: 'FOO',
  },
  {
    pattern: /ba+r/,
    replacement: 'BAR',
    limit: 2
  },
]))
.on('data', (chunk) => {
  chunks.push(chunk);
})
.once('end', () => {
  console.log(Buffer.concat(chunks).toString());
  // FOOfooBARBARbaaar
});
for (const byte of Buffer.from('foofoobarbaarbaaar')) {
  stream.write(Buffer.from([byte]));
}
stream.end();const {PassThrough} = require('stream');
const {ReplaceStream} = require('@nlib/replace-stream');
const stream = new PassThrough();
const chunks = [];
stream.pipe(new ReplaceStream([
  {
    pattern: '😀',
    replacement: '😎',
  },
]))
.on('data', (chunk) => {
  chunks.push(chunk);
})
.once('end', () => {
  console.log(Buffer.concat(chunks).toString());
  // 😎😁😂
});
for (const byte of Buffer.from('😀😁😂')) {
  stream.write(Buffer.from([byte]));
}
stream.end();LICENSE
MIT