import type {AmbPlayObject, DateLike, PlayObject, PlayObjectMinimal, PlayPlatformId} from "./Atomic.ts"; import dayjs from "dayjs"; export const sortByNewestDate = (aPlayDate: DateLike, bPlayDate: DateLike) => { if (aPlayDate === undefined && bPlayDate === undefined) { return 0; } if (aPlayDate === undefined) { return 1; } if (bPlayDate === undefined) { return -1; } const realA = typeof aPlayDate === 'string' ? dayjs(aPlayDate) : aPlayDate; const realB = typeof bPlayDate === 'string' ? dayjs(bPlayDate) : bPlayDate; return realA.isBefore(realB) ? 1 : -1; }; /** sorts playObj formatted objects by playDate in descending (newest first) order */ export const sortByNewestPlayDateAccessor = (accessor: (play: AmbPlayObject) => DateLike) => (a: AmbPlayObject, b: AmbPlayObject) => { const aPlayDate = accessor(a); const bPlayDate = accessor(b); return sortByNewestDate(aPlayDate, bPlayDate); }; /** sorts playObj formatted objects by playDate in descending (newest first) order */ export const sortByNewestPlayDate = sortByNewestPlayDateAccessor((play) => play.data?.playDate); /** sorts playObj formatted objects by playDate in descending (newest first) order */ export const sortByNewestSeenDate = sortByNewestPlayDateAccessor((play) => play.meta?.seenAt); export const genGroupIdStr = (id: PlayPlatformId) => { return `${id[0]}-${id[1]}`; }; export const statefulInvariantTransform = (play: PlayObject, withIds?: boolean): PlayObjectMinimal => { const { meta, data, id, uid } = play; if(withIds) { return { data, meta, uid, id } } return { data, meta }; };