Skip to content

Commit

Permalink
restructure functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonmyers committed May 22, 2024
1 parent ace762c commit af9ee9a
Showing 1 changed file with 55 additions and 12 deletions.
67 changes: 55 additions & 12 deletions summaryStats.js
Original file line number Diff line number Diff line change
@@ -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 => ({
Expand All @@ -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 =>
Expand Down

0 comments on commit af9ee9a

Please sign in to comment.