use includes instead of indexOf

use _.uniq instead of filter&indexOf
This commit is contained in:
Barış Soner Uşaklı
2018-10-20 14:40:48 -04:00
parent a6c70412db
commit 26d4e0852f
57 changed files with 156 additions and 208 deletions

View File

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

View File

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