@@ -887,36 +887,27 @@ defaultMethods['==='].compile = function (data, buildState) {
887
887
for ( let i = 2 ; i < data . length ; i ++ ) res = buildState . compile `(${ res } && ${ data [ i - 1 ] } === ${ data [ i ] } )`
888
888
return res
889
889
}
890
+
891
+ /**
892
+ * Transforms the operands of the arithmetic operation to numbers.
893
+ */
894
+ function numberCoercion ( i , buildState ) {
895
+ if ( Array . isArray ( i ) ) return 'NaN'
896
+ if ( typeof i === 'string' || typeof i === 'number' || typeof i === 'boolean' ) return `(+${ buildString ( i , buildState ) } )`
897
+ return `(+precoerceNumber(${ buildString ( i , buildState ) } ))`
898
+ }
899
+
890
900
// @ts -ignore Allow custom attribute
891
901
defaultMethods [ '+' ] . compile = function ( data , buildState ) {
892
- if ( Array . isArray ( data ) ) {
893
- return `(${ data
894
- . map ( ( i ) => {
895
- // Todo: Actually make this correct, this is a decent optimization but
896
- // does not coerce the built string.
897
- if ( Array . isArray ( i ) ) return 'NaN'
898
- return `(+${ buildString ( i , buildState ) } )`
899
- } )
900
- . join ( ' + ' ) } )`
901
- } else if ( typeof data === 'string' || typeof data === 'number' || typeof data === 'boolean' ) {
902
- return `(+${ buildString ( data , buildState ) } )`
903
- } else {
904
- return `([].concat(${ buildString (
905
- data ,
906
- buildState
907
- ) } )).reduce((a,b) => (+a)+(+b), 0)`
908
- }
902
+ if ( Array . isArray ( data ) ) return `(${ data . map ( i => numberCoercion ( i , buildState ) ) . join ( ' + ' ) } )`
903
+ if ( typeof data === 'string' || typeof data === 'number' || typeof data === 'boolean' ) return `(+${ buildString ( data , buildState ) } )`
904
+ return `([].concat(${ buildString ( data , buildState ) } )).reduce((a,b) => (+a)+(+precoerceNumber(b)), 0)`
909
905
}
910
906
911
907
// @ts -ignore Allow custom attribute
912
908
defaultMethods [ '%' ] . compile = function ( data , buildState ) {
913
- if ( Array . isArray ( data ) ) {
914
- return `(${ data
915
- . map ( ( i ) => `(+${ buildString ( i , buildState ) } )` )
916
- . join ( ' % ' ) } )`
917
- } else {
918
- return `(${ buildString ( data , buildState ) } ).reduce((a,b) => (+a)%(+b))`
919
- }
909
+ if ( Array . isArray ( data ) ) return `(${ data . map ( i => numberCoercion ( i , buildState ) ) . join ( ' % ' ) } )`
910
+ return `(${ buildString ( data , buildState ) } ).reduce((a,b) => (+a)%(+precoerceNumber(b)))`
920
911
}
921
912
922
913
// @ts -ignore Allow custom attribute
@@ -927,11 +918,7 @@ defaultMethods.in.compile = function (data, buildState) {
927
918
928
919
// @ts -ignore Allow custom attribute
929
920
defaultMethods [ '-' ] . compile = function ( data , buildState ) {
930
- if ( Array . isArray ( data ) ) {
931
- return `${ data . length === 1 ? '-' : '' } (${ data
932
- . map ( ( i ) => `(+${ buildString ( i , buildState ) } )` )
933
- . join ( ' - ' ) } )`
934
- }
921
+ if ( Array . isArray ( data ) ) return `${ data . length === 1 ? '-' : '' } (${ data . map ( i => numberCoercion ( i , buildState ) ) . join ( ' - ' ) } )`
935
922
if ( typeof data === 'string' || typeof data === 'number' ) {
936
923
return `(-${ buildString ( data , buildState ) } )`
937
924
} else {
@@ -943,23 +930,13 @@ defaultMethods['-'].compile = function (data, buildState) {
943
930
}
944
931
// @ts -ignore Allow custom attribute
945
932
defaultMethods [ '/' ] . compile = function ( data , buildState ) {
946
- if ( Array . isArray ( data ) ) {
947
- return `(${ data
948
- . map ( ( i ) => `(+${ buildString ( i , buildState ) } )` )
949
- . join ( ' / ' ) } )`
950
- } else {
951
- return `(${ buildString ( data , buildState ) } ).reduce((a,b) => (+a)/(+b))`
952
- }
933
+ if ( Array . isArray ( data ) ) return `(${ data . map ( i => numberCoercion ( i , buildState ) ) . join ( ' / ' ) } )`
934
+ return `(${ buildString ( data , buildState ) } ).reduce((a,b) => (+a)/(+b))`
953
935
}
954
936
// @ts -ignore Allow custom attribute
955
937
defaultMethods [ '*' ] . compile = function ( data , buildState ) {
956
- if ( Array . isArray ( data ) ) {
957
- return `(${ data
958
- . map ( ( i ) => `(+${ buildString ( i , buildState ) } )` )
959
- . join ( ' * ' ) } )`
960
- } else {
961
- return `(${ buildString ( data , buildState ) } ).reduce((a,b) => (+a)*(+b))`
962
- }
938
+ if ( Array . isArray ( data ) ) return `(${ data . map ( i => numberCoercion ( i , buildState ) ) . join ( ' * ' ) } )`
939
+ return `(${ buildString ( data , buildState ) } ).reduce((a,b) => (+a)*(+b))`
963
940
}
964
941
// @ts -ignore Allow custom attribute
965
942
defaultMethods . cat . compile = function ( data , buildState ) {
0 commit comments