From af9ee9a9e9d8f380d839dd9215a44d24ae3914d4 Mon Sep 17 00:00:00 2001 From: alisonmyers Date: Wed, 22 May 2024 13:08:21 -0700 Subject: [PATCH] restructure functions --- summaryStats.js | 67 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/summaryStats.js b/summaryStats.js index 81751f9..507e23f 100644 --- a/summaryStats.js +++ b/summaryStats.js @@ -1,5 +1,5 @@ const {getDiscussions, courseIds} = require('./index') -const { flatten } = require('./util') +const { flatten, escapeComment, stripHTML } = require('./util') const flattenReplies = discussions => { const flattenedDiscussions = discussions.map(discussion => ({ @@ -10,31 +10,74 @@ const flattenReplies = discussions => { return flattenedDiscussions } -const getTotalReplyLikes = discussion => { - const totalLikes = discussion.replies.reduce((acc, reply) => acc + reply.postLikes, 0); +const getTotalReplyLikes = replies => { + const totalLikes = replies.reduce((acc, reply) => acc + reply.postLikes, 0); return totalLikes; }; -const getTotalReplies = discussion => { - const totalReplies = discussion.replies.length || 0 +const getTotalReplies = replies => { + const totalReplies = replies.length || 0 return totalReplies } -const getWordCount = reply => { - const wordCount = 0 - return wordCount +const getWordCount = message => { + const formattedMessage = stripHTML(escapeComment(message)) + const words = formattedMessage.trim().split(/\s+/) || 0 + return words.length +} + +const getWordCounts = replies => { + const wordCounts = replies.map(reply => getWordCount(reply.postMessage)); + return wordCounts; +} + +const getAverageWordCount = replies => { + const wordCounts = getWordCounts(replies); + const totalWords = wordCounts.reduce((total, count) => total + count, 0); + const totalReplies = replies.length || 1; // To avoid division by zero + const averageWordCount = totalWords / totalReplies || 0; + return averageWordCount; +} + +const getMinWordCount = replies => { + const wordCounts = getWordCounts(replies); + const minWordCount = Math.min(...wordCounts); + return minWordCount; +} + +const getMaxWordCount = replies => { + const wordCounts = getWordCounts(replies); + const maxWordCount = Math.max(...wordCounts); + return maxWordCount; +} + +const getMedianWordCount = replies => { + const wordCounts = getWordCounts(replies); + wordCounts.sort((a, b) => a - b); + const mid = Math.floor(wordCounts.length / 2); + const medianWordCount = wordCounts.length % 2 === 0 ? (wordCounts[mid - 1] + wordCounts[mid]) / 2 : wordCounts[mid]; + return medianWordCount; } const addStatsToDiscussion = discussion => { // Create a new object with the original discussion properties and add stats + // pass ...discussion to keep full object + + const { replies, ...discussionWithoutReplies } = discussion; return { - ...discussion, + ...discussionWithoutReplies, stats: { - totalReplyLikes: getTotalReplyLikes(discussion), - totalReplies: getTotalReplies(discussion) + totalReplyLikes: getTotalReplyLikes(replies), + totalReplies: getTotalReplies(replies), + wordCount: { + average: getAverageWordCount(replies), + max: getMaxWordCount(replies), + min: getMinWordCount(replies), + median: getMedianWordCount(replies) + } } }; - }; +}; Promise.all( courseIds.map(courseId =>