diff --git a/src/components/date_picker/super_date_picker/relative_options.ts b/src/components/date_picker/super_date_picker/relative_options.ts index db5c2e83d0e2..f2a83af34b9f 100644 --- a/src/components/date_picker/super_date_picker/relative_options.ts +++ b/src/components/date_picker/super_date_picker/relative_options.ts @@ -1,4 +1,4 @@ -import { TimeUnitId, RelativeOption } from '../types'; +import { RelativeOption, TimeUnitId, TimeUnitFromNowId } from '../types'; export const relativeOptions: RelativeOption[] = [ { text: 'Seconds ago', value: 's' }, @@ -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();