import * as _ from 'lodash' ; import keyPathify from "./keyPathify" ; import { State } from './state' ; import { Predicate } from 'storyboard-lang' ; export default function checkPredicate ( predicate , state ) { if ( ! predicate ) { return true ; } console . log ( predicate ) ; function check ( memo , obj , key ) { const value = keyPathify ( key , state ) || key ; if ( key === "or" ) { if ( ! _ . isArray ( obj ) ) { return false ; } return _ . reduce ( obj , ( ( memo , p ) => memo || checkPredicate ( p , state ) ) , false ) ; } if ( key === "and" ) { if ( ! _ . isArray ( obj ) ) { return false ; } return _ . reduce ( obj , ( ( memo , p ) => memo && checkPredicate ( p , state ) ) , true ) ; } if ( key === "not" ) { return ! checkPredicate ( obj , state ) ; } if ( ! _ . isUndefined ( obj . or ) && _ . isArray ( obj . or ) ) { memo = memo && _ . reduce ( obj . or , ( ( m , o , k ) => m || check ( true , o , key ) ) , false ) ; } if ( ! _ . isUndefined ( obj . eq ) ) { memo = memo && ( value === obj . eq || ( value === keyPathify ( obj . eq , state , true ) ) ) ; } if ( ! _ . isUndefined ( obj . neq ) ) { memo = memo && ( value !== obj . neq && ( value !== keyPathify ( obj . neq , state , true ) ) ) ; } if ( ! _ . isUndefined ( obj . gte ) ) { memo = memo && ( value >= keyPathify ( obj . gte , state , true ) ) ; } if ( ! _ . isUndefined ( obj . lte ) ) { memo = memo && ( value <= keyPathify ( obj . lte , state , true ) ) ; } if ( ! _ . isUndefined ( obj . exists ) ) { memo = memo && ( obj . exists !== _ . isUndefined ( keyPathify ( value , state ) ) ) ; } return memo ; } return _ . reduce ( predicate , check , true ) ; }