UNPKG

pockybot

Version:

Spark bot that handles team recognition

103 lines (102 loc) 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const stringWidth = require('string-width'); function mapResults(pegRecipients, categories = null) { return pegRecipients.map(user => { const person = user.validPegsReceived[0] ? user.validPegsReceived[0].receiver : null; return { id: user.id, person, pegs: user.validPegsReceived.map(peg => { return { sender: peg.sender, comment: peg.comment, categories: categories ? parseCategories(peg.comment, categories) : null }; }), weightedPegsReceived: user.weightedPegResult, validPegsReceived: user.numberOfValidPegsReceived }; }); } function mapPenalties(pegRecipients, penaltyKeywords) { return pegRecipients.map(user => { const person = user.validPegsReceived[0] ? user.validPegsReceived[0].receiver : null; return { id: user.id, person, pegs: user.penaltyPegsSent.map(peg => { return { sender: peg.sender, comment: peg.comment, categories: penaltyKeywords ? parseCategories(peg.comment, penaltyKeywords) : null }; }), weightedPegsReceived: user.weightedPegResult, validPegsReceived: user.numberOfValidPegsReceived }; }); } function parseCategories(comment, categories) { return categories.filter(category => comment.includes(category)); } function getReceiverColumnWidths(results) { let longestReceiver = this.stringLength('receiver'); let longestSender = this.stringLength('sender'); let longestComment = this.stringLength('comments'); results.forEach((winner) => { if (this.stringLength(winner.personName) > longestReceiver) { longestReceiver = this.stringLength(winner.personName); } winner.validPegsReceived.forEach((peg) => { if (this.stringLength(peg.senderName) > longestSender) { longestSender = this.stringLength(peg.senderName); } if (this.stringLength(peg.comment) > longestComment) { longestComment = this.stringLength(peg.comment); } }); }); return { receiver: longestReceiver, sender: longestSender, comment: longestComment }; } function getColumnWidths(arr, accessors, columnNames) { const stringWidth = require('string-width'); const longestValues = []; for (let i = 0; i < accessors.length; i++) { if (columnNames.length > i) { longestValues.push(stringWidth(columnNames[i])); } else { longestValues.push(0); } } arr.forEach((value) => { for (let i = 0; i < accessors.length; i++) { const width = stringWidth(accessors[i](value)); if (width > longestValues[i]) { longestValues[i] = width; } } }); return longestValues; } function padString(str, length) { let a = (length / 2) - (this.stringLength(str) / 2); return str.padStart(a + this.stringLength(str)).padEnd(length); } function stringLength(str) { // todo: get correct width of emojis return stringWidth(str); } exports.default = { mapResults, mapPenalties, getReceiverColumnWidths, getColumnWidths, padString, stringLength };