import { cloneDeep , omit } from 'lodash' ; import { SchemaType } from '../../../shared/schema/types' ; import { RawBoost , Boost , SearchSettings , BoostType , ValueBoost } from './types' ; export const filterIfTerm = ( array , filterTerm ) : string [ ] => { return filterTerm === '' ? array : array . filter ( ( item ) => item . includes ( filterTerm ) ) ; } ; export const removeBoostStateProps = ( searchSettings ) : SearchSettings => { const updatedSettings = cloneDeep ( searchSettings ) ; const { boosts } = updatedSettings ; const keys = Object . keys ( boosts ) ; keys . forEach ( ( key ) => boosts [ key ] . forEach ( ( boost ) => delete boost . newBoost ) ) ; return updatedSettings ; } ; export const parseBoostCenter = ( fieldType , value ) => { if ( fieldType === SchemaType . Number ) { const floatValue = parseFloat ( value as string ) ; return isNaN ( floatValue ) ? value : floatValue ; } return value ; } ; const toArray = < T > ( v ) : T [ ] => ( Array . isArray ( v ) ? v : [ v ] ) ; const toString = < T > ( v1 ) => String ( v1 ) ; const normalizeBoostValue = ( boost ) : Boost => { if ( ! boost . hasOwnProperty ( 'value' ) ) { return omit ( boost , 'value' ) ; } return { ... boost , type : boost . type as BoostType , value : toArray ( boost . value ) . map ( toString ) , } ; } ; export const normalizeBoostValues = ( boosts ) : Record < string , Boost [ ] > => Object . entries ( boosts ) . reduce ( ( newBoosts , [ fieldName , boostList ] ) => { return { ... newBoosts , [ fieldName ] : boostList . map ( normalizeBoostValue ) , } ; } , { } ) ; export const removeEmptyValueBoosts = ( boosts ) : Record < string , Boost [ ] > => { const filterEmptyValueBoosts = ( fieldBoosts ) => { return fieldBoosts . filter ( ( boost ) => { if ( boost . type !== BoostType . Value ) return true ; const valueBoost = boost as ValueBoost ; const filteredValues = valueBoost . value . filter ( ( value ) => value . trim ( ) !== '' ) ; if ( filteredValues . length ) { boost . value = filteredValues ; return true ; } else { return false ; } } ) ; } ; return Object . entries ( boosts ) . reduce ( ( acc , [ fieldName , fieldBoosts ] ) => { const updatedBoosts = filterEmptyValueBoosts ( fieldBoosts ) ; return updatedBoosts . length ? { ... acc , [ fieldName ] : updatedBoosts } : acc ; } , { } ) ; } ;