From 26d4e0852fd3343c3a7025a84376ae0f8fdf68f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sat, 20 Oct 2018 14:40:48 -0400 Subject: [PATCH] use includes instead of indexOf use _.uniq instead of filter&indexOf --- Gruntfile.js | 2 +- src/categories/activeusers.js | 8 ++---- src/categories/recentreplies.js | 6 +--- src/categories/topics.js | 2 +- src/cli/index.js | 2 +- src/cli/upgrade-plugins.js | 2 +- src/controllers/404.js | 2 +- src/controllers/admin/plugins.js | 2 +- src/controllers/admin/settings.js | 2 +- src/controllers/admin/uploads.js | 2 +- src/controllers/authentication.js | 4 +-- src/controllers/helpers.js | 2 +- src/controllers/mods.js | 4 +-- src/controllers/uploads.js | 2 +- src/database/mongo/sets.js | 6 ++-- src/events.js | 6 +--- src/file.js | 2 +- src/groups.js | 42 +++++++++++----------------- src/groups/data.js | 2 +- src/groups/membership.js | 4 +-- src/groups/search.js | 2 +- src/install.js | 6 ++-- src/messaging.js | 2 +- src/meta/blacklist.js | 18 +++++------- src/meta/build.js | 2 +- src/meta/dependencies.js | 2 +- src/meta/js.js | 2 +- src/middleware/index.js | 2 +- src/notifications.js | 23 ++++++--------- src/pagination.js | 4 +-- src/plugins/hooks.js | 2 +- src/plugins/load.js | 2 +- src/posts/recent.js | 9 +++--- src/posts/summary.js | 19 ++++++------- src/privileges/helpers.js | 6 ++-- src/privileges/posts.js | 18 ++++++------ src/privileges/topics.js | 12 ++++---- src/social.js | 4 +-- src/socket.io/categories.js | 4 +-- src/socket.io/helpers.js | 2 +- src/socket.io/modules.js | 2 +- src/socket.io/topics.js | 2 +- src/socket.io/topics/tools.js | 2 +- src/socket.io/user/status.js | 2 +- src/start.js | 2 +- src/topics/posts.js | 16 ++++------- src/topics/tags.js | 5 ++-- src/upgrade.js | 6 ++-- src/upgrades/1.4.4/sound_settings.js | 4 +-- src/user/categories.js | 4 ++- src/user/data.js | 13 ++++----- src/user/notifications.js | 13 +++------ src/user/settings.js | 2 +- test/controllers-admin.js | 18 ++++++------ test/controllers.js | 12 ++++---- test/feeds.js | 12 ++++---- test/topics.js | 4 +-- 57 files changed, 156 insertions(+), 208 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 2d89ad0178..b18d0574d0 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -37,7 +37,7 @@ module.exports = function (grunt) { // Do nothing, just restart } - if (compiling && incomplete.indexOf(compiling) === -1) { + if (compiling && !incomplete.includes(compiling)) { incomplete.push(compiling); } diff --git a/src/categories/activeusers.js b/src/categories/activeusers.js index 5ce28c4b11..a274b87c54 100644 --- a/src/categories/activeusers.js +++ b/src/categories/activeusers.js @@ -1,6 +1,8 @@ 'use strict'; var async = require('async'); +var _ = require('lodash'); + var posts = require('../posts'); var db = require('../database'); @@ -14,11 +16,7 @@ module.exports = function (Categories) { posts.getPostsFields(pids, ['uid'], next); }, function (posts, next) { - var uids = posts.map(function (post) { - return post.uid; - }).filter(function (uid, index, array) { - return parseInt(uid, 10) && array.indexOf(uid) === index; - }); + var uids = _.uniq(posts.map(post => post.uid).filter(uid => parseInt(uid, 10))); next(null, uids); }, diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js index 13d3be2914..5c3eaa4528 100644 --- a/src/categories/recentreplies.js +++ b/src/categories/recentreplies.js @@ -121,11 +121,7 @@ module.exports = function (Categories) { topic.teaserPid = topic.teaserPid || topic.mainPid; } }); - var cids = _topicData.map(function (topic) { - return topic && topic.cid; - }).filter(function (cid, index, array) { - return cid && array.indexOf(cid) === index; - }); + var cids = _.uniq(topicData.map(topic => topic && topic.cid).filter(cid => parseInt(cid, 10))); async.parallel({ categoryData: async.apply(Categories.getCategoriesFields, cids, ['cid', 'parentCid']), diff --git a/src/categories/topics.js b/src/categories/topics.js index 19634e1dcf..8574321585 100644 --- a/src/categories/topics.js +++ b/src/categories/topics.js @@ -102,7 +102,7 @@ module.exports = function (Categories) { }, function (normalTids, next) { normalTids = normalTids.filter(function (tid) { - return pinnedTids.indexOf(tid) === -1; + return !pinnedTids.includes(tid); }); next(null, pinnedTids.concat(normalTids)); diff --git a/src/cli/index.js b/src/cli/index.js index 20f7c85271..2387185437 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -50,7 +50,7 @@ try { checkVersion('commander'); checkVersion('colors'); } catch (e) { - if (['ENOENT', 'DEP_WRONG_VERSION', 'MODULE_NOT_FOUND'].indexOf(e.code) !== -1) { + if (['ENOENT', 'DEP_WRONG_VERSION', 'MODULE_NOT_FOUND'].includes(e.code)) { console.warn('Dependencies outdated or not yet installed.'); console.log('Installing them now...\n'); diff --git a/src/cli/upgrade-plugins.js b/src/cli/upgrade-plugins.js index a61a711bf7..7a874d5e7d 100644 --- a/src/cli/upgrade-plugins.js +++ b/src/cli/upgrade-plugins.js @@ -201,7 +201,7 @@ function upgradePlugins(callback) { return callback(err); } - if (['y', 'Y', 'yes', 'YES'].indexOf(result.upgrade) !== -1) { + if (['y', 'Y', 'yes', 'YES'].includes(result.upgrade)) { console.log('\nUpgrading packages...'); var args = packageManagerInstallArgs.concat(found.map(function (suggestObj) { return suggestObj.name + '@' + suggestObj.suggested; diff --git a/src/controllers/404.js b/src/controllers/404.js index 44dcf59174..adcc9f68a8 100644 --- a/src/controllers/404.js +++ b/src/controllers/404.js @@ -21,7 +21,7 @@ exports.handle404 = function (req, res) { if (isClientScript.test(req.url)) { res.type('text/javascript').status(200).send(''); - } else if (req.path.startsWith(relativePath + '/assets/uploads') || (req.get('accept') && req.get('accept').indexOf('text/html') === -1) || req.path === '/favicon.ico') { + } else if (req.path.startsWith(relativePath + '/assets/uploads') || (req.get('accept') && !req.get('accept').includes('text/html')) || req.path === '/favicon.ico') { meta.errors.log404(req.path || ''); res.sendStatus(404); } else if (req.accepts('html')) { diff --git a/src/controllers/admin/plugins.js b/src/controllers/admin/plugins.js index b5863e7c9e..5cee3aa598 100644 --- a/src/controllers/admin/plugins.js +++ b/src/controllers/admin/plugins.js @@ -55,7 +55,7 @@ pluginsController.get = function (req, res, next) { return !plugin.installed; }), incompatible: payload.all.filter(function (plugin) { - return compatiblePkgNames.indexOf(plugin.name) === -1; + return !compatiblePkgNames.includes(plugin.name); }), }); }, diff --git a/src/controllers/admin/settings.js b/src/controllers/admin/settings.js index d711e7cdcc..7242cbab1f 100644 --- a/src/controllers/admin/settings.js +++ b/src/controllers/admin/settings.js @@ -36,7 +36,7 @@ function renderEmail(req, res, next) { res.render('admin/settings/email', { emails: results.emails, sendable: results.emails.filter(function (email) { - return email.path.indexOf('_plaintext') === -1 && email.path.indexOf('partials') === -1; + return !email.path.includes('_plaintext') && !email.path.includes('partials'); }), services: results.services, }); diff --git a/src/controllers/admin/uploads.js b/src/controllers/admin/uploads.js index c7952ee98c..8e6ad7cc45 100644 --- a/src/controllers/admin/uploads.js +++ b/src/controllers/admin/uploads.js @@ -261,7 +261,7 @@ function upload(name, req, res, next) { } function validateUpload(req, res, next, uploadedFile, allowedTypes) { - if (allowedTypes.indexOf(uploadedFile.type) === -1) { + if (!allowedTypes.includes(uploadedFile.type)) { file.delete(uploadedFile.path); res.json({ error: '[[error:invalid-image-type, ' + allowedTypes.join(', ') + ']]' }); return false; diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js index 941ec5bcc9..3dae021976 100644 --- a/src/controllers/authentication.js +++ b/src/controllers/authentication.js @@ -221,7 +221,7 @@ authenticationController.login = function (req, res, next) { var loginWith = meta.config.allowLoginWith || 'username-email'; - if (req.body.username && utils.isEmailValid(req.body.username) && loginWith.indexOf('email') !== -1) { + if (req.body.username && utils.isEmailValid(req.body.username) && loginWith.includes('email')) { async.waterfall([ function (next) { user.getUsernameByEmail(req.body.username, next); @@ -231,7 +231,7 @@ authenticationController.login = function (req, res, next) { continueLogin(req, res, next); }, ], next); - } else if (loginWith.indexOf('username') !== -1 && !validator.isEmail(req.body.username)) { + } else if (loginWith.includes('username') && !validator.isEmail(req.body.username)) { continueLogin(req, res, next); } else { var err = '[[error:wrong-login-type-' + loginWith + ']]'; diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js index 636abd77bb..9f445e3aa4 100644 --- a/src/controllers/helpers.js +++ b/src/controllers/helpers.js @@ -268,7 +268,7 @@ function getCategoryData(cids, uid, selectedCid, callback) { var selectedCategory = []; var selectedCids = []; categoryData.forEach(function (category) { - category.selected = selectedCid ? selectedCid.indexOf(String(category.cid)) !== -1 : false; + category.selected = selectedCid ? selectedCid.includes(String(category.cid)) : false; category.parentCid = category.hasOwnProperty('parentCid') && utils.isNumber(category.parentCid) ? category.parentCid : 0; if (category.selected) { selectedCategory.push(category); diff --git a/src/controllers/mods.js b/src/controllers/mods.js index e456f24959..bfdac30f7a 100644 --- a/src/controllers/mods.js +++ b/src/controllers/mods.js @@ -67,9 +67,9 @@ modsController.flags.list = function (req, res, next) { } else if (Array.isArray(filters.cid)) { // Remove cids they do not moderate filters.cid = filters.cid.filter(function (cid) { - return res.locals.cids.indexOf(String(cid)) !== -1; + return res.locals.cids.includes(String(cid)); }); - } else if (res.locals.cids.indexOf(String(filters.cid)) === -1) { + } else if (!res.locals.cids.includes(String(filters.cid))) { filters.cid = res.locals.cids; hasFilter = false; } diff --git a/src/controllers/uploads.js b/src/controllers/uploads.js index 4421438cda..4ab683f45d 100644 --- a/src/controllers/uploads.js +++ b/src/controllers/uploads.js @@ -213,7 +213,7 @@ uploadsController.uploadFile = function (uid, uploadedFile, callback) { var allowed = file.allowedExtensions(); var extension = path.extname(uploadedFile.name).toLowerCase(); - if (allowed.length > 0 && (!extension || extension === '.' || allowed.indexOf(extension) === -1)) { + if (allowed.length > 0 && (!extension || extension === '.' || !allowed.includes(extension))) { return callback(new Error('[[error:invalid-file-type, ' + allowed.join(', ') + ']]')); } diff --git a/src/database/mongo/sets.js b/src/database/mongo/sets.js index b66772639c..7d628d9b69 100644 --- a/src/database/mongo/sets.js +++ b/src/database/mongo/sets.js @@ -117,10 +117,8 @@ module.exports = function (db, module) { return callback(err); } - values = values.map(function (value) { - return !!(items && Array.isArray(items.members) && items.members.indexOf(value) !== -1); - }); - + const membersSet = new Set(items && Array.isArray(items.members) ? items.members : []); + values = values.map(value => membersSet.has(value)); callback(null, values); }); }; diff --git a/src/events.js b/src/events.js index 34dcf15ec4..17c20f6cd1 100644 --- a/src/events.js +++ b/src/events.js @@ -120,11 +120,7 @@ events.getEvents = function (filter, start, stop, callback) { }; function addUserData(eventsData, field, objectName, callback) { - var uids = eventsData.map(function (event) { - return event && event[field]; - }).filter(function (uid, index, array) { - return uid && array.indexOf(uid) === index; - }); + var uids = _.uniq(eventsData.map(event => event && event[field])); if (!uids.length) { return callback(null, eventsData); diff --git a/src/file.js b/src/file.js index a1f5f0580c..1451179c1a 100644 --- a/src/file.js +++ b/src/file.js @@ -137,7 +137,7 @@ file.allowedExtensions = function () { return extension.toLowerCase(); }); - if (allowedExtensions.indexOf('.jpg') !== -1 && allowedExtensions.indexOf('.jpeg') === -1) { + if (allowedExtensions.includes('.jpg') && !allowedExtensions.includes('.jpeg')) { allowedExtensions.push('.jpeg'); } diff --git a/src/groups.js b/src/groups.js index 12a9d48fee..fe832b4e83 100644 --- a/src/groups.js +++ b/src/groups.js @@ -40,7 +40,7 @@ Groups.getEphemeralGroup = function (groupName) { Groups.removeEphemeralGroups = function (groups) { for (var x = groups.length; x >= 0; x -= 1) { - if (Groups.ephemeralGroups.indexOf(groups[x]) !== -1) { + if (Groups.ephemeralGroups.includes(groups[x])) { groups.splice(x, 1); } } @@ -209,7 +209,7 @@ Groups.getOwnersAndMembers = function (groupName, uid, start, stop, callback) { }); results.members = results.members.filter(function (user) { - return user && user.uid && ownerUids.indexOf(user.uid.toString()) === -1; + return user && user.uid && !ownerUids.includes(user.uid.toString()); }); results.members = results.owners.concat(results.members); @@ -266,34 +266,24 @@ function isFieldOn(groupName, field, callback) { Groups.exists = function (name, callback) { if (Array.isArray(name)) { - var slugs = name.map(function (groupName) { - return utils.slugify(groupName); - }); - async.parallel([ - function (next) { - next(null, slugs.map(function (slug) { - return Groups.ephemeralGroups.indexOf(slug) !== -1; - })); - }, + var slugs = name.map(groupName => utils.slugify(groupName)); + async.waterfall([ async.apply(db.isSortedSetMembers, 'groups:createtime', name), - ], function (err, results) { - if (err) { - return callback(err); - } - callback(null, name.map(function (n, index) { - return results[0][index] || results[1][index]; - })); - }); + function (isMembersOfRealGroups, next) { + const isMembersOfEphemeralGroups = slugs.map(slug => Groups.ephemeralGroups.includes(slug)); + const exists = name.map((n, index) => isMembersOfRealGroups[index] || isMembersOfEphemeralGroups[index]); + next(null, exists); + }, + ], callback); } else { var slug = utils.slugify(name); - async.parallel([ - function (next) { - next(null, Groups.ephemeralGroups.indexOf(slug) !== -1); - }, + async.waterfall([ async.apply(db.isSortedSetMember, 'groups:createtime', name), - ], function (err, results) { - callback(err, !err ? (results[0] || results[1]) : null); - }); + function (isMemberOfRealGroups, next) { + const isMemberOfEphemeralGroups = Groups.ephemeralGroups.includes(slug); + next(null, isMemberOfRealGroups || isMemberOfEphemeralGroups); + }, + ], callback); } }; diff --git a/src/groups/data.js b/src/groups/data.js index f791b1f5f1..22b1bd1215 100644 --- a/src/groups/data.js +++ b/src/groups/data.js @@ -19,7 +19,7 @@ module.exports = function (Groups) { }); var ephemeralIdx = groupNames.reduce(function (memo, cur, idx) { - if (Groups.ephemeralGroups.indexOf(cur) !== -1) { + if (Groups.ephemeralGroups.includes(cur)) { memo.push(idx); } return memo; diff --git a/src/groups/membership.js b/src/groups/membership.js index bca8d57778..0e7414193d 100644 --- a/src/groups/membership.js +++ b/src/groups/membership.js @@ -265,14 +265,14 @@ module.exports = function (Groups) { }, function (groupNames, next) { groupNames = Groups.removeEphemeralGroups(groupNames); - if (groupNames.length === 0) { + if (!groupNames.length) { return callback(null, false); } Groups.isMemberOfGroups(uid, groupNames, next); }, function (isMembers, next) { - next(null, isMembers.indexOf(true) !== -1); + next(null, isMembers.includes(true)); }, ], callback); }; diff --git a/src/groups/search.js b/src/groups/search.js index b6a9db00f8..14b20a084f 100644 --- a/src/groups/search.js +++ b/src/groups/search.js @@ -18,7 +18,7 @@ module.exports = function (Groups) { // Ephemeral groups and the registered-users groups are searchable groupNames = Groups.ephemeralGroups.concat(groupNames).concat('registered-users'); groupNames = groupNames.filter(function (name) { - return name.toLowerCase().indexOf(query) !== -1 && name !== 'administrators' && !Groups.isPrivilegeGroup(name); + return name.toLowerCase().includes(query) && name !== 'administrators' && !Groups.isPrivilegeGroup(name); }); groupNames = groupNames.slice(0, 100); Groups.getGroupsData(groupNames, next); diff --git a/src/install.js b/src/install.js index 5ef191cf44..078a2da2d0 100644 --- a/src/install.js +++ b/src/install.js @@ -7,6 +7,8 @@ var path = require('path'); var prompt = require('prompt'); var winston = require('winston'); var nconf = require('nconf'); +var _ = require('lodash'); + var utils = require('./utils.js'); var install = module.exports; @@ -488,9 +490,7 @@ function enableDefaultPlugins(next) { } } - defaultEnabled = defaultEnabled.filter(function (plugin, index, array) { - return array.indexOf(plugin) === index; - }); + defaultEnabled = _.uniq(defaultEnabled); winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled); diff --git a/src/messaging.js b/src/messaging.js index 543d6d03a7..7a32c43df1 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -353,7 +353,7 @@ Messaging.hasPrivateChat = function (uid, withUid, callback) { }, function (results, next) { var roomIds = results.myRooms.filter(function (roomId) { - return roomId && results.theirRooms.indexOf(roomId) !== -1; + return roomId && results.theirRooms.includes(roomId); }); if (!roomIds.length) { diff --git a/src/meta/blacklist.js b/src/meta/blacklist.js index c349a76cb0..eced8ea413 100644 --- a/src/meta/blacklist.js +++ b/src/meta/blacklist.js @@ -3,6 +3,7 @@ var ipaddr = require('ipaddr.js'); var winston = require('winston'); var async = require('async'); +var _ = require('lodash'); var db = require('../database'); var pubsub = require('../pubsub'); @@ -79,8 +80,8 @@ Blacklist.test = function (clientIp, callback) { } if ( - Blacklist._rules.ipv4.indexOf(clientIp) === -1 && // not explicitly specified in ipv4 list - Blacklist._rules.ipv6.indexOf(clientIp) === -1 && // not explicitly specified in ipv6 list + !Blacklist._rules.ipv4.includes(clientIp) && // not explicitly specified in ipv4 list + !Blacklist._rules.ipv6.includes(clientIp) && // not explicitly specified in ipv6 list !Blacklist._rules.cidr.some(function (subnet) { var cidr = ipaddr.parseCIDR(subnet); if (addr.kind() !== cidr[0].kind()) { @@ -127,14 +128,9 @@ Blacklist.validate = function (rules, callback) { }).filter(Boolean); // Filter out duplicates - rules = rules.filter(function (rule, index) { - const pass = rules.indexOf(rule) === index; - if (!pass) { - duplicateCount += 1; - } - - return pass; - }); + const uniqRules = _.uniq(rules); + duplicateCount += rules.length - uniqRules.length; + rules = uniqRules; // Filter out invalid rules rules = rules.filter(function (rule) { @@ -153,7 +149,7 @@ Blacklist.validate = function (rules, callback) { // Do nothing } - if (!addr || whitelist.indexOf(rule) !== -1) { + if (!addr || whitelist.includes(rule)) { invalid.push(rule); return false; } diff --git a/src/meta/build.js b/src/meta/build.js index 62288dad3a..c5fc937d30 100644 --- a/src/meta/build.js +++ b/src/meta/build.js @@ -156,7 +156,7 @@ function build(targets, options, callback) { target = target.toLowerCase().replace(/-/g, ''); if (!aliases[target]) { winston.warn('[build] Unknown target: ' + target); - if (target.indexOf(',') !== -1) { + if (target.includes(',')) { winston.warn('[build] Are you specifying multiple targets? Separate them with spaces:'); winston.warn('[build] e.g. `./nodebb build adminjs tpl`'); } diff --git a/src/meta/dependencies.js b/src/meta/dependencies.js index 095b48e19d..439d0b628b 100644 --- a/src/meta/dependencies.js +++ b/src/meta/dependencies.js @@ -72,7 +72,7 @@ Dependencies.doesSatisfy = function (moduleData, packageJSONVersion) { return false; } var versionOk = !semver.validRange(packageJSONVersion) || semver.satisfies(moduleData.version, packageJSONVersion); - var githubRepo = moduleData._resolved && moduleData._resolved.indexOf('//github.com') !== -1; + var githubRepo = moduleData._resolved && moduleData._resolved.includes('//github.com'); var satisfies = versionOk || githubRepo; if (!satisfies) { winston.warn('[' + 'outdated'.yellow + '] ' + moduleData.name.bold + ' installed v' + moduleData.version + ', package.json requires ' + packageJSONVersion + '\n'); diff --git a/src/meta/js.js b/src/meta/js.js index 4e974c74e8..230d7ef54a 100644 --- a/src/meta/js.js +++ b/src/meta/js.js @@ -115,7 +115,7 @@ var basePath = path.resolve(__dirname, '../..'); function minifyModules(modules, fork, callback) { var moduleDirs = modules.reduce(function (prev, mod) { var dir = path.resolve(path.dirname(mod.destPath)); - if (prev.indexOf(dir) === -1) { + if (!prev.includes(dir)) { prev.push(dir); } return prev; diff --git a/src/middleware/index.js b/src/middleware/index.js index 5eba1ed3dc..0af7e7a6ee 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -171,7 +171,7 @@ middleware.applyBlacklist = function (req, res, next) { }; middleware.processTimeagoLocales = function (req, res, next) { - var fallback = req.path.indexOf('-short') === -1 ? 'jquery.timeago.en.js' : 'jquery.timeago.en-short.js'; + var fallback = !req.path.includes('-short') ? 'jquery.timeago.en.js' : 'jquery.timeago.en-short.js'; var localPath = path.join(__dirname, '../../public/vendor/jquery/timeago/locales', req.path); async.waterfall([ diff --git a/src/notifications.js b/src/notifications.js index e45625e6e4..ac79722384 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -122,27 +122,20 @@ Notifications.filterExists = function (nids, callback) { Notifications.findRelated = function (mergeIds, set, callback) { // A related notification is one in a zset that has the same mergeId - var _nids; + var nids; async.waterfall([ async.apply(db.getSortedSetRevRange, set, 0, -1), - function (nids, next) { - _nids = nids; - - var keys = nids.map(function (nid) { - return 'notifications:' + nid; - }); + function (_nids, next) { + nids = _nids; + var keys = nids.map(nid => 'notifications:' + nid); db.getObjectsFields(keys, ['mergeId'], next); }, function (sets, next) { - sets = sets.map(function (set) { - return set.mergeId; - }); - - next(null, _nids.filter(function (nid, idx) { - return mergeIds.indexOf(sets[idx]) !== -1; - })); + sets = sets.map(set => set.mergeId); + var mergeSet = new Set(mergeIds); + next(null, nids.filter((nid, idx) => mergeSet.has(sets[idx]))); }, ], callback); }; @@ -536,7 +529,7 @@ Notifications.merge = function (notifications, callback) { // Each isolated mergeId may have multiple differentiators, so process each separately differentiators = isolated.reduce(function (cur, next) { differentiator = next.mergeId.split('|')[1] || 0; - if (cur.indexOf(differentiator) === -1) { + if (!cur.includes(differentiator)) { cur.push(differentiator); } diff --git a/src/pagination.js b/src/pagination.js index 5789573b92..83b2acf55c 100644 --- a/src/pagination.js +++ b/src/pagination.js @@ -32,9 +32,7 @@ pagination.create = function (currentPage, pageCount, queryObj) { pagesToShow.push(startPage + i); } - pagesToShow = pagesToShow.filter(function (page, index, array) { - return page > 0 && page <= pageCount && array.indexOf(page) === index; - }).sort(function (a, b) { + pagesToShow = _.uniq(pagesToShow).filter(page => page > 0 && page <= pageCount).sort(function (a, b) { return a - b; }); diff --git a/src/plugins/hooks.js b/src/plugins/hooks.js index 755a5e6e7f..d5f268a80e 100644 --- a/src/plugins/hooks.js +++ b/src/plugins/hooks.js @@ -33,7 +33,7 @@ module.exports = function (Plugins) { var method; - if (Object.keys(Plugins.deprecatedHooks).indexOf(data.hook) !== -1) { + if (Object.keys(Plugins.deprecatedHooks).includes(data.hook)) { winston.warn('[plugins/' + id + '] Hook `' + data.hook + '` is deprecated, ' + (Plugins.deprecatedHooks[data.hook] ? 'please use `' + Plugins.deprecatedHooks[data.hook] + '` instead.' : diff --git a/src/plugins/load.js b/src/plugins/load.js index 1f36b4eb55..4a47b24c21 100644 --- a/src/plugins/load.js +++ b/src/plugins/load.js @@ -157,7 +157,7 @@ module.exports = function (Plugins) { function checkVersion(pluginData) { function add() { - if (Plugins.versionWarning.indexOf(pluginData.id) === -1) { + if (!Plugins.versionWarning.includes(pluginData.id)) { Plugins.versionWarning.push(pluginData.id); } } diff --git a/src/posts/recent.js b/src/posts/recent.js index 320febff19..22df0c5895 100644 --- a/src/posts/recent.js +++ b/src/posts/recent.js @@ -1,6 +1,8 @@ 'use strict'; var async = require('async'); +var _ = require('lodash'); + var db = require('../database'); var privileges = require('../privileges'); @@ -42,11 +44,8 @@ module.exports = function (Posts) { Posts.getPostsFields(pids, ['uid'], next); }, function (postData, next) { - var uids = postData.map(function (post) { - return post && post.uid; - }).filter(function (uid, index, array) { - return uid && array.indexOf(uid) === index; - }); + var uids = _.uniq(postData.map(post => post && post.uid).filter(uid => parseInt(uid, 10))); + next(null, uids); }, ], callback); diff --git a/src/posts/summary.js b/src/posts/summary.js index 139fea16a8..f9dfe7f182 100644 --- a/src/posts/summary.js +++ b/src/posts/summary.js @@ -33,23 +33,20 @@ module.exports = function (Posts) { user.blocks.filter(uid, posts, next); }, function (_posts, next) { - var uids = []; - var topicKeys = []; + posts = _posts; + var uids = {}; + var topicKeys = {}; - posts.forEach(function (post, i) { - if (uids.indexOf(posts[i].uid) === -1) { - uids.push(posts[i].uid); - } - if (topicKeys.indexOf(posts[i].tid) === -1) { - topicKeys.push(posts[i].tid); - } + posts.forEach(function (post) { + uids[post.uid] = 1; + topicKeys[post.tid] = 1; }); async.parallel({ users: function (next) { - user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next); + user.getUsersFields(Object.keys(uids), ['uid', 'username', 'userslug', 'picture'], next); }, topicsAndCategories: function (next) { - getTopicAndCategories(topicKeys, next); + getTopicAndCategories(Object.keys(topicKeys), next); }, }, next); }, diff --git a/src/privileges/helpers.js b/src/privileges/helpers.js index cb57870853..49002c8680 100644 --- a/src/privileges/helpers.js +++ b/src/privileges/helpers.js @@ -147,7 +147,7 @@ helpers.getUserPrivileges = function (cid, hookName, userPrivilegeList, callback memberData.forEach(function (member) { member.privileges = {}; for (var x = 0, numPrivs = userPrivileges.length; x < numPrivs; x += 1) { - member.privileges[userPrivileges[x]] = memberSets[x].indexOf(parseInt(member.uid, 10)) !== -1; + member.privileges[userPrivileges[x]] = memberSets[x].includes(parseInt(member.uid, 10)); } }); @@ -178,7 +178,7 @@ helpers.getGroupPrivileges = function (cid, hookName, groupPrivilegeList, callba var uniqueGroups = _.uniq(_.flatten(memberSets)); var groupNames = results.groupNames.filter(function (groupName) { - return groupName.indexOf(':privileges:') === -1 && uniqueGroups.indexOf(groupName) !== -1; + return !groupName.includes(':privileges:') && uniqueGroups.includes(groupName); }); groupNames = groups.ephemeralGroups.concat(groupNames); @@ -200,7 +200,7 @@ helpers.getGroupPrivileges = function (cid, hookName, groupPrivilegeList, callba memberPrivs = {}; for (var x = 0, numPrivs = groupPrivileges.length; x < numPrivs; x += 1) { - memberPrivs[groupPrivileges[x]] = memberSets[x].indexOf(member) !== -1; + memberPrivs[groupPrivileges[x]] = memberSets[x].includes(member); } return { name: member, diff --git a/src/privileges/posts.js b/src/privileges/posts.js index 60ed7ed1d3..02f9b7136e 100644 --- a/src/privileges/posts.js +++ b/src/privileges/posts.js @@ -88,9 +88,8 @@ module.exports = function (privileges) { }, function (_posts, next) { postData = _posts; - tids = _.uniq(_posts.map(function (post) { - return post && post.tid; - }).filter(Boolean)); + + tids = _.uniq(_posts.map(post => post && post.tid).filter(Boolean)); topics.getTopicsFields(tids, ['deleted', 'cid'], next); }, @@ -107,9 +106,9 @@ module.exports = function (privileges) { post.topic = tidToTopic[post.tid]; } return tidToTopic[post.tid] && tidToTopic[post.tid].cid; - }).filter(function (cid, index, array) { - return cid && array.indexOf(cid) === index; - }); + }).filter(cid => parseInt(cid, 10)); + + cids = _.uniq(cids); privileges.categories.getBase(privilege, cids, uid, next); }, @@ -121,13 +120,12 @@ module.exports = function (privileges) { (results.allowedTo[index] || results.isAdmin || results.isModerators[index]); }); + const cidsSet = new Set(cids); pids = postData.filter(function (post) { - return post.topic && cids.indexOf(post.topic.cid) !== -1 && + return post.topic && cidsSet.has(post.topic.cid) && ((parseInt(post.topic.deleted, 10) !== 1 && parseInt(post.deleted, 10) !== 1) || results.isAdmin || isModOf[post.cid]); - }).map(function (post) { - return post.pid; - }); + }).map(post => post.pid); plugins.fireHook('filter:privileges.posts.filter', { privilege: privilege, diff --git a/src/privileges/topics.js b/src/privileges/topics.js index 9e387acc77..0a5960178a 100644 --- a/src/privileges/topics.js +++ b/src/privileges/topics.js @@ -86,9 +86,7 @@ module.exports = function (privileges) { }, function (_topicsData, next) { topicsData = _topicsData; - cids = _.uniq(topicsData.map(function (topic) { - return topic.cid; - })); + cids = _.uniq(topicsData.map(topic => topic.cid)); privileges.categories.getBase(privilege, cids, uid, next); }, @@ -100,12 +98,12 @@ module.exports = function (privileges) { (results.allowedTo[index] || results.isAdmin || results.isModerators[index]); }); + const cidsSet = new Set(cids); + tids = topicsData.filter(function (topic) { - return cids.indexOf(topic.cid) !== -1 && + return cidsSet.has(topic.cid) && (parseInt(topic.deleted, 10) !== 1 || results.isAdmin || isModOf[topic.cid]); - }).map(function (topic) { - return topic.tid; - }); + }).map(topic => topic.tid); plugins.fireHook('filter:privileges.topics.filter', { privilege: privilege, diff --git a/src/social.js b/src/social.js index e26b497d21..c1315e54e6 100644 --- a/src/social.js +++ b/src/social.js @@ -39,8 +39,8 @@ social.getPostSharing = function (callback) { db.getSetMembers('social:posts.activated', next); }, function (activated, next) { - networks.forEach(function (network, i) { - networks[i].activated = (activated.indexOf(network.id) !== -1); + networks.forEach(function (network) { + network.activated = activated.includes(network.id); }); social.postSharing = networks; diff --git a/src/socket.io/categories.js b/src/socket.io/categories.js index 72a5785a55..3a1ebf883b 100644 --- a/src/socket.io/categories.js +++ b/src/socket.io/categories.js @@ -47,7 +47,7 @@ SocketCategories.getWatchedCategories = function (socket, data, callback) { }, function (results, next) { var watchedCategories = results.categories.filter(function (category) { - return category && results.ignoredCids.indexOf(category.cid.toString()) === -1; + return category && !results.ignoredCids.includes(String(category.cid)); }); next(null, watchedCategories); @@ -197,7 +197,7 @@ function ignoreOrWatch(fn, socket, cid, callback) { var cat; do { cat = categoryData.find(function (c) { - return cids.indexOf(c.cid) === -1 && cids.indexOf(c.parentCid) !== -1; + return !cids.includes(c.cid) && cids.includes(c.parentCid); }); if (cat) { cids.push(cat.cid); diff --git a/src/socket.io/helpers.js b/src/socket.io/helpers.js index 1688d88252..775e3820e3 100644 --- a/src/socket.io/helpers.js +++ b/src/socket.io/helpers.js @@ -199,7 +199,7 @@ SocketHelpers.upvote = function (data, notification) { return votes > 0 && votes % 10 === 0; }, threshold: function () { - return [1, 5, 10, 25].indexOf(votes) !== -1 || (votes >= 50 && votes % 50 === 0); + return [1, 5, 10, 25].includes(votes) || (votes >= 50 && votes % 50 === 0); }, logarithmic: function () { return votes > 1 && Math.log10(votes) % 1 === 0; diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js index 30459f75eb..2a169a3cfa 100644 --- a/src/socket.io/modules.js +++ b/src/socket.io/modules.js @@ -318,7 +318,7 @@ SocketModules.chats.markRead = function (socket, roomId, callback) { Messaging.pushUnreadCount(socket.uid); server.in('uid_' + socket.uid).emit('event:chats.markedAsRead', { roomId: roomId }); - if (results.uidsInRoom.indexOf(socket.uid.toString()) === -1) { + if (!results.uidsInRoom.includes(String(socket.uid))) { return callback(); } diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index bbeda1bee5..aa8bc07e25 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -101,7 +101,7 @@ SocketTopics.changeWatching = function (socket, data, callback) { return callback(new Error('[[error:invalid-data]]')); } var commands = ['follow', 'unfollow', 'ignore']; - if (commands.indexOf(data.type) === -1) { + if (!commands.includes(data.type)) { return callback(new Error('[[error:invalid-command]]')); } followCommand(topics[data.type], socket, data.tid, callback); diff --git a/src/socket.io/topics/tools.js b/src/socket.io/topics/tools.js index 6045353c33..4c133d1d4e 100644 --- a/src/socket.io/topics/tools.js +++ b/src/socket.io/topics/tools.js @@ -105,7 +105,7 @@ module.exports = function (SocketTopics) { function logTopicAction(action, socket, tid, title, callback) { var actionsToLog = ['delete', 'restore', 'purge']; - if (actionsToLog.indexOf(action) === -1) { + if (!actionsToLog.includes(action)) { return setImmediate(callback); } events.log({ diff --git a/src/socket.io/user/status.js b/src/socket.io/user/status.js index ccae98ff4c..dbb0d3344b 100644 --- a/src/socket.io/user/status.js +++ b/src/socket.io/user/status.js @@ -26,7 +26,7 @@ module.exports = function (SocketUser) { } var allowedStatus = ['online', 'offline', 'dnd', 'away']; - if (allowedStatus.indexOf(status) === -1) { + if (!allowedStatus.includes(status)) { return callback(new Error('[[error:invalid-user-status]]')); } diff --git a/src/start.js b/src/start.js index d16e4d3a34..a9b11c56ba 100644 --- a/src/start.js +++ b/src/start.js @@ -107,7 +107,7 @@ function printStartupInfo() { winston.info('Initializing NodeBB v%s %s', nconf.get('version'), nconf.get('url')); var host = nconf.get(nconf.get('database') + ':host'); - var storeLocation = host ? 'at ' + host + (host.indexOf('/') === -1 ? ':' + nconf.get(nconf.get('database') + ':port') : '') : ''; + var storeLocation = host ? 'at ' + host + (!host.includes('/') ? ':' + nconf.get(nconf.get('database') + ':port') : '') : ''; winston.verbose('* using %s store %s', nconf.get('database'), storeLocation); winston.verbose('* using themes stored in: %s', nconf.get('themes_path')); diff --git a/src/topics/posts.js b/src/topics/posts.js index 7e1140e5fa..8c131cfe9e 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -57,24 +57,20 @@ module.exports = function (Topics) { } function getPostUserData(field, method, callback) { - var uids = []; + var uidsMap = {}; - postData.forEach(function (postData) { - if (postData && parseInt(postData[field], 10) >= 0 && uids.indexOf(postData[field]) === -1) { - uids.push(postData[field]); + postData.forEach((post) => { + if (post && parseInt(post[field], 10) >= 0) { + uidsMap[post[field]] = 1; } }); - + const uids = Object.keys(uidsMap); async.waterfall([ function (next) { method(uids, next); }, function (users, next) { - var userData = {}; - users.forEach(function (user, index) { - userData[uids[index]] = user; - }); - next(null, userData); + next(null, _.zipObject(uids, users)); }, ], callback); } diff --git a/src/topics/tags.js b/src/topics/tags.js index 6d725c2269..8b2e1c3972 100644 --- a/src/topics/tags.js +++ b/src/topics/tags.js @@ -65,9 +65,8 @@ module.exports = function (Topics) { if (!tagWhitelist.length) { return next(null, tags); } - tags = tags.filter(function (tag) { - return tagWhitelist.indexOf(tag) !== -1; - }); + var whitelistSet = new Set(tagWhitelist); + tags = tags.filter(tag => whitelistSet.has(tag)); next(null, tags); }, ], callback); diff --git a/src/upgrade.js b/src/upgrade.js index 89541ec9f2..1b698480da 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -87,7 +87,7 @@ Upgrade.check = function (callback) { } var remainder = files.filter(function (name) { - return executed.indexOf(path.basename(name, '.js')) === -1; + return !executed.includes(path.basename(name, '.js')); }); next(remainder.length > 0 ? new Error('schema-out-of-date') : null); @@ -112,7 +112,7 @@ Upgrade.run = function (callback) { } queue = data.available.reduce(function (memo, cur) { - if (data.completed.indexOf(path.basename(cur, '.js')) === -1) { + if (!data.completed.includes(path.basename(cur, '.js'))) { memo.push(cur); } else { skipped += 1; @@ -132,7 +132,7 @@ Upgrade.runParticular = function (names, callback) { async.apply(file.walk, path.join(__dirname, './upgrades')), function (files, next) { var upgrades = files.filter(function (file) { - return names.indexOf(path.basename(file, '.js')) !== -1; + return names.includes(path.basename(file, '.js')); }); Upgrade.process(upgrades, 0, next); diff --git a/src/upgrades/1.4.4/sound_settings.js b/src/upgrades/1.4.4/sound_settings.js index 57768fcc65..7ea381bcca 100644 --- a/src/upgrades/1.4.4/sound_settings.js +++ b/src/upgrades/1.4.4/sound_settings.js @@ -27,7 +27,7 @@ module.exports = { } keys.forEach(function (key) { - if (settings[key] && settings[key].indexOf(' | ') === -1) { + if (settings[key] && !settings[key].includes(' | ')) { settings[key] = map[settings[key]] || ''; } }); @@ -46,7 +46,7 @@ module.exports = { } var newSettings = {}; keys.forEach(function (key) { - if (settings[key] && settings[key].indexOf(' | ') === -1) { + if (settings[key] && !settings[key].includes(' | ')) { newSettings[key] = map[settings[key]] || ''; } }); diff --git a/src/user/categories.js b/src/user/categories.js index dd8afc4f03..c88eac802d 100644 --- a/src/user/categories.js +++ b/src/user/categories.js @@ -23,8 +23,10 @@ module.exports = function (User) { }, next); }, function (results, next) { + const ignored = new Set(results.ignored); + var watched = results.all.filter(function (cid) { - return cid && results.ignored.indexOf(cid) === -1; + return cid && !ignored.has(cid); }); next(null, watched); }, diff --git a/src/user/data.js b/src/user/data.js index dc851dc8c4..25739eda03 100644 --- a/src/user/data.js +++ b/src/user/data.js @@ -4,6 +4,7 @@ var async = require('async'); var validator = require('validator'); var nconf = require('nconf'); var winston = require('winston'); +var _ = require('lodash'); var db = require('../database'); var meta = require('../meta'); @@ -49,27 +50,25 @@ module.exports = function (User) { var fieldsToRemove = []; function addField(field) { - if (fields.indexOf(field) === -1) { + if (!fields.includes(field)) { fields.push(field); fieldsToRemove.push(field); } } - if (fields.length && fields.indexOf('uid') === -1) { + if (fields.length && !fields.includes('uid')) { fields.push('uid'); } - if (fields.indexOf('picture') !== -1) { + if (fields.includes('picture')) { addField('uploadedpicture'); } - if (fields.indexOf('status') !== -1) { + if (fields.includes('status')) { addField('lastonline'); } - var uniqueUids = uids.filter(function (uid, index) { - return index === uids.indexOf(uid); - }); + var uniqueUids = _.uniq(uids); async.waterfall([ function (next) { diff --git a/src/user/notifications.js b/src/user/notifications.js index 851a2d2e94..563c06eccf 100644 --- a/src/user/notifications.js +++ b/src/user/notifications.js @@ -225,19 +225,14 @@ UserNotifications.getUnreadByField = function (uid, field, values, callback) { return callback(null, []); } - var keys = nids.map(function (nid) { - return 'notifications:' + nid; - }); - + var keys = nids.map(nid => 'notifications:' + nid); db.getObjectsFields(keys, ['nid', field], next); }, function (notifications, next) { - values = values.map(function () { return values.toString(); }); + const valuesSet = new Set(values.map(value => String(value))); nids = notifications.filter(function (notification) { - return notification && notification[field] && values.indexOf(notification[field].toString()) !== -1; - }).map(function (notification) { - return notification.nid; - }); + return notification && notification[field] && valuesSet.has(String(notification[field])); + }).map(notification => notification.nid); next(null, nids); }, diff --git a/src/user/settings.js b/src/user/settings.js index 202c440d14..9d8307b475 100644 --- a/src/user/settings.js +++ b/src/user/settings.js @@ -176,7 +176,7 @@ module.exports = function (User) { db.sortedSetsRemove(['digest:day:uids', 'digest:week:uids', 'digest:month:uids'], uid, next); }, function (next) { - if (['day', 'week', 'month'].indexOf(dailyDigestFreq) !== -1) { + if (['day', 'week', 'month'].includes(dailyDigestFreq)) { db.sortedSetAdd('digest:' + dailyDigestFreq + ':uids', Date.now(), uid, next); } else { next(); diff --git a/test/controllers-admin.js b/test/controllers-admin.js index 425c8c029b..eba66ff653 100644 --- a/test/controllers-admin.js +++ b/test/controllers-admin.js @@ -481,9 +481,9 @@ describe('Admin Controllers', function () { body = body.posts.map(function (network) { return network && network.id; }); - assert(body.indexOf('facebook') !== -1); - assert(body.indexOf('twitter') !== -1); - assert(body.indexOf('google') !== -1); + assert(body.includes('facebook')); + assert(body.includes('twitter')); + assert(body.includes('google')); done(); }); }); @@ -665,16 +665,16 @@ describe('Admin Controllers', function () { assert.ifError(err); assert.equal(res.statusCode, 200); assert(body); - assert(body.indexOf('"someValue":"\\\\"foo\\\\""') !== -1); - assert(body.indexOf('"otherValue":"\\\'123\\\'"') !== -1); - assert(body.indexOf('"script":"<\\/script>"') !== -1); + assert(body.includes('"someValue":"\\\\"foo\\\\""')); + assert(body.includes('"otherValue":"\\\'123\\\'"')); + assert(body.includes('"script":"<\\/script>"')); request(nconf.get('url'), { jar: jar }, function (err, res, body) { assert.ifError(err); assert.equal(res.statusCode, 200); assert(body); - assert(body.indexOf('"someValue":"\\\\"foo\\\\""') !== -1); - assert(body.indexOf('"otherValue":"\\\'123\\\'"') !== -1); - assert(body.indexOf('"script":"<\\/script>"') !== -1); + assert(body.includes('"someValue":"\\\\"foo\\\\""')); + assert(body.includes('"otherValue":"\\\'123\\\'"')); + assert(body.includes('"script":"<\\/script>"')); plugins.unregisterHook('somePlugin', 'filter:config.get', onConfigGet); done(); }); diff --git a/test/controllers.js b/test/controllers.js index 61d70f05f3..e79d4da0db 100644 --- a/test/controllers.js +++ b/test/controllers.js @@ -218,7 +218,7 @@ describe('Controllers', function () { assert.equal(res.statusCode, 200); assert.ok(body); assert.ok(body.indexOf('