import { Query } from '@directus/shared/types' ; import generateJoi from './generate-joi' ; export function filterItems ( items , filter ) { if ( ! filter ) return items ; return items . filter ( ( item ) => { return passesFilter ( item , filter ) ; } ) ; function passesFilter ( item , filter ) { if ( ! filter ) return true ; if ( Object . keys ( filter ) [ 0 ] === '_and' ) { const subfilter = Object . values ( filter ) [ 0 ] as Query [ 'filter' ] [ ] ; return subfilter . every ( ( subFilter ) => { return passesFilter ( item , subFilter ) ; } ) ; } else if ( Object . keys ( filter ) [ 0 ] === '_or' ) { const subfilter = Object . values ( filter ) [ 0 ] as Query [ 'filter' ] [ ] ; return subfilter . some ( ( subFilter ) => { return passesFilter ( item , subFilter ) ; } ) ; } else { const schema = generateJoi ( filter ) ; const { error } = schema . validate ( item ) ; return error === undefined ; } } }