export enum FilterStateStore { APP_STATE = 'appState' , GLOBAL_STATE = 'globalState' } export type FilterState = { store : FilterStateStore ; } ; type FilterFormatterFunction = ( value : any ) => string ; export interface FilterValueFormatter { convert : FilterFormatterFunction ; getConverterFor : ( type : string ) => FilterFormatterFunction ; } export type FilterMeta = { alias : string | null ; disabled : boolean ; negate : boolean ; controlledBy ? : string ; index ? : string ; type ? : string ; key ? : string ; params ? : any ; value ? : string ; } ; export type Filter = { $state ? : FilterState ; meta : FilterMeta ; query ? : any ; } ; export interface LatLon { lat : number ; lon : number ; } export const buildEmptyFilter = ( isPinned , index ? ) : Filter => { const meta = { disabled : false , negate : false , alias : null , index , } ; const $state = { store : isPinned ? FilterStateStore . GLOBAL_STATE : FilterStateStore . APP_STATE , } ; return { meta , $state } ; } ; export const isFilterPinned = ( filter ) => { return filter . $state && filter . $state . store === FilterStateStore . GLOBAL_STATE ; } ; export const toggleFilterDisabled = ( filter ) => { const disabled = ! filter . meta . disabled ; const meta = { ... filter . meta , disabled } ; return { ... filter , meta } ; } ; export const toggleFilterNegated = ( filter ) => { const negate = ! filter . meta . negate ; const meta = { ... filter . meta , negate } ; return { ... filter , meta } ; } ; export const toggleFilterPinned = ( filter ) => { const store = isFilterPinned ( filter ) ? FilterStateStore . APP_STATE : FilterStateStore . GLOBAL_STATE ; const $state = { ... filter . $state , store } ; return { ... filter , $state } ; } ; export const enableFilter = ( filter ) => ! filter . meta . disabled ? filter : toggleFilterDisabled ( filter ) ; export const disableFilter = ( filter ) => filter . meta . disabled ? filter : toggleFilterDisabled ( filter ) ; export const pinFilter = ( filter ) => isFilterPinned ( filter ) ? filter : toggleFilterPinned ( filter ) ; export const unpinFilter = ( filter ) => ! isFilterPinned ( filter ) ? filter : toggleFilterPinned ( filter ) ; export const isFilter = ( x ) : x is Filter => ! ! x && typeof x === 'object' && ! ! ( x as Filter ) . meta && typeof ( x as Filter ) . meta === 'object' && typeof ( x as Filter ) . meta . disabled === 'boolean' ; export const isFilters = ( x ) : x is Filter [ ] => Array . isArray ( x ) && ! x . find ( ( y ) => ! isFilter ( y ) ) ;