Skip to content

Commit

Permalink
fix(checks): add checks for UniMonth
Browse files Browse the repository at this point in the history
  • Loading branch information
uladkasach committed Nov 21, 2024
1 parent 92d4c6b commit 6f061bc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/logic/checks/isUniMonth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { isUniMonth } from './isUniMonth';

describe('isUniMonth', () => {
const cases: { input: string; output: boolean }[] = [
{
input: 'May 15, 2024 at 5pm',
output: false,
},
{
input: '2024-05-15 17:21:55',
output: false,
},
{
input: '2024-05-15T17:21:55Z',
output: false,
},
{
input: '2024-05-15T17:21:55.555Z',
output: false,
},
{
input: 'May 15, 2024',
output: false,
},
{
input: '2024-05-15',
output: false,
},
{
input: 'May 2024',
output: false,
},
{
input: '2024-05',
output: true,
},
];

cases.forEach((thisCase) =>
it(`should return ${thisCase.output} for '${thisCase.input}'`, () => {
expect(isUniMonth(thisCase.input)).toEqual(thisCase.output);
}),
);
});
23 changes: 23 additions & 0 deletions src/logic/checks/isUniMonth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { format, parseISO } from 'date-fns';
import { withAssure } from 'type-fns';

import { UniDate } from '../../domain/UniDateTime';
import { castInputToDate } from '../casts/castInputToDate';

/**
* casts a date like input into a UniDate
*/
export const asUniMonth = (
input: Parameters<typeof castInputToDate>[0],
): UniDate => format(castInputToDate(input), 'yyyy-MM') as UniDate;

/**
* checks whether a string literal input is a UniDate
*/
export const isUniMonth = withAssure((input: string): input is UniDate => {
try {
return asUniMonth(parseISO(input)) === input;
} catch {
return false;
}
});

0 comments on commit 6f061bc

Please sign in to comment.