import { OperatorFunctionMap , AllOperators } from "../../../filters/Operators" ; /**
 * A Filter node
 */ export interface IFilterNode { /**
     * Checks if @param input passed the test at this node
     */ isPassed ( input ) : boolean ; } /**
 * This node has no children.
 * It is a filter operation on one field of an input.
 */ export class LeafNode implements IFilterNode { private fieldkey : string ; private filter : any ; constructor ( fieldkey , filter ) { this . fieldkey = fieldkey ; this . filter = filter ; } public isPassed ( input ) { const actualValue = input [ this . fieldkey ] ; return Object . keys ( this . filter ) . reduce ( ( prev , cur ) => { const op = OperatorFunctionMap [ cur as ( keyof AllOperators ) ] ; const targetValue = this . filter [ cur ] ; return prev && op . opFunction ( actualValue , targetValue ) ; } , true ) ; } } /**
 * This Node performs an AND operation on its children
 */ export class ANDNode implements IFilterNode { private nodes : IFilterNode [ ] ; constructor ( nodes ) { this . nodes = nodes ; } isPassed ( input ) { return ( this . nodes . reduce ( ( prev , cur ) => { return ( prev && cur . isPassed ( input ) ) ; } , true ) ) ; } } /**
 * This Node performs an OR operation on its children
 */ export class ORNode implements IFilterNode { private nodes : IFilterNode [ ] ; constructor ( nodes ) { this . nodes = nodes ; } isPassed ( input ) { return this . nodes . reduce ( ( prev , cur ) => { return prev || cur . isPassed ( input ) ; } , false ) ; } } /**
 * This Node negates the result of
 * an AND operation on its children
 */ export class NotNode implements IFilterNode { private root : ANDNode ; constructor ( root ) { this . root = root ; } isPassed ( input ) { return ! this . root . isPassed ( input ) ; } }