diff --git a/src/sed.js b/src/sed.js index 6535246..e1aeb01 100644 --- a/src/sed.js +++ b/src/sed.js @@ -28,13 +28,20 @@ function _sed(options, regex, replacement, file) { else common.error('invalid replacement string'); + // Convert all search strings to RegExp + if (typeof regex === 'string') + regex = RegExp(regex); + if (!file) common.error('no file given'); if (!fs.existsSync(file)) common.error('no such file or directory: ' + file); - var result = fs.readFileSync(file, 'utf8').replace(regex, replacement); + var result = fs.readFileSync(file, 'utf8').split('\n').map(function (line) { + return line.replace(regex, replacement); + }).join('\n'); + if (options.inplace) fs.writeFileSync(file, result, 'utf8'); diff --git a/test/grep.js b/test/grep.js index df57483..267a989 100644 --- a/test/grep.js +++ b/test/grep.js @@ -63,4 +63,24 @@ var result = shell.grep(/test/, '**/file*.js'); assert.equal(shell.error(), null); assert.equal(result, 'test\ntest\ntest\ntest\n'); +// one file, * in regex +var result = shell.grep(/alpha*beta/, 'resources/grep/file'); +assert.equal(shell.error(), null); +assert.equal(result, 'alphaaaaaaabeta\nalphbeta\n'); + +// one file, * in string-regex +var result = shell.grep('alpha*beta', 'resources/grep/file'); +assert.equal(shell.error(), null); +assert.equal(result, 'alphaaaaaaabeta\nalphbeta\n'); + +// one file, * in regex, make sure * is not globbed +var result = shell.grep(/l*\.js/, 'resources/grep/file'); +assert.equal(shell.error(), null); +assert.equal(result, 'this line ends in.js\nlllllllllllllllll.js\n'); + +// one file, * in string-regex, make sure * is not globbed +var result = shell.grep('l*\\.js', 'resources/grep/file'); +assert.equal(shell.error(), null); +assert.equal(result, 'this line ends in.js\nlllllllllllllllll.js\n'); + shell.exit(123); diff --git a/test/resources/grep/file b/test/resources/grep/file new file mode 100644 index 0000000..dbb55d2 --- /dev/null +++ b/test/resources/grep/file @@ -0,0 +1,5 @@ +alphaaaaaaabeta +howareyou +alphbeta +this line ends in.js +lllllllllllllllll.js diff --git a/test/sed.js b/test/sed.js index 74f0980..469a69e 100644 --- a/test/sed.js +++ b/test/sed.js @@ -43,7 +43,7 @@ assert.equal(shell.error(), null); assert.equal(result, '1234'); var replaceFun = function (match) { - return match.toUpperCase() + match; + return match.toUpperCase() + match; }; var result = shell.sed(/test1/, replaceFun, 'tmp/file1'); // replacement function assert.equal(shell.error(), null); @@ -54,4 +54,24 @@ assert.equal(shell.error(), null); assert.equal(result, 'hello'); assert.equal(shell.cat('tmp/file1'), 'hello'); +// make sure * in regex is not globbed +var result = shell.sed(/alpha*beta/, 'hello', 'resources/grep/file'); +assert.equal(shell.error(), null); +assert.equal(result, 'hello\nhowareyou\nhello\nthis line ends in.js\nlllllllllllllllll.js\n'); + +// make sure * in string-regex is not globbed +var result = shell.sed('alpha*beta', 'hello', 'resources/grep/file'); +assert.ok(!shell.error()); +assert.equal(result, 'hello\nhowareyou\nhello\nthis line ends in.js\nlllllllllllllllll.js\n'); + +// make sure * in regex is not globbed +var result = shell.sed(/l*\.js/, '', 'resources/grep/file'); +assert.ok(!shell.error()); +assert.equal(result, 'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n'); + +// make sure * in string-regex is not globbed +var result = shell.sed('l*\\.js', '', 'resources/grep/file'); +assert.ok(!shell.error()); +assert.equal(result, 'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n'); + shell.exit(123);