Skip to content

Commit ea57f4e

Browse files
committed
refactor: prefer includes() over indexOf()
No change to logic. This swaps out Array.prototype.indexOf in favor for Array.prototype.includes() to simplify logic. This does the same for the equivalent String.prototype functions. Test: npm test
1 parent f29a7a8 commit ea57f4e

8 files changed

Lines changed: 155 additions & 155 deletions

File tree

src/chmod.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,16 @@ function _chmod(options, mode, filePattern) {
136136
var operator = matches[2];
137137
var change = matches[3];
138138

139-
var changeOwner = applyTo.indexOf('u') !== -1 || applyTo === 'a' || applyTo === '';
140-
var changeGroup = applyTo.indexOf('g') !== -1 || applyTo === 'a' || applyTo === '';
141-
var changeOther = applyTo.indexOf('o') !== -1 || applyTo === 'a' || applyTo === '';
142-
143-
var changeRead = change.indexOf('r') !== -1;
144-
var changeWrite = change.indexOf('w') !== -1;
145-
var changeExec = change.indexOf('x') !== -1;
146-
var changeExecDir = change.indexOf('X') !== -1;
147-
var changeSticky = change.indexOf('t') !== -1;
148-
var changeSetuid = change.indexOf('s') !== -1;
139+
var changeOwner = applyTo.includes('u') || applyTo === 'a' || applyTo === '';
140+
var changeGroup = applyTo.includes('g') || applyTo === 'a' || applyTo === '';
141+
var changeOther = applyTo.includes('o') || applyTo === 'a' || applyTo === '';
142+
143+
var changeRead = change.includes('r');
144+
var changeWrite = change.includes('w');
145+
var changeExec = change.includes('x');
146+
var changeExecDir = change.includes('X');
147+
var changeSticky = change.includes('t');
148+
var changeSetuid = change.includes('s');
149149

150150
if (changeExecDir && isDir) {
151151
changeExec = true;

src/which.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function _which(options, cmd) {
6464
var queryMatches = [];
6565

6666
// No relative/absolute paths provided?
67-
if (cmd.indexOf('/') === -1) {
67+
if (!cmd.includes('/')) {
6868
// Assume that there are no extensions to append to queries (this is the
6969
// case for unix)
7070
var pathExtArray = [''];
@@ -87,7 +87,7 @@ function _which(options, cmd) {
8787
}
8888

8989
var match = attempt.match(/\.[^<>:"/|?*.]+$/);
90-
if (match && pathExtArray.indexOf(match[0]) >= 0) { // this is Windows-only
90+
if (match && pathExtArray.includes(match[0])) { // this is Windows-only
9191
// The user typed a query with the file extension, like
9292
// `which('node.exe')`
9393
if (checkPath(attempt)) {

test/config.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ test('config.globOptions respects dot', t => {
103103
shell.config.globOptions = { dot: true };
104104
const result = common.expand(['test/resources/ls/*']);
105105
t.is(result.length, 8);
106-
t.truthy(result.indexOf('test/resources/ls/.hidden_dir') > -1);
107-
t.truthy(result.indexOf('test/resources/ls/.hidden_file') > -1);
106+
t.truthy(result.includes('test/resources/ls/.hidden_dir'));
107+
t.truthy(result.includes('test/resources/ls/.hidden_file'));
108108
});
109109

110110
test('config.globOptions respects ignore', t => {
@@ -119,7 +119,7 @@ test('config.globOptions respects ignore', t => {
119119
];
120120
t.deepEqual(result, expected);
121121
// Does not include the result that we chose to ignore
122-
t.truthy(result.indexOf('test/resources/external') < 0);
122+
t.falsy(result.includes('test/resources/external'));
123123
});
124124

125125
test('config.globOptions respects absolute', t => {
@@ -153,9 +153,9 @@ test('config.globOptions respects nodir', t => {
153153
];
154154
t.deepEqual(result, expected);
155155
// Does not include the directories.
156-
t.truthy(result.indexOf('test/resources/cat') < 0);
157-
t.truthy(result.indexOf('test/resources/head') < 0);
158-
t.truthy(result.indexOf('test/resources/external') < 0);
156+
t.falsy(result.includes('test/resources/cat'));
157+
t.falsy(result.includes('test/resources/head'));
158+
t.falsy(result.includes('test/resources/external'));
159159
});
160160

161161
test('config.globOptions respects mark', t => {

test/find.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ test('current path', t => {
2828
const result = shell.find('.');
2929
t.falsy(shell.error());
3030
t.is(result.code, 0);
31-
t.truthy(result.indexOf('.hidden') > -1);
32-
t.truthy(result.indexOf('dir1/dir11/a_dir11') > -1);
31+
t.truthy(result.includes('.hidden'));
32+
t.truthy(result.includes('dir1/dir11/a_dir11'));
3333
t.is(result.length, 12);
3434
shell.cd('../..');
3535
});
@@ -38,26 +38,26 @@ test('simple path', t => {
3838
const result = shell.find('test/resources/find');
3939
t.falsy(shell.error());
4040
t.is(result.code, 0);
41-
t.truthy(result.indexOf('test/resources/find/.hidden') > -1);
42-
t.truthy(result.indexOf('test/resources/find/dir1/dir11/a_dir11') > -1);
41+
t.truthy(result.includes('test/resources/find/.hidden'));
42+
t.truthy(result.includes('test/resources/find/dir1/dir11/a_dir11'));
4343
t.is(result.length, 12);
4444
});
4545

4646
test('multiple paths - comma', t => {
4747
const result = shell.find('test/resources/find/dir1', 'test/resources/find/dir2');
4848
t.falsy(shell.error());
4949
t.is(result.code, 0);
50-
t.truthy(result.indexOf('test/resources/find/dir1/dir11/a_dir11') > -1);
51-
t.truthy(result.indexOf('test/resources/find/dir2/a_dir1') > -1);
50+
t.truthy(result.includes('test/resources/find/dir1/dir11/a_dir11'));
51+
t.truthy(result.includes('test/resources/find/dir2/a_dir1'));
5252
t.is(result.length, 6);
5353
});
5454

5555
test('multiple paths - array', t => {
5656
const result = shell.find(['test/resources/find/dir1', 'test/resources/find/dir2']);
5757
t.falsy(shell.error());
5858
t.is(result.code, 0);
59-
t.truthy(result.indexOf('test/resources/find/dir1/dir11/a_dir11') > -1);
60-
t.truthy(result.indexOf('test/resources/find/dir2/a_dir1') > -1);
59+
t.truthy(result.includes('test/resources/find/dir1/dir11/a_dir11'));
60+
t.truthy(result.includes('test/resources/find/dir2/a_dir1'));
6161
t.is(result.length, 6);
6262
});
6363

@@ -71,6 +71,6 @@ test('-L flag, folder is symlinked', t => {
7171
const result = shell.find('-L', 'test/resources/find');
7272
t.falsy(shell.error());
7373
t.is(result.code, 0);
74-
t.truthy(result.indexOf('test/resources/find/dir2_link/a_dir1') > -1);
74+
t.truthy(result.includes('test/resources/find/dir2_link/a_dir1'));
7575
t.is(result.length, 13);
7676
});

0 commit comments

Comments
 (0)