From c738dc7d3e102afa892c1a32a0aded58984039c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 22 Oct 2018 21:58:34 -0400 Subject: [PATCH] remove some more parseInts --- src/categories/activeusers.js | 2 +- src/categories/create.js | 29 ++++++++++++----------------- src/categories/delete.js | 5 +---- src/categories/recentreplies.js | 15 +++++---------- src/categories/topics.js | 10 +++------- src/categories/unread.js | 16 +++------------- src/categories/update.js | 6 +----- test/categories.js | 21 ++++++++++++++++++++- test/database/sets.js | 7 +++++++ 9 files changed, 53 insertions(+), 58 deletions(-) diff --git a/src/categories/activeusers.js b/src/categories/activeusers.js index a274b87c54..40ff7bc016 100644 --- a/src/categories/activeusers.js +++ b/src/categories/activeusers.js @@ -16,7 +16,7 @@ module.exports = function (Categories) { posts.getPostsFields(pids, ['uid'], next); }, function (posts, next) { - var uids = _.uniq(posts.map(post => post.uid).filter(uid => parseInt(uid, 10))); + var uids = _.uniq(posts.map(post => post.uid).filter(uid => uid)); next(null, uids); }, diff --git a/src/categories/create.js b/src/categories/create.js index 6a5d9c075f..42fdfed919 100644 --- a/src/categories/create.js +++ b/src/categories/create.js @@ -82,26 +82,21 @@ module.exports = function (Categories) { ], next); }, function (results, next) { - async.series([ - function (next) { - if (data.cloneFromCid && parseInt(data.cloneFromCid, 10)) { - return Categories.copySettingsFrom(data.cloneFromCid, category.cid, !data.parentCid, next); - } + if (data.cloneFromCid && parseInt(data.cloneFromCid, 10)) { + return Categories.copySettingsFrom(data.cloneFromCid, category.cid, !data.parentCid, next); + } - next(); - }, - function (next) { - if (data.cloneChildren) { - return duplicateCategoriesChildren(category.cid, data.cloneFromCid, data.uid, next); - } + next(null, category); + }, + function (_category, next) { + category = _category; + if (data.cloneChildren) { + return duplicateCategoriesChildren(category.cid, data.cloneFromCid, data.uid, next); + } - next(); - }, - ], function (err) { - next(err, category); - }); + next(); }, - function (category, next) { + function (next) { plugins.fireHook('action:category.create', { category: category }); next(null, category); }, diff --git a/src/categories/delete.js b/src/categories/delete.js index 4e3a7060ce..eacc336ea7 100644 --- a/src/categories/delete.js +++ b/src/categories/delete.js @@ -58,9 +58,7 @@ module.exports = function (Categories) { ], next); }, function (next) { - groups.destroy(privileges.privilegeList.map(function (privilege) { - return 'cid:' + cid + ':privileges:' + privilege; - }), next); + groups.destroy(privileges.privilegeList.map(privilege => 'cid:' + cid + ':privileges:' + privilege), next); }, ], function (err) { callback(err); @@ -82,7 +80,6 @@ module.exports = function (Categories) { function (results, next) { async.parallel([ function (next) { - results.parentCid = parseInt(results.parentCid, 10) || 0; db.sortedSetRemove('cid:' + results.parentCid + ':children', cid, next); }, function (next) { diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js index 5c3eaa4528..a4149a0b80 100644 --- a/src/categories/recentreplies.js +++ b/src/categories/recentreplies.js @@ -69,7 +69,7 @@ module.exports = function (Categories) { posts.getPostField(pid, 'tid', next); }, function (tid, next) { - if (!parseInt(tid, 10)) { + if (!tid) { return next(); } @@ -85,9 +85,7 @@ module.exports = function (Categories) { async.waterfall([ function (next) { - var keys = categoryData.map(function (category) { - return 'cid:' + category.cid + ':recent_tids'; - }); + var keys = categoryData.map(category => 'cid:' + category.cid + ':recent_tids'); db.getSortedSetsMembers(keys, next); }, function (results, next) { @@ -154,12 +152,9 @@ module.exports = function (Categories) { function assignTopicsToCategories(categories, topics) { categories.forEach(function (category) { - category.posts = topics.filter(function (topic) { - return topic.cid && (parseInt(topic.cid, 10) === parseInt(category.cid, 10) || - parseInt(topic.parentCid, 10) === parseInt(category.cid, 10)); - }).sort(function (a, b) { - return b.pid - a.pid; - }).slice(0, parseInt(category.numRecentReplies, 10)); + category.posts = topics.filter(topic => topic.cid && (topic.cid === category.cid || topic.parentCid === category.cid)) + .sort((a, b) => b.pid - a.pid) + .slice(0, parseInt(category.numRecentReplies, 10)); }); } diff --git a/src/categories/topics.js b/src/categories/topics.js index 8574321585..b7eafae95e 100644 --- a/src/categories/topics.js +++ b/src/categories/topics.js @@ -101,9 +101,7 @@ module.exports = function (Categories) { } }, function (normalTids, next) { - normalTids = normalTids.filter(function (tid) { - return !pinnedTids.includes(tid); - }); + normalTids = normalTids.filter(tid => !pinnedTids.includes(tid)); next(null, pinnedTids.concat(normalTids)); }, @@ -150,9 +148,7 @@ module.exports = function (Categories) { if (data.tag) { if (Array.isArray(data.tag)) { - set = [set].concat(data.tag.map(function (tag) { - return 'tag:' + tag + ':topics'; - })); + set = [set].concat(data.tag.map(tag => 'tag:' + tag + ':topics')); } else { set = [set, 'tag:' + data.tag + ':topics']; } @@ -225,7 +221,7 @@ module.exports = function (Categories) { db.incrObjectField('category:' + cid, 'post_count', next); }, function (next) { - if (parseInt(pinned, 10) === 1) { + if (pinned) { return setImmediate(next); } diff --git a/src/categories/unread.js b/src/categories/unread.js index 498f5e65dc..d1869b5620 100644 --- a/src/categories/unread.js +++ b/src/categories/unread.js @@ -10,22 +10,14 @@ module.exports = function (Categories) { if (!Array.isArray(cids) || !cids.length) { return callback(); } - var keys = cids.map(function (cid) { - return 'cid:' + cid + ':read_by_uid'; - }); + var keys = cids.map(cid => 'cid:' + cid + ':read_by_uid'); async.waterfall([ function (next) { db.isMemberOfSets(keys, uid, next); }, function (hasRead, next) { - keys = keys.filter(function (key, index) { - return !hasRead[index]; - }); - - if (!keys.length) { - return callback(); - } + keys = keys.filter((key, index) => !hasRead[index]); db.setsAdd(keys, uid, next); }, @@ -41,9 +33,7 @@ module.exports = function (Categories) { }; Categories.hasReadCategories = function (cids, uid, callback) { - var sets = cids.map(function (cid) { - return 'cid:' + cid + ':read_by_uid'; - }); + var sets = cids.map(cid => 'cid:' + cid + ':read_by_uid'); db.isMemberOfSets(sets, uid, callback); }; diff --git a/src/categories/update.js b/src/categories/update.js index c0572f1ffb..f1b1ec60f7 100644 --- a/src/categories/update.js +++ b/src/categories/update.js @@ -97,7 +97,6 @@ module.exports = function (Categories) { function (oldParent, next) { async.series([ function (next) { - oldParent = parseInt(oldParent, 10) || 0; db.sortedSetRemove('cid:' + oldParent + ':children', cid, next); }, function (next) { @@ -125,9 +124,7 @@ module.exports = function (Categories) { db.delete('cid:' + cid + ':tag:whitelist', next); }, function (next) { - var scores = tags.map(function (tag, index) { - return index; - }); + var scores = tags.map((tag, index) => index); db.sortedSetAdd('cid:' + cid + ':tag:whitelist', scores, tags, next); }, ], callback); @@ -144,7 +141,6 @@ module.exports = function (Categories) { db.sortedSetAdd('categories:cid', order, cid, next); }, function (next) { - parentCid = parseInt(parentCid, 10) || 0; db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next); }, ], next); diff --git a/test/categories.js b/test/categories.js index 644c9258ee..70e24343b5 100644 --- a/test/categories.js +++ b/test/categories.js @@ -461,6 +461,25 @@ describe('Categories', function () { ], done); }); + it('should create category with settings from', function (done) { + var child1Cid; + var parentCid; + async.waterfall([ + function (next) { + Categories.create({ name: 'copy from', description: 'copy me' }, next); + }, + function (category, next) { + parentCid = category.cid; + Categories.create({ name: 'child1', description: 'will be gone', cloneFromCid: parentCid }, next); + }, + function (category, next) { + child1Cid = category.cid; + assert.equal(category.description, 'copy me'); + next(); + }, + ], done); + }); + it('should copy settings from', function (done) { var child1Cid; var parentCid; @@ -476,7 +495,7 @@ describe('Categories', function () { child1Cid = category.cid; socketCategories.copySettingsFrom({ uid: adminUid }, { fromCid: parentCid, toCid: child1Cid }, next); }, - function (canDelete, next) { + function (destinationCategory, next) { Categories.getCategoryField(child1Cid, 'description', next); }, function (description, next) { diff --git a/test/database/sets.js b/test/database/sets.js index daef0a6d4c..c667758a66 100644 --- a/test/database/sets.js +++ b/test/database/sets.js @@ -60,6 +60,13 @@ describe('Set methods', function () { done(); }); }); + + it('should not error if keys is empty array', function (done) { + db.setsAdd([], 'value', function (err) { + assert.ifError(err); + done(); + }); + }); }); describe('getSetsMembers()', function () {