line-replace
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "line-replace", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Replace a line in a file with passed string.", | ||
@@ -5,0 +5,0 @@ "main": "src/line-replace.js", |
@@ -31,5 +31,6 @@ # line-replace | ||
replaceLine({ | ||
file: error.file, | ||
line: error.line, | ||
text: fixedString, | ||
file: 'a-file.txt', | ||
line: 42, | ||
text: 'Answer to the Ultimate Question of Life, the Universe, and Everything.', | ||
addNewLine: true, | ||
onReplace | ||
@@ -44,2 +45,32 @@ }) | ||
`addNewLine` defaults to `true`, which doesn't add or remove | ||
lines from the original document. | ||
Setting it to `false` is the equivalent of removing the passed line and appending | ||
the passed text to the next line. | ||
For example, with the following content: | ||
``` | ||
First line. | ||
Second line. | ||
Third line. | ||
``` | ||
Running: | ||
``` | ||
replaceLine({ | ||
file: 'the-file.txt', | ||
line: 2, | ||
text: 'LOOK: ', | ||
addNewLine: false, | ||
onReplace | ||
}) | ||
``` | ||
Will result in: | ||
``` | ||
First line. | ||
LOOK: Third line. | ||
``` | ||
## Should it work with big files? | ||
@@ -46,0 +77,0 @@ |
@@ -5,3 +5,3 @@ const fs = require('fs') | ||
function replaceLine ({file, line, text, callback}) { | ||
function replaceLine ({file, line, text, addNewLine = true, callback}) { | ||
const readStream = fs.createReadStream(file) | ||
@@ -20,4 +20,4 @@ const tempFile = `${file}.tmp` | ||
replacedText = originalLine | ||
writeStream.write(`${text}\n`) | ||
return | ||
if (addNewLine) return writeStream.write(`${text}\n`) | ||
return writeStream.write(`${text}`) | ||
} | ||
@@ -24,0 +24,0 @@ |
@@ -56,2 +56,21 @@ const {expect} = require('chai') | ||
it('should not add new line', (done) => { | ||
replaceLine({ | ||
file: testFile, | ||
line: 1, | ||
text: 'mmm... ', | ||
addNewLine: false, | ||
callback: onReplace | ||
}) | ||
function onReplace (replaceData) { | ||
const fileData = fs.readFileSync(testFile, {encoding: 'utf8'}) | ||
const newContent = testFileContent | ||
.replace('This kinda works!\n', '') | ||
.replace('First line.', 'mmm... This kinda works!') | ||
expect(fileData).to.equal(newContent) | ||
done() | ||
} | ||
}) | ||
it('should pass replacement data to callback', (done) => { | ||
@@ -58,0 +77,0 @@ replaceLine({ |
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
10444
245
79