From 7e4298848976bccc8cee2cf0b8ca83c1ca0dd474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 8 May 2020 20:27:04 -0400 Subject: [PATCH] refactor: shorter function --- src/categories/index.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/categories/index.js b/src/categories/index.js index 661275e6c4..03db4f68c5 100644 --- a/src/categories/index.js +++ b/src/categories/index.js @@ -175,20 +175,16 @@ function calculateTopicPostCount(category) { return; } - var postCount = category.post_count; - var topicCount = category.topic_count; - if (!Array.isArray(category.children) || !category.children.length) { - category.totalPostCount = postCount; - category.totalTopicCount = topicCount; - return; + let postCount = category.post_count; + let topicCount = category.topic_count; + if (Array.isArray(category.children)) { + category.children.forEach(function (child) { + calculateTopicPostCount(child); + postCount += parseInt(child.totalPostCount, 10) || 0; + topicCount += parseInt(child.totalTopicCount, 10) || 0; + }); } - category.children.forEach(function (child) { - calculateTopicPostCount(child); - postCount += parseInt(child.totalPostCount, 10) || 0; - topicCount += parseInt(child.totalTopicCount, 10) || 0; - }); - category.totalPostCount = postCount; category.totalTopicCount = topicCount; }