-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(checks): add checks for UniMonth
- Loading branch information
1 parent
92d4c6b
commit 6f061bc
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}), | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); |