2017-09-15 03:04:51 +00:00
|
|
|
// Work out what % value is of the range between min and max.
|
|
|
|
// If max isn't given then you get the % of value to min.
|
|
|
|
// You can optionally specify an upperMax, which is a mid-way point in the range
|
|
|
|
// that represents 100%, after which the % starts to go down to zero again.
|
|
|
|
|
|
|
|
var Percent = function (value, min, max, upperMax)
|
2016-12-07 17:16:59 +00:00
|
|
|
{
|
2017-09-15 03:04:51 +00:00
|
|
|
if (max === undefined) { max = min + 1; }
|
2016-12-07 17:16:59 +00:00
|
|
|
|
2017-09-15 03:04:51 +00:00
|
|
|
var percentage = (value - min) / (max - min);
|
|
|
|
|
|
|
|
if (percentage > 1)
|
2016-12-07 17:16:59 +00:00
|
|
|
{
|
2017-09-15 03:04:51 +00:00
|
|
|
if (upperMax !== undefined)
|
|
|
|
{
|
|
|
|
percentage = ((upperMax - value)) / (upperMax - max);
|
|
|
|
|
|
|
|
if (percentage < 0)
|
|
|
|
{
|
|
|
|
percentage = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
percentage = 1;
|
|
|
|
}
|
2016-12-07 17:16:59 +00:00
|
|
|
}
|
2017-09-15 03:04:51 +00:00
|
|
|
else if (percentage < 0)
|
2016-12-07 17:16:59 +00:00
|
|
|
{
|
2017-09-15 03:04:51 +00:00
|
|
|
percentage = 0;
|
2016-12-07 17:16:59 +00:00
|
|
|
}
|
2017-09-15 03:04:51 +00:00
|
|
|
|
|
|
|
return percentage;
|
2016-12-07 17:16:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Percent;
|