import { isPlainObject , wildCardToRegex , matchWildcard , match } from '@terascope/utils' ; import { BooleanCB } from '../interfaces' ; export function regexp ( regexStr ) { return function regexpTerm ( str ) : boolean { return match ( `^${ regexStr }$` , str ) != null ; } ; } export function wildcard ( wildcardStr ) { return function wildcardTerm ( str ) : boolean { return matchWildcard ( wildcardStr , str ) ; } ; } export function findWildcardField ( field , cb ) { return function WildcardField ( data ) : boolean { const resultsArray = recurseDownObject ( field || '' , data ) ; if ( resultsArray . length === 0 ) return false ; return resultsArray . some ( cb ) ; } ; } function recurseDownObject ( field , object ) { const fieldSequence = field . split ( '.' ) . map ( wildCardToRegex ) ; return recurse ( fieldSequence , object ) ; } function recurse ( arr , obj ) { if ( arr . length === 0 ) return [ ] ; const regExpr = arr . shift ( ) ! ; const results = [ ] ; for ( const [ key , value ] of Object . entries ( obj ) ) { if ( regExpr . exec ( key ) ) { if ( arr . length === 0 ) { results . push ( value ) ; } else if ( isPlainObject ( value ) ) { results . push ( ... recurse ( arr . slice ( ) , value ) ) ; } } } return results ; }