Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

replacestream

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

replacestream - npm Package Compare versions

Comparing version 0.0.6 to 0.1.0

17

examples/simple.js

@@ -1,2 +0,2 @@

var replaceStream = require('replacestream')
var replaceStream = require('..')
, fs = require('fs')

@@ -28,1 +28,16 @@ , path = require('path');

*/
var words = ['Awesome', 'Good', 'Super', 'Joyous'];
function replaceFn(match) {
return words.shift();
}
fs.createReadStream(path.join(__dirname, 'happybirthday.txt'))
.pipe(replaceStream('Happy', replaceFn))
.pipe(process.stdout);
/*
Awesome birthday to you!
Good birthday to you!
Super birthday to dear Liza!
Joyous birthday to you!
*/

13

index.js

@@ -12,2 +12,9 @@ var through = require('through');

var replaceFn = replace;
if (typeof replace !== 'function') {
replaceFn = function () {
return replace;
};
}
var match = permuteMatch(search, options);

@@ -58,3 +65,3 @@

totalMatches++;
dataToAppend += replace;
dataToAppend += replaceFn(match);

@@ -72,6 +79,6 @@ return dataToAppend;

return rewritten;
}
}
dataToQueue = rewritten +tail + remaining;
}
}

@@ -78,0 +85,0 @@ tail = '';

{
"name": "replacestream",
"version": "0.0.6",
"version": "0.1.0",
"description": "A node.js through stream that does basic streaming text search and replace and is chunk boundary friendly",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -70,2 +70,25 @@ # replacestream

And you can also pass in a replacement function which will get called for each
replacement:
// Replace all the instances of 'birthday' with 'earthday'
var words = ['Awesome', 'Good', 'Super', 'Joyous'];
function replaceFn(match) {
return words.shift();
}
fs.createReadStream(path.join(__dirname, 'happybirthday.txt'))
.pipe(replaceStream('birthday', replaceFn))
.pipe(process.stdout);
```
Which would output:
```
$ node simple.js
Awesome birthday to you!
Good birthday to you!
Super birthday to dear Liza!
Joyous birthday to you!
```
### Web server search and replace over a test file

@@ -72,0 +95,0 @@

@@ -386,2 +386,101 @@ var expect = require('chai').expect

});
it('should be able to use a replace function', function (done) {
var haystacks = [
[ '<!DOCTYPE html>',
'<html>',
' <head>',
' <title>Test</title>',
' </he'
].join('\n'),
[ 'ad>',
' <body>',
' <h1>Head</h1>',
' </body>',
'</html>'
].join('\n'),
];
var acc = '';
var inject = script(fs.readFileSync('./test/fixtures/inject.js'));
function replaceFn(match) {
expect(match).to.equal('</head>');
return inject + '</head>';
}
var replace = replaceStream('</head>', replaceFn);
replace.on('data', function (data) {
acc += data;
});
replace.on('end', function () {
expect(acc).to.include(inject);
done();
});
haystacks.forEach(function (haystack) {
replace.write(haystack);
});
replace.end();
});
it('should be able to change each replacement value with a function',
function (done) {
var haystacks = [
[ '<!DOCTYPE html>',
'<html>',
' <head>',
' <title>Test</title>',
' </head>',
' <body>',
' <p> Hello 1</p>',
' <p> Hello 2</'
].join('\n'),
[ 'p>',
' <p> Hello 3</p>',
' <p> Hello 4</p>',
' <p> Hello 5</p>',
' </body>',
'</html>'
].join('\n'),
];
var acc = '';
var greetings = ['Hi', 'Hey', 'Gday', 'Bonjour', 'Greetings'];
function replaceFn(match) {
return greetings.shift();
}
var replace = replaceStream('Hello', replaceFn);
replace.on('data', function (data) {
acc += data;
});
replace.on('end', function () {
var expected = [
'<!DOCTYPE html>',
'<html>',
' <head>',
' <title>Test</title>',
' </head>',
' <body>',
' <p> Hi 1</p>',
' <p> Hey 2</p>',
' <p> Gday 3</p>',
' <p> Bonjour 4</p>',
' <p> Greetings 5</p>',
' </body>',
'</html>'
].join('\n');
expect(acc).to.equal(expected);
done();
});
haystacks.forEach(function (haystack) {
replace.write(haystack);
});
replace.end();
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc