Needed a way to sort Slack channels by priority based on unread messages, mentions, and recency. Here's a JavaScript implementation. // Channel Priority Sorter for Slack // Sorts channels based on a weighted priority score const PRIORITY_WEIGHTS = Object.freeze({ MENTIONS: 10, UNREAD: 2, RECENCY: 5, STARRED: 15, }); /** * Calculates a priority score for a given channel * @param {Object} channel - The channel object * @returns {number} The computed priority score / function computePriorityScore(channel) { if (!channel || typeof channel !== 'object') { throw new TypeError('Channel must be a valid object'); } const mentions = channel.mentions ?? 0; const unread = channel.unreadCount ?? 0; const isStarred = channel.isStarred ?? false; const lastActivity = channel.lastActivity ?? 0; // Compute recency factor (more recent = higher score) const now = Date.now(); const hoursSince = (now - lastActivity) / (1000 * 60 * 60); const recencyFactor = Math.max(0, 24 - hoursSince) / 24; let score = 0; score += mentions * PRIORITY_WEIGHTS.MENTIONS; score += unread * PRIORITY_WEIGHTS.UNREAD; score += recencyFactor * PRIORITY_WEIGHTS.RECENCY; if (isStarred) score += PRIORITY_WEIGHTS.STARRED; return score; } /* * Sorts an array of Slack channels by priority (descending) * @param {Array} channels - List of channel objects * @returns {Array} Sorted array of channels */ function sortChannelsByPriority(channels) { if (!Array.isArray(channels)) { throw new TypeError('Expected an array of channels'); } // Create a copy to avoid mutating the original const channelsCopy = [...channels]; return channelsCopy .map((ch) => ({ channel: ch, score: computePriorityScore(ch) })) .sort((a, b) => b.score - a.score) .map((entry) => entry.channel); } // Example usage const channels = [ { name: 'general', mentions: 0, unreadCount: 5, isStarred: false, lastActivity: Date.now() - 3600000 }, { name: 'dev-team', mentions: 3, unreadCount: 12, isStarred: true, lastActivity: Date.now() - 600000 }, { name: 'random', mentions: 0, unreadCount: 2, isStarred: false, lastActivity: Date.now() - 86400000 }, ]; const sorted = sortChannelsByPriority(channels); console.log(sorted.map((c) => c.name)); Code Review 1. Lines 4-9. Object.freeze on a constants object that never leaves this file. Who exactly are we defending against here, our future selves at 2am? 2. Lines 17-19. Throwing a TypeError because someone might pass null to an internal function that's only called by us, two lines below. Defensive programming has gone too far. 3. Lines 28-30. The recencyFactor math is fine but 'hoursSince' going negative if lastActivity is in the future just silently maxes to 0. No comment about it, no warning, just vibes. 4. Lines 46-48. Same TypeError dance again. If sortChannelsByPriority gets called with a non-array we have bigger problems than this function's error message. 5. Line 51. Spreading into channelsCopy and then calling .map which already returns a new array. The copy is doing nothing. Classic. 6. Lines 53-56. Schwartzian transform for sorting three channels. Technically correct, spiritually excessive. 7. Line 15. JSDoc on every function in a file with no exports and no consumers. We're writing documentation for an audience of one (me, angry).