Make oneEmojiPerAccount() work

merge-requests/18/head
Alex Gleason 4 years ago
parent 0ed867f7bd
commit b4f5321c0f
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

@ -123,12 +123,12 @@ describe('oneEmojiPerAccount', () => {
// Sorted
{ 'count': 2, 'me': true, 'name': '👍', accounts: [{ id: '1' }, { id: '2' }] },
{ 'count': 2, 'me': true, 'name': '❤', accounts: [{ id: '1' }, { id: '2' }] },
{ 'count': 1, 'me': true, 'name': '😯', accounts: [{ id: '1' }] },
{ 'count': 1, 'me': false, 'name': '😂', accounts: [{ id: '3' }] },
{ 'count': 1, 'me': true, 'name': '😯', accounts: [{ id: '1' }] },
{ 'count': 1, 'me': false, 'name': '😂', accounts: [{ id: '3' }] },
]);
expect(oneEmojiPerAccount(emojiReacts)).toEqual(fromJS([
expect(oneEmojiPerAccount(emojiReacts, '1')).toEqual(fromJS([
{ 'count': 2, 'me': true, 'name': '👍', accounts: [{ id: '1' }, { id: '2' }] },
{ 'count': 1, 'me': false, 'name': '😂', accounts: [{ id: '3' }] },
{ 'count': 1, 'me': false, 'name': '😂', accounts: [{ id: '3' }] },
]));
});
});

@ -33,14 +33,33 @@ export const mergeEmojiFavourites = (emojiReacts, favouritesCount) => {
}
};
export const oneEmojiPerAccount = emojiReacts => {
const hasMultiReactions = (emojiReacts, account) => (
emojiReacts.filter(
e => e.get('accounts').filter(
a => a.get('id') === account.get('id')
).count() > 0
).count() > 1
);
const inAccounts = (accounts, id) => (
accounts.filter(a => a.get('id') === id).count() > 0
);
export const oneEmojiPerAccount = (emojiReacts, me) => {
emojiReacts = emojiReacts.reverse();
return emojiReacts.reduce((acc, cur) => {
if (acc.filter(
e => e.get('me') && e.get('name') !== cur.get('name')
).length > 0) return acc.delete(cur); // FIXME: Incomplete
return acc;
}, emojiReacts).reverse();
return emojiReacts.reduce((acc, cur, idx) => {
const accounts = cur.get('accounts', ImmutableList())
.filter(a => !hasMultiReactions(acc, a));
return acc.set(idx, cur.merge({
accounts: accounts,
count: accounts.count(),
me: inAccounts(accounts, me),
}));
}, emojiReacts)
.filter(e => e.get('count') > 0)
.reverse();
};
export const filterEmoji = emojiReacts => (

Loading…
Cancel
Save