import { FilterList , TimeBound } from '.' ; import { parseDateToApiAcceptedFormat , cut } from '../utils' ; import * as regExp from '../utils/reg-exp' ; import * as validators from '../utils/string-validators' ; export interface Query { displayString : string ; queryString : string ; routerString : string ; filter : FilterList ; location : string ; timeBound : TimeBound ; from : boolean ; } function arg query return export function parseQueryToQueryString ( query ) { let qs ; qs = query . displayString ; if ( query . location ) { qs += ` near:${ query . location }` ; } if ( query . timeBound . since ) { qs += ` since:${ parseDateToApiAcceptedFormat ( query . timeBound . since ) }` ; } if ( query . timeBound . until ) { qs += ` until:${ parseDateToApiAcceptedFormat ( query . timeBound . until ) }` ; } return qs ; } function argument query return export function parseQueryToRouterString ( query ) { let qs ; qs = parseQueryToQueryString ( query ) ; const activeFilterArray = new Array < string > ( ) ; Object . keys ( query . filter ) . sort ( ) . forEach ( filter => { if ( query . filter [ filter ] ) { activeFilterArray . push ( filter ) ; } } ) ; qs += ( activeFilterArray . length ) ? ` filter:${ activeFilterArray . join ( ',' ) }` : '' ; return qs ; } function arg str return export function parseStringToQuery ( str ) { const queryObject = { displayString : '' , queryString : '' , routerString : '' , filter : { video : false , image : false } , location : null , timeBound : { since : null , until : null } , from : false , } ; const fromMatch = regExp . fromRegExp . exec ( str ) ; const nearMatch = regExp . nearRegExp . exec ( str ) ; const sinceMatch = regExp . sinceRegExp . exec ( str ) ; const untilMatch = regExp . untilRegExp . exec ( str ) ; const filterMatch = regExp . filterRegExp . exec ( str ) ; str = ( fromMatch ) ? cut ( str , fromMatch [ 0 ] , ' ' ) : str ; str = ( nearMatch ) ? cut ( str , nearMatch [ 0 ] , ' ' ) : str ; str = ( sinceMatch ) ? cut ( str , sinceMatch [ 0 ] , ' ' ) : str ; str = ( untilMatch ) ? cut ( str , untilMatch [ 0 ] , ' ' ) : str ; str = ( filterMatch ) ? cut ( str , filterMatch [ 0 ] , ' ' ) : str ; queryObject . displayString = str ; if ( fromMatch && fromMatch [ 1 ] !== ':' ) { if ( validators . containNameCharactersOnly ( fromMatch [ 1 ] ) && fromMatch . index === 0 ) { queryObject . from = true ; queryObject . displayString = fromMatch [ 0 ] ; } else { } } if ( nearMatch && nearMatch [ 1 ] !== ':' ) { if ( validators . containAlphabetsOnly ( nearMatch [ 1 ] ) && fromMatch . index === 0 ) { queryObject . location = nearMatch [ 1 ] ; queryObject . displayString = nearMatch [ 0 ] ; } else { } } if ( sinceMatch && sinceMatch [ 1 ] !== ':' ) { if ( validators . isValidApiAcceptedDateString ( sinceMatch [ 1 ] ) ) { const validDateTimeString = sinceMatch [ 1 ] . split ( '_' ) . join ( ':' ) ; queryObject . timeBound . since = new Date ( validDateTimeString ) ; } else { } } if ( untilMatch && untilMatch [ 1 ] !== ':' ) { if ( validators . isValidApiAcceptedDateString ( untilMatch [ 1 ] ) ) { const validDateTimeString = untilMatch [ 1 ] . split ( '_' ) . join ( ':' ) ; queryObject . timeBound . until = new Date ( validDateTimeString ) ; } else { } } if ( filterMatch && filterMatch [ 1 ] !== ':' ) { const filtersArray = filterMatch [ 1 ] . split ( ',' ) ; filtersArray . forEach ( filter => { if ( filter === 'image' ) { queryObject . filter . image = true ; } else if ( filter === 'video' ) { queryObject . filter . video = true ; } else { } } ) ; } queryObject . queryString = parseQueryToQueryString ( queryObject ) ; queryObject . routerString = parseQueryToRouterString ( queryObject ) ; return queryObject ; }