feat(friends): pure category filter + count helpers

This commit is contained in:
simoleo89
2026-06-02 17:54:30 +02:00
parent 76b7e21494
commit f9320b4582
3 changed files with 86 additions and 0 deletions
@@ -0,0 +1,53 @@
import { describe, expect, it } from 'vitest';
import { MessengerFriend } from './MessengerFriend';
import { countFriendsByCategory, filterFriendsByCategory } from './friendCategory.helpers';
const makeFriend = (id: number, categoryId: number): MessengerFriend =>
{
const friend = new MessengerFriend();
friend.id = id;
friend.categoryId = categoryId;
return friend;
};
describe('filterFriendsByCategory', () =>
{
const friends = [ makeFriend(1, 0), makeFriend(2, 5), makeFriend(3, 5), makeFriend(4, 8) ];
it('returns all friends when categoryId is 0 (All)', () =>
{
expect(filterFriendsByCategory(friends, 0)).toHaveLength(4);
});
it('returns only the friends in the given category', () =>
{
expect(filterFriendsByCategory(friends, 5).map(f => f.id)).toEqual([ 2, 3 ]);
});
it('returns an empty array for a category with no members', () =>
{
expect(filterFriendsByCategory(friends, 99)).toEqual([]);
});
it('is null-safe', () =>
{
expect(filterFriendsByCategory(null, 5)).toEqual([]);
});
});
describe('countFriendsByCategory', () =>
{
const friends = [ makeFriend(1, 0), makeFriend(2, 5), makeFriend(3, 5) ];
it('counts members per category id', () =>
{
const counts = countFriendsByCategory(friends);
expect(counts.get(0)).toBe(1);
expect(counts.get(5)).toBe(2);
});
it('is null-safe', () =>
{
expect(countFriendsByCategory(null).size).toBe(0);
});
});
+32
View File
@@ -0,0 +1,32 @@
import { MessengerFriend } from './MessengerFriend';
/**
* Filter a friend list to a single category. categoryId 0 means
* "All" (no filtering) and returns the list unchanged.
*/
export const filterFriendsByCategory = (friends: MessengerFriend[], categoryId: number): MessengerFriend[] =>
{
if(!friends) return [];
if(!categoryId) return friends;
return friends.filter(friend => (friend.categoryId === categoryId));
};
/**
* Count how many friends belong to each category id. Used to render
* member counts on the group chips.
*/
export const countFriendsByCategory = (friends: MessengerFriend[]): Map<number, number> =>
{
const counts = new Map<number, number>();
if(!friends) return counts;
for(const friend of friends)
{
counts.set(friend.categoryId, (counts.get(friend.categoryId) ?? 0) + 1);
}
return counts;
};
+1
View File
@@ -1,3 +1,4 @@
export * from './friendCategory.helpers';
export * from './GetGroupChatData'; export * from './GetGroupChatData';
export * from './IGroupChatData'; export * from './IGroupChatData';
export * from './MessengerFriend'; export * from './MessengerFriend';