@@ -2,23 +2,16 @@ import { isComparable } from '../drop/comparable'
2
2
import { Context } from '../context'
3
3
import { isFunction , toValue } from '../util'
4
4
import { isFalsy , isTruthy } from '../render/boolean'
5
+ import { isArray } from '../util/underscore' ;
5
6
6
7
export type UnaryOperatorHandler = ( operand : any , ctx : Context ) => boolean ;
7
8
export type BinaryOperatorHandler = ( lhs : any , rhs : any , ctx : Context ) => boolean ;
8
9
export type OperatorHandler = UnaryOperatorHandler | BinaryOperatorHandler ;
9
10
export type Operators = Record < string , OperatorHandler >
10
11
11
12
export 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 ) ,
22
15
'>' : ( l : any , r : any ) => {
23
16
if ( isComparable ( l ) ) return l . gt ( r )
24
17
if ( isComparable ( r ) ) return r . lt ( l )
@@ -48,3 +41,19 @@ export const defaultOperators: Operators = {
48
41
'and' : ( l : any , r : any , ctx : Context ) => isTruthy ( toValue ( l ) , ctx ) && isTruthy ( toValue ( r ) , ctx ) ,
49
42
'or' : ( l : any , r : any , ctx : Context ) => isTruthy ( toValue ( l ) , ctx ) || isTruthy ( toValue ( r ) , ctx )
50
43
}
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