@@ -2,23 +2,16 @@ import { isComparable } from '../drop/comparable'
22import { Context } from '../context'
33import { isFunction , toValue } from '../util'
44import { isFalsy , isTruthy } from '../render/boolean'
5+ import { isArray } from '../util/underscore' ;
56
67export type UnaryOperatorHandler = ( operand : any , ctx : Context ) => boolean ;
78export type BinaryOperatorHandler = ( lhs : any , rhs : any , ctx : Context ) => boolean ;
89export type OperatorHandler = UnaryOperatorHandler | BinaryOperatorHandler ;
910export type Operators = Record < string , OperatorHandler >
1011
1112export const defaultOperators : Operators = {
12- '==' : ( l : any , r : any ) => {
13- if ( isComparable ( l ) ) return l . equals ( r )
14- if ( isComparable ( r ) ) return r . equals ( l )
15- return toValue ( l ) === toValue ( r )
16- } ,
17- '!=' : ( l : any , r : any ) => {
18- if ( isComparable ( l ) ) return ! l . equals ( r )
19- if ( isComparable ( r ) ) return ! r . equals ( l )
20- return toValue ( l ) !== toValue ( r )
21- } ,
13+ '==' : equal ,
14+ '!=' : ( l : any , r : any ) => ! equal ( l , r ) ,
2215 '>' : ( l : any , r : any ) => {
2316 if ( isComparable ( l ) ) return l . gt ( r )
2417 if ( isComparable ( r ) ) return r . lt ( l )
@@ -48,3 +41,19 @@ export const defaultOperators: Operators = {
4841 'and' : ( l : any , r : any , ctx : Context ) => isTruthy ( toValue ( l ) , ctx ) && isTruthy ( toValue ( r ) , ctx ) ,
4942 'or' : ( l : any , r : any , ctx : Context ) => isTruthy ( toValue ( l ) , ctx ) || isTruthy ( toValue ( r ) , ctx )
5043}
44+
45+ function equal ( lhs : any , rhs : any ) : boolean {
46+ if ( isComparable ( lhs ) ) return lhs . equals ( rhs )
47+ if ( isComparable ( rhs ) ) return rhs . equals ( lhs )
48+ lhs = toValue ( lhs )
49+ rhs = toValue ( rhs )
50+ if ( isArray ( lhs ) ) {
51+ return isArray ( rhs ) && arrayEqual ( lhs , rhs )
52+ }
53+ return lhs === rhs
54+ }
55+
56+ function arrayEqual ( lhs : any [ ] , rhs : any [ ] ) : boolean {
57+ if ( lhs . length !== rhs . length ) return false
58+ return ! lhs . some ( ( value , i ) => ! equal ( value , rhs [ i ] ) )
59+ }
0 commit comments