Files
freeCodeCamp/api/src/utils/progress.ts
Oliver Eyton-Williams a8f7e15dc2 feat(api): api/users/get-public-profile (#54729)
Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
2024-06-10 12:46:55 -05:00

37 lines
1.0 KiB
TypeScript

export type ProgressTimestamp = number | { timestamp: number } | null;
export type Calendar = Record<number, 1>;
/**
* Converts a ProgressTimestamp array to a object with keys based on the timestamps.
*
* @param progressTimestamps The ProgressTimestamp array.
* @returns The object with keys based on the timestamps.
*/
export const getCalendar = (
progressTimestamps: ProgressTimestamp[] | null
): Calendar => {
const calendar: Calendar = {};
progressTimestamps?.forEach(progress => {
if (progress === null) return;
if (typeof progress === 'number') {
calendar[Math.floor(progress / 1000)] = 1;
} else {
calendar[Math.floor(progress.timestamp / 1000)] = 1;
}
});
return calendar;
};
/**
* Converts a ProgressTimestamp array to an integer number of points.
*
* @param progressTimestamps The ProgressTimestamp array.
* @returns The number of points.
*/
export const getPoints = (
progressTimestamps: ProgressTimestamp[] | null
): number => {
return progressTimestamps?.length ?? 1;
};