Skip to content

Commit 1ee696d

Browse files
committed
fix: regexes are more consistent with sed and grep
sed will now convert search strings to regex form, so `'a*'` will now work like `/a*/`. Also, new tests for grep and sed ensure that '*' is not expanded for filename globbing.
1 parent 5ea67cf commit 1ee696d

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

src/sed.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@ function _sed(options, regex, replacement, file) {
2828
else
2929
common.error('invalid replacement string');
3030

31+
// Convert all search strings to RegExp
32+
if (typeof regex === 'string')
33+
regex = RegExp(regex);
34+
3135
if (!file)
3236
common.error('no file given');
3337

3438
if (!fs.existsSync(file))
3539
common.error('no such file or directory: ' + file);
3640

37-
var result = fs.readFileSync(file, 'utf8').replace(regex, replacement);
41+
var result = fs.readFileSync(file, 'utf8').split('\n').map(function (line) {
42+
return line.replace(regex, replacement);
43+
}).join('\n');
44+
3845
if (options.inplace)
3946
fs.writeFileSync(file, result, 'utf8');
4047

test/grep.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,24 @@ var result = shell.grep(/test/, '**/file*.js');
6363
assert.equal(shell.error(), null);
6464
assert.equal(result, 'test\ntest\ntest\ntest\n');
6565

66+
// one file, * in regex
67+
var result = shell.grep(/alpha*beta/, 'resources/grep/file');
68+
assert.equal(shell.error(), null);
69+
assert.equal(result, 'alphaaaaaaabeta\nalphbeta\n');
70+
71+
// one file, * in string-regex
72+
var result = shell.grep('alpha*beta', 'resources/grep/file');
73+
assert.equal(shell.error(), null);
74+
assert.equal(result, 'alphaaaaaaabeta\nalphbeta\n');
75+
76+
// one file, * in regex, make sure * is not globbed
77+
var result = shell.grep(/l*\.js/, 'resources/grep/file');
78+
assert.equal(shell.error(), null);
79+
assert.equal(result, 'this line ends in.js\nlllllllllllllllll.js\n');
80+
81+
// one file, * in string-regex, make sure * is not globbed
82+
var result = shell.grep('l*\\.js', 'resources/grep/file');
83+
assert.equal(shell.error(), null);
84+
assert.equal(result, 'this line ends in.js\nlllllllllllllllll.js\n');
85+
6686
shell.exit(123);

test/resources/grep/file

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
alphaaaaaaabeta
2+
howareyou
3+
alphbeta
4+
this line ends in.js
5+
lllllllllllllllll.js

test/sed.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ assert.equal(shell.error(), null);
4343
assert.equal(result, '1234');
4444

4545
var replaceFun = function (match) {
46-
return match.toUpperCase() + match;
46+
return match.toUpperCase() + match;
4747
};
4848
var result = shell.sed(/test1/, replaceFun, 'tmp/file1'); // replacement function
4949
assert.equal(shell.error(), null);
@@ -54,4 +54,24 @@ assert.equal(shell.error(), null);
5454
assert.equal(result, 'hello');
5555
assert.equal(shell.cat('tmp/file1'), 'hello');
5656

57+
// make sure * in regex is not globbed
58+
var result = shell.sed(/alpha*beta/, 'hello', 'resources/grep/file');
59+
assert.equal(shell.error(), null);
60+
assert.equal(result, 'hello\nhowareyou\nhello\nthis line ends in.js\nlllllllllllllllll.js\n');
61+
62+
// make sure * in string-regex is not globbed
63+
var result = shell.sed('alpha*beta', 'hello', 'resources/grep/file');
64+
assert.ok(!shell.error());
65+
assert.equal(result, 'hello\nhowareyou\nhello\nthis line ends in.js\nlllllllllllllllll.js\n');
66+
67+
// make sure * in regex is not globbed
68+
var result = shell.sed(/l*\.js/, '', 'resources/grep/file');
69+
assert.ok(!shell.error());
70+
assert.equal(result, 'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n');
71+
72+
// make sure * in string-regex is not globbed
73+
var result = shell.sed('l*\\.js', '', 'resources/grep/file');
74+
assert.ok(!shell.error());
75+
assert.equal(result, 'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n');
76+
5777
shell.exit(123);

0 commit comments

Comments
 (0)