Skip to content

Commit

Permalink
grep includes the i flag (#876)
Browse files Browse the repository at this point in the history
grep includes the i flag to ignored upper/lower case differences
  • Loading branch information
ppsleep authored and nfischer committed Jul 23, 2018
1 parent 8dae55f commit 4113a72
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -362,7 +362,8 @@ include the base directories (e.g., `lib/resources/file1` instead of just `file1
Available options:

+ `-v`: Invert `regex_filter` (only print non-matching lines).
+ `-l`: Print only filenames of matching files
+ `-l`: Print only filenames of matching files.
+ `-i`: Ignore case.

Examples:

Expand Down
7 changes: 6 additions & 1 deletion src/grep.js
Expand Up @@ -7,6 +7,7 @@ common.register('grep', _grep, {
cmdOptions: {
'v': 'inverse',
'l': 'nameOnly',
'i': 'ignoreCase',
},
});

Expand All @@ -17,7 +18,8 @@ common.register('grep', _grep, {
//@ Available options:
//@
//@ + `-v`: Invert `regex_filter` (only print non-matching lines).
//@ + `-l`: Print only filenames of matching files
//@ + `-l`: Print only filenames of matching files.
//@ + `-i`: Ignore case.
//@
//@ Examples:
//@
Expand All @@ -41,6 +43,9 @@ function _grep(options, regex, files) {
}

var grep = [];
if (options.ignoreCase) {
regex = new RegExp(regex, 'i');
}
files.forEach(function (file) {
if (!fs.existsSync(file) && file !== '-') {
common.error('no such file or directory: ' + file, 2, { continue: true });
Expand Down
7 changes: 7 additions & 0 deletions test/grep.js
Expand Up @@ -137,6 +137,13 @@ test('-l option', t => {
t.is(result.split('\n').length - 1, 2);
});

test('-i option', t => {
const result = shell.grep('-i', 'test', 'test/resources/grep/case1', 'test/resources/grep/case1.txt',
'test/resources/grep/case1.js');
t.falsy(shell.error());
t.is(result.split('\n').length - 1, 3);
});

test('the pattern looks like an option', t => {
const result = shell.grep('--', '-v', 'test/resources/grep/file2');
t.falsy(shell.error());
Expand Down
1 change: 1 addition & 0 deletions test/resources/grep/case1
@@ -0,0 +1 @@
Test3
1 change: 1 addition & 0 deletions test/resources/grep/case1.js
@@ -0,0 +1 @@
test3
1 change: 1 addition & 0 deletions test/resources/grep/case1.txt
@@ -0,0 +1 @@
TEST3

0 comments on commit 4113a72

Please sign in to comment.