replacestream
Advanced tools
Comparing version 2.1.0 to 3.0.0
81
index.js
@@ -1,5 +0,8 @@ | ||
var through = require('through'); | ||
'use strict'; | ||
module.exports = ReplaceStream; | ||
function ReplaceStream(search, replace, options) { | ||
var escapeRegExp = require('escape-string-regexp'); | ||
var objectAssign = require('object-assign'); | ||
var Transform = require('readable-stream/transform'); | ||
module.exports = function ReplaceStream(search, replace, options) { | ||
var tail = ''; | ||
@@ -9,6 +12,7 @@ var totalMatches = 0; | ||
options = options || {}; | ||
options.limit = options.limit || Infinity; | ||
options.encoding = options.encoding || 'utf8'; | ||
options.max_match_len = options.max_match_len || 100; | ||
options = objectAssign({ | ||
limit: Infinity, | ||
encoding: 'utf8', | ||
max_match_len: 100 | ||
}, options); | ||
@@ -27,3 +31,3 @@ var replaceFn = replace; | ||
function write(buf) { | ||
function transform(buf, enc, cb) { | ||
var matches; | ||
@@ -57,3 +61,3 @@ var lastPos = 0; | ||
var dataToQueue = getDataToQueue(matchCount,haystack,rewritten,lastPos); | ||
this.queue(dataToQueue); | ||
cb(null, dataToQueue); | ||
} | ||
@@ -72,25 +76,25 @@ | ||
function getDataToQueue(matchCount, haystack, rewritten, lastPos) { | ||
var dataToQueue; | ||
if (matchCount > 0) { | ||
if (haystack.length > tail.length) { | ||
dataToQueue = rewritten + haystack.slice(lastPos, haystack.length - tail.length) | ||
} else { | ||
dataToQueue = rewritten | ||
return rewritten + haystack.slice(lastPos, haystack.length - tail.length); | ||
} | ||
} else { | ||
dataToQueue = haystack.slice(0, haystack.length - tail.length) | ||
return rewritten; | ||
} | ||
return dataToQueue; | ||
return haystack.slice(0, haystack.length - tail.length); | ||
} | ||
function end() { | ||
if (tail) this.queue(tail); | ||
this.queue(null); | ||
function flush(cb) { | ||
if (tail) { | ||
this.push(tail); | ||
} | ||
cb(); | ||
} | ||
var t = through(write, end); | ||
return t; | ||
} | ||
return new Transform({ | ||
transform: transform, | ||
flush: flush | ||
}); | ||
}; | ||
@@ -110,6 +114,2 @@ function createReplaceFn(replace, isRegEx) { | ||
var stringReplaceFunction = function () { | ||
return replace; | ||
}; | ||
if (isRegEx && !(replace instanceof Function)) { | ||
@@ -120,17 +120,15 @@ return regexReplaceFunction; | ||
if (!(replace instanceof Function)) { | ||
return stringReplaceFunction | ||
return function stringReplaceFunction() { | ||
return replace; | ||
}; | ||
} | ||
return replace | ||
return replace; | ||
} | ||
function escapeRegExp(s) { | ||
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
} | ||
function matchFromRegex(regex, options) { | ||
if (options.regExpOptions) { | ||
regex = new RegExp(regex.source, options.regExpOptions) | ||
} | ||
function matchFromRegex(s, options) { | ||
var regex = s | ||
if (options.regExpOptions) | ||
regex = new RegExp(s.source, options.regExpOptions) | ||
// If there is no global flag then there can only be one match | ||
@@ -140,10 +138,11 @@ if (!regex.global) { | ||
} | ||
return regex | ||
return regex; | ||
} | ||
function matchFromString(s, options) { | ||
if (options.regExpOptions) | ||
return new RegExp(escapeRegExp(s), options.regExpOptions) | ||
if (options.regExpOptions) { | ||
return new RegExp(escapeRegExp(s), options.regExpOptions); | ||
} | ||
return new RegExp(escapeRegExp(s), options.ignoreCase === false ? 'gm' : 'gmi') | ||
return new RegExp(escapeRegExp(s), options.ignoreCase === false ? 'gm' : 'gmi'); | ||
} |
{ | ||
"name": "replacestream", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "A node.js through stream that does basic streaming text search and replace and is chunk boundary friendly", | ||
"main": "index.js", | ||
"repository": "eugeneware/replacestream", | ||
"author": "Eugene Ware <eugene@noblesamurai.com>", | ||
"license": "BSD-3-Clause", | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
"test": "node_modules/.bin/mocha" | ||
"test": "mocha --growl --full-trace" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/eugeneware/replacestream" | ||
}, | ||
"keywords": [ | ||
@@ -22,11 +23,12 @@ "replace", | ||
], | ||
"author": "Eugene Ware <eugene@noblesamurai.com>", | ||
"license": "BSD", | ||
"dependencies": { | ||
"escape-string-regexp": "^1.0.3", | ||
"object-assign": "^3.0.0", | ||
"readable-stream": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"chai": "^1.9.1", | ||
"mocha": "^1.21.4" | ||
}, | ||
"dependencies": { | ||
"through": "~2.3.4" | ||
"chai": "^3.0.0", | ||
"concat-stream": "^1.5.0", | ||
"mocha": "^2.2.5" | ||
} | ||
} |
@@ -265,3 +265,5 @@ # replacestream | ||
<tr><th align="left">Tim Chaplin</th><td><a href="https://github.com/tjchaplin">GitHub/tjchaplin</a></td></tr> | ||
<tr><th align="left">Bryce Gibson</th><td><a href="https://github.com/bryce-gibson">GitHub/bryce-gibson</a></td></tr> | ||
<tr><th align="left">Romain</th><td><a href="https://github.com/Filirom1">GitHub/Filirom1</a></td></tr> | ||
<tr><th align="left">Shinnosuke Watanabe</th><td><a href="https://github.com/shinnn">GitHub/shinnn</a></td></tr> | ||
</tbody></table> |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
269
1
13804
3
3
4
112
+ Addedescape-string-regexp@^1.0.3
+ Addedobject-assign@^3.0.0
+ Addedreadable-stream@^2.0.1
+ Addedcore-util-is@1.0.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedobject-assign@3.0.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedthrough@~2.3.4
- Removedthrough@2.3.8(transitive)