Separate parse and format functions to their own files
This commit is contained in:
parent
409c6c2739
commit
4835f4878e
3 changed files with 33 additions and 27 deletions
17
src/format.ts
Normal file
17
src/format.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import dayjs, { Dayjs } from 'dayjs';
|
||||||
|
import { Duration } from 'dayjs/plugin/duration.js';
|
||||||
|
|
||||||
|
export const formatTimestamp = (timestamp: Dayjs): string => timestamp.format('YYYY-MM-DD HH:mm');
|
||||||
|
|
||||||
|
export const formatDuration = (unLogged: Duration): string => unLogged.format('HH[ hours and ]mm [minutes]');
|
||||||
|
|
||||||
|
export const getHoursRoundedStr = (duration: Duration) =>
|
||||||
|
`(${getHoursRounded(duration)} as hours rounded to next even 15 minutes)`;
|
||||||
|
|
||||||
|
const getHoursRounded = (duration: Duration) => {
|
||||||
|
// Round up to the next multiple of 15
|
||||||
|
const minutes = Math.ceil(duration.as('minutes') / 15) * 15;
|
||||||
|
|
||||||
|
// Return as hours
|
||||||
|
return dayjs.duration(minutes, 'minutes').asHours();
|
||||||
|
};
|
30
src/main.ts
30
src/main.ts
|
@ -1,39 +1,15 @@
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import dayjs, { Dayjs } from 'dayjs';
|
import dayjs, { Dayjs } from 'dayjs';
|
||||||
import customParseFormat from 'dayjs/plugin/customParseFormat.js';
|
|
||||||
import duration, { Duration } from 'dayjs/plugin/duration.js';
|
|
||||||
import * as readline from 'readline/promises';
|
import * as readline from 'readline/promises';
|
||||||
|
import { formatDuration, formatTimestamp, getHoursRoundedStr } from './format';
|
||||||
dayjs.extend(customParseFormat);
|
import { Duration } from 'dayjs/plugin/duration';
|
||||||
dayjs.extend(duration);
|
import { parseDuration, parseTimestamp } from './parse';
|
||||||
|
|
||||||
const { log, error } = console;
|
const { log, error } = console;
|
||||||
const defaultStartTime = '08:00';
|
const defaultStartTime = '08:00';
|
||||||
const lunchBreakDuration = dayjs.duration(30, 'minutes');
|
const lunchBreakDuration = dayjs.duration(30, 'minutes');
|
||||||
const defaultWorkDayDuration = dayjs.duration({ hours: 7, minutes: 30 });
|
const defaultWorkDayDuration = dayjs.duration({ hours: 7, minutes: 30 });
|
||||||
|
|
||||||
const formatTimestamp = (timestamp: dayjs.Dayjs): string => timestamp.format('YYYY-MM-DD HH:mm');
|
|
||||||
|
|
||||||
const formatDuration = (unLogged: duration.Duration): string => unLogged.format('HH[ hours and ]mm [minutes]');
|
|
||||||
|
|
||||||
const parseTimestamp = (time: string): Dayjs => dayjs(time, 'H:mm', true);
|
|
||||||
|
|
||||||
const parseDuration = (time: string): Duration => {
|
|
||||||
const [hours, minutes] = time.split(':').map(Number);
|
|
||||||
return dayjs.duration({ hours, minutes });
|
|
||||||
};
|
|
||||||
|
|
||||||
const getHoursRounded = (duration: Duration) => {
|
|
||||||
// Round up to the next multiple of 15
|
|
||||||
const minutes = Math.ceil(duration.as('minutes') / 15) * 15;
|
|
||||||
|
|
||||||
// Return as hours
|
|
||||||
return dayjs.duration(minutes, 'minutes').asHours();
|
|
||||||
};
|
|
||||||
|
|
||||||
const getHoursRoundedStr = (duration: Duration) =>
|
|
||||||
`(${getHoursRounded(duration)} as hours rounded to next even 15 minutes)`;
|
|
||||||
|
|
||||||
const main = async () => {
|
const main = async () => {
|
||||||
const rl = readline.createInterface({
|
const rl = readline.createInterface({
|
||||||
input: process.stdin,
|
input: process.stdin,
|
||||||
|
|
13
src/parse.ts
Normal file
13
src/parse.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import dayjs, { Dayjs } from 'dayjs';
|
||||||
|
import customParseFormat from 'dayjs/plugin/customParseFormat.js';
|
||||||
|
import duration, { Duration } from 'dayjs/plugin/duration.js';
|
||||||
|
|
||||||
|
dayjs.extend(customParseFormat);
|
||||||
|
dayjs.extend(duration);
|
||||||
|
|
||||||
|
export const parseTimestamp = (time: string): Dayjs => dayjs(time, 'H:mm', true);
|
||||||
|
|
||||||
|
export const parseDuration = (time: string): Duration => {
|
||||||
|
const [hours, minutes] = time.split(':').map(Number);
|
||||||
|
return dayjs.duration({ hours, minutes });
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue