Skip to content

percentage function should throw error if result would be NaN #2644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }