Skip to content

Commit

Permalink
Merge pull request #2644 from SomMeri/percentage-should-error-on-nan-…
Browse files Browse the repository at this point in the history
…2553

`percentage` function should throw error if result would be `NaN`
  • Loading branch information
lukeapage committed Sep 17, 2015
2 parents 8dc3bfb + e8efa6e commit 1f624bd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
16 changes: 16 additions & 0 deletions lib/less/functions/math-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var Dimension = require("../tree/dimension");

var MathHelper = function() {
};
MathHelper._math = function (fn, unit, n) {
if (!(n instanceof Dimension)) {
throw { type: "Argument", message: "argument must be a number" };
}
if (unit == null) {
unit = n.unit;
} else {
n = n.unify();
}
return new Dimension(fn(parseFloat(n.value)), unit);
};
module.exports = MathHelper;
20 changes: 4 additions & 16 deletions lib/less/functions/math.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Dimension = require("../tree/dimension"),
functionRegistry = require("./function-registry");
var functionRegistry = require("./function-registry"),
mathHelper = require("./math-helper.js");

var mathFunctions = {
// name, unit
Expand All @@ -15,27 +15,15 @@ var mathFunctions = {
acos: "rad"
};

function _math(fn, unit, n) {
if (!(n instanceof Dimension)) {
throw { type: "Argument", message: "argument must be a number" };
}
if (unit == null) {
unit = n.unit;
} else {
n = n.unify();
}
return new Dimension(fn(parseFloat(n.value)), unit);
}

for (var f in mathFunctions) {
if (mathFunctions.hasOwnProperty(f)) {
mathFunctions[f] = _math.bind(null, Math[f], mathFunctions[f]);
mathFunctions[f] = mathHelper._math.bind(null, Math[f], mathFunctions[f]);
}
}

mathFunctions.round = function (n, f) {
var fraction = typeof f === "undefined" ? 0 : f.value;
return _math(function(num) { return num.toFixed(fraction); }, null, n);
return mathHelper._math(function(num) { return num.toFixed(fraction); }, null, n);
};

functionRegistry.addMultiple(mathFunctions);
9 changes: 7 additions & 2 deletions lib/less/functions/number.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Dimension = require("../tree/dimension"),
Anonymous = require("../tree/anonymous"),
functionRegistry = require("./function-registry");
functionRegistry = require("./function-registry"),
mathHelper = require("./math-helper.js");

var minMax = function (isMin, args) {
args = Array.prototype.slice.call(args);
Expand Down Expand Up @@ -71,6 +72,10 @@ functionRegistry.addMultiple({
return new Dimension(Math.pow(x.value, y.value), x.unit);
},
percentage: function (n) {
return new Dimension(n.value * 100, '%');
var result = mathHelper._math(function(num) {
return num * 100;
}, '%', n);

return result;
}
});
3 changes: 3 additions & 0 deletions test/less/errors/percentage-non-number-argument.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div {
percentage: percentage(16/17);
}
4 changes: 4 additions & 0 deletions test/less/errors/percentage-non-number-argument.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ArgumentError: error evaluating function `percentage`: argument must be a number in {path}percentage-non-number-argument.less on line 2, column 15:
1 div {
2 percentage: percentage(16/17);
3 }

0 comments on commit 1f624bd

Please sign in to comment.