From 35024db7f68a30b2ff24e45b98583e20e72266f4 Mon Sep 17 00:00:00 2001 From: m31271n Date: Sat, 23 Dec 2017 11:05:04 +0800 Subject: [PATCH] styles: replace `indexOf()` with `includes()` --- lib/loader/egg_loader.js | 2 +- lib/loader/mixin/plugin.js | 4 ++-- lib/utils/router.js | 4 ++-- lib/utils/sequencify.js | 4 ++-- test/egg.test.js | 6 +++--- test/loader/get_appname.test.js | 2 +- test/loader/mixin/load_config.test.js | 2 +- test/loader/mixin/load_plugin.test.js | 6 +++--- test/utils/router.test.js | 2 +- 9 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/loader/egg_loader.js b/lib/loader/egg_loader.js index 3d555458..50690180 100644 --- a/lib/loader/egg_loader.js +++ b/lib/loader/egg_loader.js @@ -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); } } diff --git a/lib/loader/mixin/plugin.js b/lib/loader/mixin/plugin.js index 4d9bf09e..dc0a78f9 100644 --- a/lib/loader/mixin/plugin.js +++ b/lib/loader/mixin/plugin.js @@ -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; @@ -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); } } diff --git a/lib/utils/router.js b/lib/utils/router.js index 6c9e9fb6..fdb6bf99 100644 --- a/lib/utils/router.js +++ b/lib/utils/router.js @@ -210,7 +210,7 @@ class Router extends KoaRouter { }); for (const key in args) { - if (replacedParams.indexOf(key) !== -1) { + if (replacedParams.includes(key)) { continue; } @@ -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}`; diff --git a/lib/utils/sequencify.js b/lib/utils/sequencify.js index a979ad82..01707ae0 100644 --- a/lib/utils/sequencify.js +++ b/lib/utils/sequencify.js @@ -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]; @@ -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); diff --git a/test/egg.test.js b/test/egg.test.js index 33e8ee3e..cc051699 100644 --- a/test/egg.test.js +++ b/test/egg.test.js @@ -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`)); } }); @@ -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(); }); }); @@ -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(); }); }); diff --git a/test/loader/get_appname.test.js b/test/loader/get_appname.test.js index 21360ab1..f9907743 100644 --- a/test/loader/get_appname.test.js +++ b/test/loader/get_appname.test.js @@ -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(); } }); diff --git a/test/loader/mixin/load_config.test.js b/test/loader/mixin/load_config.test.js index dd341a7a..3488bcda 100644 --- a/test/loader/mixin/load_config.test.js +++ b/test/loader/mixin/load_config.test.js @@ -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')}`)); } }); diff --git a/test/loader/mixin/load_plugin.test.js b/test/loader/mixin/load_plugin.test.js index 79793967..be170f34 100644 --- a/test/loader/mixin/load_plugin.test.js +++ b/test/loader/mixin/load_plugin.test.js @@ -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() { @@ -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'); @@ -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', diff --git a/test/utils/router.test.js b/test/utils/router.test.js index 5c41d541..101eaa5e 100644 --- a/test/utils/router.test.js +++ b/test/utils/router.test.js @@ -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')); }); });