gulp-replace
Advanced tools
Comparing version 0.3.0 to 0.4.0
84
index.js
var es = require('event-stream'); | ||
var rs = require('replacestream'); | ||
var stream = require('stream'); | ||
var istextorbinary = require('istextorbinary'); | ||
module.exports = function(search, replacement) { | ||
module.exports = function(search, replacement, options) { | ||
var doReplace = function(file, callback) { | ||
@@ -15,42 +16,63 @@ var isRegExp = search instanceof RegExp; | ||
if (isStream) { | ||
file.contents = file.contents.pipe(rs(search, replacement)); | ||
return callback(null, file); | ||
} | ||
if (isBuffer) { | ||
if (isRegExp) { | ||
file.contents = new Buffer(String(file.contents).replace(search, replacement)); | ||
function doReplace() { | ||
if (isStream) { | ||
file.contents = file.contents.pipe(rs(search, replacement)); | ||
return callback(null, file); | ||
} | ||
else { | ||
var chunks = String(file.contents).split(search); | ||
var result; | ||
if (typeof replacement === 'function') { | ||
// Start with the first chunk already in the result | ||
// Replacements will be added thereafter | ||
// This is done to avoid checking the value of i in the loop | ||
result = [ chunks[0] ]; | ||
if (isBuffer) { | ||
if (isRegExp) { | ||
file.contents = new Buffer(String(file.contents).replace(search, replacement)); | ||
} | ||
else { | ||
var chunks = String(file.contents).split(search); | ||
// The replacement function should be called once for each match | ||
for (var i = 1; i < chunks.length; i++) { | ||
// Add the replacement value | ||
result.push(replacement(search)); | ||
var result; | ||
if (typeof replacement === 'function') { | ||
// Start with the first chunk already in the result | ||
// Replacements will be added thereafter | ||
// This is done to avoid checking the value of i in the loop | ||
result = [ chunks[0] ]; | ||
// Add the next chunk | ||
result.push(chunks[i]); | ||
// The replacement function should be called once for each match | ||
for (var i = 1; i < chunks.length; i++) { | ||
// Add the replacement value | ||
result.push(replacement(search)); | ||
// Add the next chunk | ||
result.push(chunks[i]); | ||
} | ||
result = result.join(''); | ||
} | ||
else { | ||
result = chunks.join(replacement); | ||
} | ||
result = result.join(''); | ||
file.contents = new Buffer(result); | ||
} | ||
else { | ||
result = chunks.join(replacement); | ||
} | ||
return callback(null, file); | ||
} | ||
file.contents = new Buffer(result); | ||
} | ||
return callback(null, file); | ||
callback(null, file); | ||
} | ||
callback(null, file); | ||
if (options && options.skipBinary) { | ||
istextorbinary.isText('', file.contents, function(err, result) { | ||
if (err) { | ||
return callback(err, file); | ||
} | ||
if (!result) { | ||
return callback(null, file); | ||
} else { | ||
doReplace(); | ||
} | ||
}); | ||
} | ||
else { | ||
doReplace(); | ||
} | ||
}; | ||
@@ -57,0 +79,0 @@ |
{ | ||
"name": "gulp-replace", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A string replace plugin for gulp", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"event-stream": "~3.0.18", | ||
"replacestream": "0.1.3" | ||
"replacestream": "0.1.3", | ||
"istextorbinary": "~1.0.0" | ||
}, | ||
@@ -11,0 +12,0 @@ "devDependencies": { |
@@ -29,3 +29,3 @@ # gulp-replace [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url] | ||
### replace(string, replacement) | ||
### replace(string, replacement[, options]) | ||
@@ -42,3 +42,3 @@ #### string | ||
### replace(regex, replace) | ||
### replace(regex, replacement[, options]) | ||
@@ -57,2 +57,16 @@ *Note:* gulp-replace cannot perform regex replacement on streams. | ||
### gulp-replace options | ||
An optional third argument, `options`, can be passed. | ||
#### options | ||
Type: `Object` | ||
##### options.skipBinary | ||
Type: `boolean` | ||
Default: `false` | ||
Skip binary files | ||
[MDN documentation for RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp | ||
@@ -62,4 +76,4 @@ [MDN documentation for String.replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter | ||
[travis-url]: http://travis-ci.org/lazd/gulp-replace | ||
[travis-image]: https://secure.travis-ci.org/lazd/gulp-replace.png?branch=master | ||
[travis-image]: https://secure.travis-ci.org/lazd/gulp-replace.svg?branch=master | ||
[npm-url]: https://npmjs.org/package/gulp-replace | ||
[npm-image]: https://badge.fury.io/js/gulp-replace.png | ||
[npm-image]: https://badge.fury.io/js/gulp-replace.svg |
@@ -9,11 +9,2 @@ var replacePlugin = require('../'); | ||
var makeFile = function(contents) { | ||
return new gutil.File({ | ||
path: 'test/file.txt', | ||
cwd: 'test/', | ||
base: 'test/', | ||
contents: contents | ||
}); | ||
}; | ||
describe('gulp-replace', function() { | ||
@@ -182,3 +173,3 @@ describe('replacePlugin()', function() { | ||
it('should call function once for each replacement when replacing a string on a buffern', function(done) { | ||
it('should call function once for each replacement when replacing a string on a buffer', function(done) { | ||
var file = new gutil.File({ | ||
@@ -209,3 +200,66 @@ path: 'test/fixtures/helloworld.txt', | ||
}); | ||
it('should ignore binary files when skipBinary is enabled', function(done) { | ||
var file = new gutil.File({ | ||
path: 'test/fixtures/binary.png', | ||
cwd: 'test/', | ||
base: 'test/fixtures', | ||
contents: fs.readFileSync('test/fixtures/binary.png') | ||
}); | ||
var stream = replacePlugin('world', 'person', {skipBinary: true}); | ||
stream.on('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
newFile.contents.should.eql(fs.readFileSync('test/expected/binary.png')); | ||
done(); | ||
}); | ||
stream.write(file); | ||
stream.end(); | ||
}); | ||
it('should replace string on non binary files when skipBinary is enabled', function(done) { | ||
var file = new gutil.File({ | ||
path: 'test/fixtures/helloworld.txt', | ||
cwd: 'test/', | ||
base: 'test/fixtures', | ||
contents: fs.createReadStream('test/fixtures/helloworld.txt') | ||
}); | ||
var stream = replacePlugin('world', 'person', {skipBinary: true}); | ||
stream.on('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
newFile.contents.pipe(es.wait(function(err, data) { | ||
should.not.exist(err); | ||
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8')); | ||
done(); | ||
})); | ||
}); | ||
stream.write(file); | ||
stream.end(); | ||
}); | ||
it('should trigger events on a stream', function(done) { | ||
var file = new gutil.File({ | ||
path: 'test/fixtures/helloworld.txt', | ||
cwd: 'test/', | ||
base: 'test/fixtures', | ||
contents: fs.readFileSync('test/fixtures/helloworld.txt') | ||
}); | ||
var stream = replacePlugin('world', 'elephant') | ||
.on('end', function() { | ||
// No assertion required, we should end up here, if we don't the test will time out | ||
done(); | ||
}); | ||
stream.write(file); | ||
stream.end(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18276
17
375
76
3
3
+ Addedistextorbinary@~1.0.0
+ Addedbinaryextensions@1.0.1(transitive)
+ Addedistextorbinary@1.0.2(transitive)
+ Addedtextextensions@1.0.2(transitive)