type DateMagnitude = 'day' | 'hour' | 'minute' | 'second';
type ReadableTime = {
- count: number,
- label: string
+ count: number;
+ label: string;
};
-
const internals = {
- magnitudes: {
- day: 86400000,
- hour: 3600000,
- minute: 60000,
- second: 1000
- },
- labels: {
- day: 'time.days',
- hour: 'time.hours',
- minute: 'time.minutes',
- second: 'time.seconds'
- },
-
- makeTimeReadable(time: number, magnitude: DateMagnitude): ReadableTime {
+ magnitudes: {
+ day: 86400000,
+ hour: 3600000,
+ minute: 60000,
+ second: 1000
+ },
+ labels: {
+ day: 'time.days',
+ hour: 'time.hours',
+ minute: 'time.minutes',
+ second: 'time.seconds'
+ },
- return {
- count: Math.floor(time / internals.magnitudes[magnitude]),
- label: internals.labels[magnitude]
- };
- }
+ makeTimeReadable(time: number, magnitude: DateMagnitude): ReadableTime {
+ return {
+ count: Math.floor(time / internals.magnitudes[magnitude]),
+ label: internals.labels[magnitude]
+ };
+ }
};
export const readableTime = function readableTime(time: number): ReadableTime {
-
- switch (true) {
- case time >= internals.magnitudes.day:
- return internals.makeTimeReadable(time, 'day');
- case time >= internals.magnitudes.hour:
- return internals.makeTimeReadable(time, 'hour');
- case time >= internals.magnitudes.minute:
- return internals.makeTimeReadable(time, 'minute');
- case time < 0:
- return internals.makeTimeReadable(0, 'second');
- default:
- return internals.makeTimeReadable(time, 'second');
- }
+ switch (true) {
+ case time >= internals.magnitudes.day:
+ return internals.makeTimeReadable(time, 'day');
+ case time >= internals.magnitudes.hour:
+ return internals.makeTimeReadable(time, 'hour');
+ case time >= internals.magnitudes.minute:
+ return internals.makeTimeReadable(time, 'minute');
+ case time < 0:
+ return internals.makeTimeReadable(0, 'second');
+ default:
+ return internals.makeTimeReadable(time, 'second');
+ }
};