diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index 37807c5fe2..bbf7adf22e 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -3073,7 +3073,7 @@ return "[].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }"; }, modulo: function() { - return "function(a, b) { return (a % b + b) % b; }"; + return "function(a, b) { return (a % b + +b) % b; }"; }, hasProp: function() { return '{}.hasOwnProperty'; diff --git a/src/nodes.coffee b/src/nodes.coffee index 5d1d31c544..be1484db21 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -2194,7 +2194,7 @@ UTILITIES = " modulo: -> """ - function(a, b) { return (a % b + b) % b; } + function(a, b) { return (a % b + +b) % b; } """ # Shortcuts to speed up the lookup time for native functions. diff --git a/test/operators.coffee b/test/operators.coffee index a16bd9f78c..6821b0df02 100644 --- a/test/operators.coffee +++ b/test/operators.coffee @@ -355,3 +355,8 @@ test "modulo operator compound assignment", -> a = -2 a %%= 5 eq 3, a + +test "modulo operator converts arguments to numbers", -> + eq 1, 1 %% '42' + eq 1, '1' %% 42 + eq 1, '1' %% '42'