Files
freeCodeCamp/api/src/utils/progress.ts
Naomi Carrigan 0aa1ad0d09 feat: require JSDoc in new api (#50429)
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2023-08-03 21:50:54 +05:30

37 lines
1016 B
TypeScript

export type ProgressTimestamp = number | { timestamp: number } | null;
/**
* 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
): Record<string, 1> => {
const calendar: Record<string, 1> = {};
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;
};