1717 * under the License.
1818 */
1919
20- import dateMath from '@kbn/datemath' ;
20+ import dateMath , { Unit } from '@kbn/datemath' ;
2121
2222import { InvalidEsCalendarIntervalError } from './invalid_es_calendar_interval_error' ;
2323import { InvalidEsIntervalFormatError } from './invalid_es_interval_format_error' ;
@@ -26,6 +26,8 @@ const ES_INTERVAL_STRING_REGEX = new RegExp(
2626 '^([1-9][0-9]*)\\s*(' + dateMath . units . join ( '|' ) + ')$'
2727) ;
2828
29+ export type ParsedInterval = ReturnType < typeof parseEsInterval > ;
30+
2931/**
3032 * Extracts interval properties from an ES interval string. Disallows unrecognized interval formats
3133 * and fractional values. Converts some intervals from "calendar" to "fixed" when the number of
@@ -37,15 +39,15 @@ const ES_INTERVAL_STRING_REGEX = new RegExp(
3739 * | -------- | ---------------- | ------------------- |
3840 * | ms | fixed | fixed |
3941 * | s | fixed | fixed |
40- * | m | fixed | fixed |
42+ * | m | calendar | fixed |
4143 * | h | calendar | fixed |
4244 * | d | calendar | fixed |
4345 * | w | calendar | N/A - disallowed |
4446 * | M | calendar | N/A - disallowed |
4547 * | y | calendar | N/A - disallowed |
4648 *
4749 */
48- export function parseEsInterval ( interval : string ) : { value : number ; unit : string ; type : string } {
50+ export function parseEsInterval ( interval : string ) {
4951 const matches = String ( interval )
5052 . trim ( )
5153 . match ( ES_INTERVAL_STRING_REGEX ) ;
@@ -54,9 +56,9 @@ export function parseEsInterval(interval: string): { value: number; unit: string
5456 throw new InvalidEsIntervalFormatError ( interval ) ;
5557 }
5658
57- const value = matches && parseFloat ( matches [ 1 ] ) ;
58- const unit = matches && matches [ 2 ] ;
59- const type = unit && dateMath . unitsMap [ unit ] . type ;
59+ const value = parseFloat ( matches [ 1 ] ) ;
60+ const unit = matches [ 2 ] as Unit ;
61+ const type = dateMath . unitsMap [ unit ] . type ;
6062
6163 if ( type === 'calendar' && value !== 1 ) {
6264 throw new InvalidEsCalendarIntervalError ( interval , value , unit , type ) ;
@@ -65,6 +67,9 @@ export function parseEsInterval(interval: string): { value: number; unit: string
6567 return {
6668 value,
6769 unit,
68- type : ( type === 'mixed' && value === 1 ) || type === 'calendar' ? 'calendar' : 'fixed' ,
70+ type :
71+ ( type === 'mixed' && value === 1 ) || type === 'calendar'
72+ ? ( 'calendar' as 'calendar' )
73+ : ( 'fixed' as 'fixed' ) ,
6974 } ;
7075}
0 commit comments