Files
ncd-fe/packages/pl-fe/src/utils/badges.ts
nicole mikołajczyk a0645902c0 pl-fe: remove account normalizer and so
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
2025-10-27 12:19:04 +01:00

39 lines
1.1 KiB
TypeScript

import type { Account } from 'pl-api';
/** Convert a plain tag into a badge. */
const tagToBadge = (tag: string) => `badge:${tag}`;
/** Convert a badge into a plain tag. */
const badgeToTag = (badge: string) => badge.replace(/^badge:/, '');
/** Difference between an old and new set of tags. */
interface TagDiff {
/** New tags that were added. */
added: string[];
/** Old tags that were removed. */
removed: string[];
}
/** Returns the differences between two sets of tags. */
const getTagDiff = (oldTags: string[], newTags: string[]): TagDiff => ({
added: newTags.filter(tag => !oldTags.includes(tag)),
removed: oldTags.filter(tag => !newTags.includes(tag)),
});
/** Returns only tags which are badges. */
const filterBadges = (tags: string[]): string[] => tags.filter(tag => tag.startsWith('badge:'));
/** Get badges from an account. */
const getBadges = (account: Pick<Account, '__meta'>) => {
const tags = account.__meta.pleroma?.tags ?? [];
return filterBadges(tags);
};
export {
tagToBadge,
badgeToTag,
filterBadges,
getTagDiff,
getBadges,
};