@@ -887,36 +887,27 @@ defaultMethods['==='].compile = function (data, buildState) {
887887 for ( let i = 2 ; i < data . length ; i ++ ) res = buildState . compile `(${ res } && ${ data [ i - 1 ] } === ${ data [ i ] } )`
888888 return res
889889}
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+
890900// @ts -ignore Allow custom attribute
891901defaultMethods [ '+' ] . 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)`
909905}
910906
911907// @ts -ignore Allow custom attribute
912908defaultMethods [ '%' ] . 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)))`
920911}
921912
922913// @ts -ignore Allow custom attribute
@@ -927,11 +918,7 @@ defaultMethods.in.compile = function (data, buildState) {
927918
928919// @ts -ignore Allow custom attribute
929920defaultMethods [ '-' ] . 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 ( ' - ' ) } )`
935922 if ( typeof data === 'string' || typeof data === 'number' ) {
936923 return `(-${ buildString ( data , buildState ) } )`
937924 } else {
@@ -943,23 +930,13 @@ defaultMethods['-'].compile = function (data, buildState) {
943930}
944931// @ts -ignore Allow custom attribute
945932defaultMethods [ '/' ] . 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))`
953935}
954936// @ts -ignore Allow custom attribute
955937defaultMethods [ '*' ] . 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))`
963940}
964941// @ts -ignore Allow custom attribute
965942defaultMethods . cat . compile = function ( data , buildState ) {
0 commit comments