Created
October 13, 2018 04:14
-
-
Save taverasmisael/686ca11ffd467b2f4248be22b1fbb0ad to your computer and use it in GitHub Desktop.
A function to display facebook like reactions '(You, Marie and 3 more reacted to this)'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Some helper functions. Self-explanatories | |
const equal = (a, b) => a === b | |
const length = a => a.length | |
const not = a => !a | |
const lengthEqual = (arr, val) => equal(length(arr), val) | |
const lastIndex = arr => length(arr) - 1 | |
/** | |
* This function returns a string based on a names array composing the 'People reacted to this' from facebook. | |
* It takes in cosideration if: You reacted, no one reacted, only one people reacted, you were the only one reacting | |
* two people reacted and last but certently not least, if X number of people reacted. On this last case two variables | |
* will be taken into consideration: `maxNamesToShow` and if `youLiked` in order to display the amount of 'other people' | |
* had reacted | |
* @param names {string[]} A list of names to display. If the name on variable YOU is in the list, it shows 'you' first | |
* @param maxNamesToShow {number} the amount of names (plus one) you want to be max displayed before it appends 'and [x] more' | |
* @example console.log(displayReactions([])) // 'Be the first to react to this' | |
* @example console.log(displayReactions(['Peter'])) //'Peter reacted to this' | |
* @example console.log(displayReactions(['Peter', 'Mary'])) // 'Peter and Mary like this' | |
* @example console.log(displayReactions(['Peter', 'Mary', 'Julie'])) // Peter, Mary and Julie like this | |
* @example console.log(displayReactions(['Peter', 'Mary', 'Julie', 'Marcus', 'Misael', 'Caesar'], 5)) // You, Peter, Mary, Julie and 2 more reacted to this | |
*/ | |
const displayReactions = (names = [], maxNamesToShow = 3) => { | |
const BaseString = 'reacted to this' | |
const YOU = 'Misael' | |
if(not(length(names))) return `Be the first to react to this` | |
if(lengthEqual(names, 1)) return `${equal(names[0], YOU)? 'you' : names[0]} ${BaseString}` | |
let namesToDisplay | |
let otherNamesTotal | |
let namesList = names; | |
let maxToShow = maxNamesToShow | |
let youReactedMessage = '' | |
if (names.includes(YOU)) { // you liked | |
namesList = names.filter(name => name !== YOU) | |
maxToShow = maxNamesToShow - 2 | |
youReactedMessage = ' You' | |
} | |
namesToDisplay = namesList.slice(0, maxToShow) | |
otherNamesTotal = length(namesList.slice(maxToShow)) | |
youReactedMessage += length(namesToDisplay) ? ', ' : '' | |
const finalString = namesToDisplay.map((name, idx, arr) => { | |
if (equal(idx, lastIndex(arr))) return name | |
if (equal(idx, length(arr) -2) && not(otherNamesTotal)) return `${name} and` | |
return `${name},` | |
}).join(' ') | |
return otherNamesTotal ? `${youReactedMessage}${finalString} and ${otherNamesTotal} more ${BaseString}` : `${finalString} ${BaseString}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment