@@ -207,6 +207,46 @@ describe('customization', () => {
207207 expect ( go ( { a : 2 } , [ 'aboutEq' , [ 'get' , 'a' ] , 2 ] , options ) ) . toEqual ( true )
208208 expect ( go ( { a : 2 } , [ 'aboutEq' , [ 'get' , 'a' ] , '2' ] , options ) ) . toEqual ( true )
209209 } )
210+
211+ test ( 'should use valueOf() in case of non-primitive types in all operators' , ( ) => {
212+ class BigNumber {
213+ public _value : string
214+
215+ constructor ( public value : string ) {
216+ this . _value = value
217+ }
218+
219+ valueOf : ( ) => number = ( ) => Number . parseFloat ( this . value )
220+ }
221+
222+ const three = new BigNumber ( '3' )
223+ const six = new BigNumber ( '6' )
224+ const two = new BigNumber ( '2' )
225+ const anotherTwo = new BigNumber ( '2' )
226+
227+ // Test whether we can use our BigNumber.valueOf()
228+ // @ts -ignore
229+ expect ( two + three ) . toEqual ( 5 )
230+
231+ expect ( go ( null , [ 'eq' , three , two ] as JSONQuery ) ) . toEqual ( false )
232+ expect ( go ( null , [ 'eq' , two , anotherTwo ] as JSONQuery ) ) . toEqual ( true )
233+ expect ( go ( null , [ 'ne' , three , two ] as JSONQuery ) ) . toEqual ( true )
234+ expect ( go ( null , [ 'ne' , two , anotherTwo ] as JSONQuery ) ) . toEqual ( false )
235+
236+ expect ( go ( null , [ 'gt' , three , two ] as JSONQuery ) ) . toEqual ( true )
237+ expect ( go ( null , [ 'gte' , three , two ] as JSONQuery ) ) . toEqual ( true )
238+ expect ( go ( null , [ 'lt' , three , two ] as JSONQuery ) ) . toEqual ( false )
239+ expect ( go ( null , [ 'lte' , three , two ] as JSONQuery ) ) . toEqual ( false )
240+
241+ expect ( go ( null , [ 'add' , three , two ] as JSONQuery ) ) . toEqual ( 5 )
242+ expect ( go ( null , [ 'subtract' , three , two ] as JSONQuery ) ) . toEqual ( 1 )
243+ expect ( go ( null , [ 'multiply' , three , two ] as JSONQuery ) ) . toEqual ( 6 )
244+ expect ( go ( null , [ 'divide' , six , two ] as JSONQuery ) ) . toEqual ( 3 )
245+ expect ( go ( null , [ 'pow' , three , two ] as JSONQuery ) ) . toEqual ( 9 )
246+ expect ( go ( null , [ 'mod' , three , two ] as JSONQuery ) ) . toEqual ( 1 )
247+
248+ expect ( go ( [ three , two ] , [ 'sort' ] as JSONQuery ) ) . toEqual ( [ two , three ] )
249+ } )
210250} )
211251
212252test ( 'should validate the compile test-suite against its JSON schema' , ( ) => {
0 commit comments