Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -48,6 +50,9 @@ function _grep(options, regex, files) {
}

var contents = file === '-' ? pipe : fs.readFileSync(file, 'utf8');
if (options.ignoreCase) {
regex = new RegExp(regex, 'i');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, one more fix: move the ignoreCase logic out of the for-loop. The CI failure is because we're repeatedly re-assigning regex, but we should only assign to it once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it's a low-level error, i fixed it, sorry.

}
if (options.nameOnly) {
if (contents.match(regex)) {
grep.push(file);
Expand Down
7 changes: 7 additions & 0 deletions test/grep.js
Original file line number Diff line number Diff line change
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/case1', 'test/resources/case1.txt',
'test/resources/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/case1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is OK, but could you move these files under test/resources/grep/case1*? That should fix the other failures.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I moved it, and sorry for my thoughtless actions.

1 change: 1 addition & 0 deletions test/resources/case1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test3
1 change: 1 addition & 0 deletions test/resources/case1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST3