Skip to content

Commit

Permalink
adds type guards for time unit ids
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitropoulos committed Mar 1, 2020
1 parent 0c83005 commit a5e12fa
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/components/date_picker/super_date_picker/relative_options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TimeUnitId, RelativeOption } from '../types';
import { RelativeOption, TimeUnitId, TimeUnitFromNowId } from '../types';

export const relativeOptions: RelativeOption[] = [
{ text: 'Seconds ago', value: 's' },
Expand All @@ -18,12 +18,20 @@ export const relativeOptions: RelativeOption[] = [
{ text: 'Years from now', value: 'y+' },
];

export const relativeUnitsFromLargestToSmallest = relativeOptions
.map(({ value }) => {
return value;
})
.filter(value => {
// NOTE_TO_SELF(dimitri): a TypeScript assertion couldn't hurt here, instead of a cast
return !value.includes('+');
})
.reverse() as TimeUnitId[];
const timeUnitIds = relativeOptions
.map(({ value }) => value)
.filter(value => value.includes('+')) as TimeUnitId[];

export const isTimeUnitId = (value: string): value is TimeUnitId =>
timeUnitIds.includes(value as TimeUnitId);

const timeUnitFromNowIds = relativeOptions
.map(({ value }) => value)
.filter(value => !value.includes('+')) as TimeUnitFromNowId[];

export const isTimeUnitFromNowId = (
value: string
): value is TimeUnitFromNowId =>
timeUnitFromNowIds.includes(value as TimeUnitFromNowId);

export const relativeUnitsFromLargestToSmallest = timeUnitIds.reverse();

0 comments on commit a5e12fa

Please sign in to comment.