import { reduceRight } from 'lodash' ; import { mapSpatialFilter } from './mappers/map_spatial_filter' ; import { mapMatchAll } from './mappers/map_match_all' ; import { mapPhrase } from './mappers/map_phrase' ; import { mapPhrases } from './mappers/map_phrases' ; import { mapRange } from './mappers/map_range' ; import { mapExists } from './mappers/map_exists' ; import { mapMissing } from './mappers/map_missing' ; import { mapQueryString } from './mappers/map_query_string' ; import { mapGeoBoundingBox } from './mappers/map_geo_bounding_box' ; import { mapGeoPolygon } from './mappers/map_geo_polygon' ; import { mapDefault } from './mappers/map_default' ; import { generateMappingChain } from './generate_mapping_chain' ; import { Filter } from '../../../../common' ; export function mapFilter ( filter ) { /** Mappers **/ const mappers = [ mapSpatialFilter , mapMatchAll , mapRange , mapPhrase , mapPhrases , mapExists , mapMissing , mapQueryString , mapGeoBoundingBox , mapGeoPolygon , mapDefault , ] ; const noop = ( ) => { throw new Error ( 'No mappings have been found for filter.' ) ; } ; const mapFn = reduceRight < Function , Function > ( mappers , ( memo , map ) => generateMappingChain ( map , memo ) , noop ) ; const mapped = mapFn ( filter ) ; filter . meta = filter . meta || { } ; filter . meta . type = mapped . type ; filter . meta . key = mapped . key ; filter . meta . value = mapped . value ; filter . meta . params = mapped . params ; filter . meta . disabled = Boolean ( filter . meta . disabled ) ; filter . meta . negate = Boolean ( filter . meta . negate ) ; filter . meta . alias = filter . meta . alias || null ; return filter ; }