import { isSearchExpressionGroup } from './group' ; import { Search , SearchExpression , SearchExpressionClause , SearchExpressionGroup , SearchExpressionOperators } from './types' ; function invertSearchExpressionGroup ( input ) { const output = { expressions : input . expressions . map ( invertSearchExpression ) } ; if ( input . clause ) { output . clause = invertedClauses [ input . clause ] ; } return output ; } const invertedOperators = { '!=' : '==' , '==' : '!=' , '<' : '>=' , '>=' : '<' , '<=' : '>' , '>' : '<=' , '!contains' : 'contains' , 'contains' : '!contains' , '!isnullorEmpty' : 'isnullorEmpty' , 'isnullorEmpty' : '!isnullorEmpty' , '!starts' : 'starts' , 'starts' : '!starts' } ; const invertedClauses = { '&&' : '||' , '||' : '&&' } ; function invertSearchExpression ( input ) { const operator = invertedOperators [ input . operator ] ; const output = { ... input , operator } ; if ( input . clause ) { output . clause = invertedClauses [ input . clause ] ; } return output ; } export function invert ( search ) { if ( Array . isArray ( search ) ) { return ( search as SearchExpressionGroup [ ] ) . map ( invertSearchExpressionGroup ) ; } else if ( isSearchExpressionGroup ( search ) ) { return invertSearchExpressionGroup ( search as SearchExpressionGroup ) ; } else { return invertSearchExpression ( search as SearchExpression ) ; } }