Skip to content

Instantly share code, notes, and snippets.

@abetss
Created November 28, 2017 18:37
Show Gist options
  • Save abetss/e6ff5a741c5763cf385639905036f3f4 to your computer and use it in GitHub Desktop.
Save abetss/e6ff5a741c5763cf385639905036f3f4 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/zitiwuc
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
const posts = [
{ id: 1, likes: [] },
{ id: 2, likes: ['Peter'] },
{ id: 3, likes: ['John', 'Mark'] },
{ id: 4, likes: ['Paul', 'Lilly', 'Alex'] },
{ id: 5, likes: ['Sarah', 'Michelle', 'Alex', 'John'] }
];
const expectedOutput = [
{ id: 1, text: 'No one likes this' },
{ id: 2, text: 'Peter likes this' },
{ id: 3, text: 'John and Mark like this' },
{ id: 4, text: 'Paul, Lilly and Alex like this' },
{ id: 5, text: 'Sarah, Michelle and 2 others like this' }
];
const MAX_NUMBER_OF_LIKES_NAMES_TO_SHOW = 3;
const NUMBER_OF_SUMMERIZED_LIKES_NAMES_TO_SHOW = 2;
const actual = theoreticalFeedFacebookLikes(posts);
console.log('Matches expected output', JSON.stringify(expectedOutput) === JSON.stringify(actual));
function theoreticalFeedFacebookLikes(posts) {
return posts.map(toTheoreticalFeedFacebookLikes);
}
function toTheoreticalFeedFacebookLikes(post) {
return {
id: post.id,
text: toFacebookLikesSentence(post.likes)
};
}
function toFacebookLikesSentence(likes) {
const seperatedNames = facebookLikesSeperatedNames(likes);
const usePluralVerb = likes.length > 1;
return addLikesSuffix(seperatedNames, usePluralVerb);
}
function facebookLikesSeperatedNames(likes) {
if (likes.length === 0) {
return 'No one';
}
else if (likes.length <= MAX_NUMBER_OF_LIKES_NAMES_TO_SHOW) {
return likes.reduce(toCommaAndSeperated, '');
}
else {
const numberOfOthersLikes = likes.length - NUMBER_OF_SUMMERIZED_LIKES_NAMES_TO_SHOW;
const commaSeperatedNames = likes
.slice(0, NUMBER_OF_SUMMERIZED_LIKES_NAMES_TO_SHOW)
.reduce(toCommaSeperated, '');
return `${commaSeperatedNames} and ${numberOfOthersLikes} others`;
}
}
function addLikesSuffix(sentence, usePluralVerb) {
return usePluralVerb ?
`${sentence} like this` :
`${sentence} likes this`;
}
function toCommaSeperated(sentence, name, index) {
if(index === 0) {
return sentence += name;
}
return sentence += `, ${name}`
}
function toCommaAndSeperated(sentence, name, index, array) {
if(index === 0) {
return sentence += name;
}
return index !== array.length - 1 ?
sentence += `, ${name}` :
sentence += ` and ${name}`;
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">
const posts = [
{ id: 1, likes: [] },
{ id: 2, likes: ['Peter'] },
{ id: 3, likes: ['John', 'Mark'] },
{ id: 4, likes: ['Paul', 'Lilly', 'Alex'] },
{ id: 5, likes: ['Sarah', 'Michelle', 'Alex', 'John'] }
];
const expectedOutput = [
{ id: 1, text: 'No one likes this' },
{ id: 2, text: 'Peter likes this' },
{ id: 3, text: 'John and Mark like this' },
{ id: 4, text: 'Paul, Lilly and Alex like this' },
{ id: 5, text: 'Sarah, Michelle and 2 others like this' }
];
const MAX_NUMBER_OF_LIKES_NAMES_TO_SHOW = 3;
const NUMBER_OF_SUMMERIZED_LIKES_NAMES_TO_SHOW = 2;
const actual = theoreticalFeedFacebookLikes(posts);
console.log('Matches expected output', JSON.stringify(expectedOutput) === JSON.stringify(actual));
function theoreticalFeedFacebookLikes(posts) {
return posts.map(toTheoreticalFeedFacebookLikes);
}
function toTheoreticalFeedFacebookLikes(post) {
return {
id: post.id,
text: toFacebookLikesSentence(post.likes)
};
}
function toFacebookLikesSentence(likes) {
const seperatedNames = facebookLikesSeperatedNames(likes);
const usePluralVerb = likes.length > 1;
return addLikesSuffix(seperatedNames, usePluralVerb);
}
function facebookLikesSeperatedNames(likes) {
if (likes.length === 0) {
return 'No one';
}
else if (likes.length <= MAX_NUMBER_OF_LIKES_NAMES_TO_SHOW) {
return likes.reduce(toCommaAndSeperated, '');
}
else {
const numberOfOthersLikes = likes.length - NUMBER_OF_SUMMERIZED_LIKES_NAMES_TO_SHOW;
const commaSeperatedNames = likes
.slice(0, NUMBER_OF_SUMMERIZED_LIKES_NAMES_TO_SHOW)
.reduce(toCommaSeperated, '');
return `${commaSeperatedNames} and ${numberOfOthersLikes} others`;
}
}
function addLikesSuffix(sentence, usePluralVerb) {
return usePluralVerb ?
`${sentence} like this` :
`${sentence} likes this`;
}
function toCommaSeperated(sentence, name, index) {
if(index === 0) {
return sentence += name;
}
return sentence += `, ${name}`
}
function toCommaAndSeperated(sentence, name, index, array) {
if(index === 0) {
return sentence += name;
}
return index !== array.length - 1 ?
sentence += `, ${name}` :
sentence += ` and ${name}`;
}
</script></body>
</html>
const posts = [
{ id: 1, likes: [] },
{ id: 2, likes: ['Peter'] },
{ id: 3, likes: ['John', 'Mark'] },
{ id: 4, likes: ['Paul', 'Lilly', 'Alex'] },
{ id: 5, likes: ['Sarah', 'Michelle', 'Alex', 'John'] }
];
const expectedOutput = [
{ id: 1, text: 'No one likes this' },
{ id: 2, text: 'Peter likes this' },
{ id: 3, text: 'John and Mark like this' },
{ id: 4, text: 'Paul, Lilly and Alex like this' },
{ id: 5, text: 'Sarah, Michelle and 2 others like this' }
];
const MAX_NUMBER_OF_LIKES_NAMES_TO_SHOW = 3;
const NUMBER_OF_SUMMERIZED_LIKES_NAMES_TO_SHOW = 2;
const actual = theoreticalFeedFacebookLikes(posts);
console.log('Matches expected output', JSON.stringify(expectedOutput) === JSON.stringify(actual));
function theoreticalFeedFacebookLikes(posts) {
return posts.map(toTheoreticalFeedFacebookLikes);
}
function toTheoreticalFeedFacebookLikes(post) {
return {
id: post.id,
text: toFacebookLikesSentence(post.likes)
};
}
function toFacebookLikesSentence(likes) {
const seperatedNames = facebookLikesSeperatedNames(likes);
const usePluralVerb = likes.length > 1;
return addLikesSuffix(seperatedNames, usePluralVerb);
}
function facebookLikesSeperatedNames(likes) {
if (likes.length === 0) {
return 'No one';
}
else if (likes.length <= MAX_NUMBER_OF_LIKES_NAMES_TO_SHOW) {
return likes.reduce(toCommaAndSeperated, '');
}
else {
const numberOfOthersLikes = likes.length - NUMBER_OF_SUMMERIZED_LIKES_NAMES_TO_SHOW;
const commaSeperatedNames = likes
.slice(0, NUMBER_OF_SUMMERIZED_LIKES_NAMES_TO_SHOW)
.reduce(toCommaSeperated, '');
return `${commaSeperatedNames} and ${numberOfOthersLikes} others`;
}
}
function addLikesSuffix(sentence, usePluralVerb) {
return usePluralVerb ?
`${sentence} like this` :
`${sentence} likes this`;
}
function toCommaSeperated(sentence, name, index) {
if(index === 0) {
return sentence += name;
}
return sentence += `, ${name}`
}
function toCommaAndSeperated(sentence, name, index, array) {
if(index === 0) {
return sentence += name;
}
return index !== array.length - 1 ?
sentence += `, ${name}` :
sentence += ` and ${name}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment