use includes instead of indexOf

use _.uniq instead of filter&indexOf
v1.18.x
Barış Soner Uşaklı 6 years ago
parent a6c70412db
commit 26d4e0852f

@ -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);
}

@ -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);
},

@ -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']),

@ -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));

@ -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');

@ -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;

@ -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')) {

@ -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);
}),
});
},

@ -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,
});

@ -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;

@ -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 + ']]';

@ -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);

@ -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;
}

@ -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(', ') + ']]'));
}

@ -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);
});
};

@ -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);

@ -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');
}

@ -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);
}
};

@ -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;

@ -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);
};

@ -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);

@ -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);

@ -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) {

@ -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;
}

@ -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`');
}

@ -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');

@ -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;

@ -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([

@ -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);
}

@ -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;
});

@ -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.' :

@ -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);
}
}

@ -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);

@ -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);
},

@ -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,

@ -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,

@ -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,

@ -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;

@ -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);

@ -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;

@ -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();
}

@ -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);

@ -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({

@ -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]]'));
}

@ -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'));

@ -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);
}

@ -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);

@ -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);

@ -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]] || '';
}
});

@ -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);
},

@ -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) {

@ -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);
},

@ -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();

@ -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();
});

@ -218,7 +218,7 @@ describe('Controllers', function () {
assert.equal(res.statusCode, 200);
assert.ok(body);
assert.ok(body.indexOf('<main id="panel"'));
assert.ok(body.indexOf(message) !== -1);
assert.ok(body.includes(message));
done();
});
@ -1056,7 +1056,7 @@ describe('Controllers', function () {
request(nconf.get('url') + '/me/bookmarks', { json: true }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body.indexOf('Login to your account') !== -1);
assert(body.includes('Login to your account'));
done();
});
});
@ -1422,7 +1422,7 @@ describe('Controllers', function () {
var contents = body.posts.map(function (p) {
return p.content;
});
assert(contents.indexOf('1st reply') === -1);
assert(!contents.includes('1st reply'));
done();
});
},
@ -1770,7 +1770,7 @@ describe('Controllers', function () {
request(nconf.get('url') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.af.js', function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body.indexOf('Afrikaans') !== -1);
assert(body.includes('Afrikaans'));
done();
});
});
@ -1779,7 +1779,7 @@ describe('Controllers', function () {
request(nconf.get('url') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.404.js', function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body.indexOf('English') !== -1);
assert(body.includes('English'));
done();
});
});
@ -2108,7 +2108,7 @@ describe('Controllers', function () {
request(nconf.get('url') + '//admin/advanced/database', { json: true }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body.indexOf('Login to your account') !== -1);
assert(body.includes('Login to your account'));
done();
});
});

@ -97,7 +97,7 @@ describe('feeds', function () {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body);
assert(body.indexOf('Login to your account') !== -1);
assert(body.includes('Login to your account'));
privileges.categories.give(['topics:read'], cid, 'guests', done);
});
});
@ -118,7 +118,7 @@ describe('feeds', function () {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body);
assert(body.indexOf('Login to your account') !== -1);
assert(body.includes('Login to your account'));
privileges.categories.give(['read'], cid, 'guests', done);
});
});
@ -159,8 +159,8 @@ describe('feeds', function () {
assert.ifError(err);
assert.equal(results.test1[0].statusCode, 200);
assert.equal(results.test2[0].statusCode, 200);
assert(results.test1[0].body.indexOf('Login to your account') !== -1);
assert(results.test2[0].body.indexOf('Login to your account') !== -1);
assert(results.test1[0].body.includes('Login to your account'));
assert(results.test2[0].body.includes('Login to your account'));
done();
});
});
@ -170,7 +170,7 @@ describe('feeds', function () {
request(nconf.get('url') + '/category/' + cid + '.rss?uid=' + fooUid + '&token=sometoken', { }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body.indexOf('Login to your account') !== -1);
assert(body.includes('Login to your account'));
done();
});
});
@ -194,7 +194,7 @@ describe('feeds', function () {
request(nconf.get('url') + '/category/' + cid + '.rss?uid=' + fooUid + '&token=' + rssToken, { }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body.indexOf('Login to your account') !== -1);
assert(body.includes('Login to your account'));
done();
});
});

@ -1919,8 +1919,8 @@ describe('Topic\'s', function () {
var tags = result.topic.tags.map(function (tag) {
return tag.value;
});
assert(tags.indexOf('tag1') !== -1);
assert(tags.indexOf('tag2') !== -1);
assert(tags.includes('tag1'));
assert(tags.includes('tag2'));
done();
});
});

Loading…
Cancel
Save