Skip to content

Commit

Permalink
refactor: replace indexOf() with includes() (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
m31271n authored and fengmk2 committed Dec 26, 2017
1 parent 613f236 commit 3384a87
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/loader/egg_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class EggLoader {
const eggPath = proto[Symbol.for('egg#eggPath')];
assert(eggPath && typeof eggPath === 'string', 'Symbol.for(\'egg#eggPath\') should be string');
const realpath = fs.realpathSync(eggPath);
if (eggPaths.indexOf(realpath) === -1) {
if (!eggPaths.includes(realpath)) {
eggPaths.unshift(realpath);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/loader/mixin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ module.exports = {
this.mergePluginConfig(plugin);

// disable the plugin that not match the serverEnv
if (env && plugin.env.length && plugin.env.indexOf(env) === -1) {
if (env && plugin.env.length && !plugin.env.includes(env)) {
this.options.logger.info('Plugin %s is disabled by env unmatched, require env(%s) but got env is %s', name, plugin.env, env);
plugin.enable = false;
continue;
Expand Down Expand Up @@ -266,7 +266,7 @@ module.exports = {
for (const missName of result.missingTasks) {
const requires = [];
for (const name in allPlugins) {
if (allPlugins[name].dependencies.indexOf(missName) >= 0) {
if (allPlugins[name].dependencies.includes(missName)) {
requires.push(name);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class Router extends KoaRouter {
});

for (const key in args) {
if (replacedParams.indexOf(key) !== -1) {
if (replacedParams.includes(key)) {
continue;
}

Expand All @@ -228,7 +228,7 @@ class Router extends KoaRouter {

if (queries.length > 0) {
const queryStr = queries.join('&');
if (url.indexOf('?') === -1) {
if (!url.includes('?')) {
url = `${url}?${queryStr}`;
} else {
url = `${url}&${queryStr}`;
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/sequencify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function sequence(tasks, names, results, missing, recursive, nest, optional) {
names.forEach(function(name) {
if (results.indexOf(name) !== -1) {
if (results.includes(name)) {
return; // de-dup results
}
const node = tasks[name];
Expand All @@ -16,7 +16,7 @@ function sequence(tasks, names, results, missing, recursive, nest, optional) {

if (!node) {
missing.push(name);
} else if (nest.indexOf(name) > -1) {
} else if (nest.includes(name)) {
nest.push(name);
recursive.push(nest.slice(0));
nest.pop(name);
Expand Down
6 changes: 3 additions & 3 deletions test/egg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('test/egg.test.js', () => {
});
throw new Error('should not run');
} catch (err) {
assert(err.message.indexOf(`Directory ${__filename} is not a directory`) >= 0);
assert(err.message.includes(`Directory ${__filename} is not a directory`));
}
});

Expand Down Expand Up @@ -213,7 +213,7 @@ describe('test/egg.test.js', () => {
app.loader.loadAll();
app.once('ready_timeout', id => {
const file = path.normalize('test/fixtures/beforestart-with-timeout-env/app.js');
assert(id.indexOf(file) >= 0);
assert(id.includes(file));
done();
});
});
Expand Down Expand Up @@ -243,7 +243,7 @@ describe('test/egg.test.js', () => {
app.loader.loadAll();
app.once('ready_timeout', id => {
const file = path.normalize('test/fixtures/beforestart-timeout/app.js');
assert(id.indexOf(file) >= 0);
assert(id.includes(file));
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/loader/get_appname.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('test/loader/get_appname.test.js', () => {
try {
utils.createApp('app-noname');
} catch (err) {
assert(err.message.indexOf(`name is required from ${pkg}`) >= 0);
assert(err.message.includes(`name is required from ${pkg}`));
done();
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/loader/mixin/load_config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('test/loader/mixin/load_config.test.js', () => {
loader.loadConfig();
throw new Error('should not run');
} catch (err) {
assert(err.message.indexOf(`Can not define middleware in ${path.join(pluginDir, 'config/config.default.js')}`) >= 0);
assert(err.message.includes(`Can not define middleware in ${path.join(pluginDir, 'config/config.default.js')}`));
}
});

Expand Down
6 changes: 3 additions & 3 deletions test/loader/mixin/load_plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ describe('test/load_plugin.test.js', function() {
const plugins = loader.orderPlugins.map(function(plugin) {
return plugin.name;
});
assert(plugins.indexOf('testMe') === -1);
assert(!plugins.includes('testMe'));
});

it('should complement infomation by config/plugin.js from plugin', function() {
Expand All @@ -339,7 +339,7 @@ describe('test/load_plugin.test.js', function() {
const keys1 = loader1.orderPlugins.map(function(plugin) {
return plugin.name;
}).join(',');
assert(keys1.indexOf('b,c,d1,f,e') > -1);
assert(keys1.includes('b,c,d1,f,e'));
assert(!loader1.plugins.a1);

mm(process.env, 'NODE_ENV', 'development');
Expand All @@ -350,7 +350,7 @@ describe('test/load_plugin.test.js', function() {
const keys2 = loader2.orderPlugins.map(function(plugin) {
return plugin.name;
}).join(',');
assert(keys2.indexOf('d1,a1,b,c,f,e') > -1);
assert(keys2.includes('d1,a1,b,c,f,e'));
assert.deepEqual(loader2.plugins.a1, {
enable: true,
name: 'a1',
Expand Down
2 changes: 1 addition & 1 deletion test/utils/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ describe('test/utils/router.test.js', () => {

describe('router.method', () => {
it('router method include HEAD', () => {
assert(app.router.methods.indexOf('HEAD') > -1);
assert(app.router.methods.includes('HEAD'));
});
});

Expand Down

0 comments on commit 3384a87

Please sign in to comment.