import { find } from 'lodash' ; import moment , { unitOfTime } from 'moment' ; import dateMath from '@elastic/datemath' ; const INTERVAL_STRING_RE = new RegExp ( '^([0-9\\.]*)\\s*(' + dateMath . units . join ( '|' ) + ')$' ) ; export function parseInterval ( interval ) { const matches = String ( interval ) . trim ( ) . match ( INTERVAL_STRING_RE ) ; if ( ! matches ) return null ; try { const value = parseFloat ( matches [ 1 ] ) || 1 ; const unit = matches [ 2 ] as unitOfTime . Base ; const duration = moment . duration ( value , unit ) ; const selectedUnit = find ( dateMath . units , ( u ) => Math . abs ( duration . as ( u ) ) >= 1 ) as unitOfTime . Base ; if ( dateMath . units . indexOf ( selectedUnit as any ) < dateMath . units . indexOf ( unit as any ) ) { return duration ; } return moment . duration ( duration . as ( selectedUnit ) , selectedUnit ) ; } catch ( e ) { return null ; } }