1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Add some error trapping to maths operators

This commit is contained in:
Jeremy Ruston 2019-06-11 17:18:18 +01:00
parent dd7837d164
commit 86387a9185

View File

@ -76,33 +76,33 @@ exports.min = makeNumericBinaryOperator(
);
exports.fixed = makeNumericBinaryOperator(
function(a,b) {return Number.prototype.toFixed.call(a,b);}
function(a,b) {return Number.prototype.toFixed.call(a,Math.min(Math.max(b,0),100));}
);
exports.precision = makeNumericBinaryOperator(
function(a,b) {return Number.prototype.toPrecision.call(a,b);}
function(a,b) {return Number.prototype.toPrecision.call(a,Math.min(Math.max(b,1),100));}
);
exports.exponential = makeNumericBinaryOperator(
function(a,b) {return Number.prototype.toExponential.call(a,b);}
function(a,b) {return Number.prototype.toExponential.call(a,Math.min(Math.max(b,0),100));}
);
exports.sum = makeNumericArrayOperator(
exports.sum = makeNumericReducingOperator(
function(accumulator,value) {return accumulator + value},
0 // Initial value
);
exports.product = makeNumericArrayOperator(
exports.product = makeNumericReducingOperator(
function(accumulator,value) {return accumulator * value},
1 // Initial value
);
exports.maxall = makeNumericArrayOperator(
exports.maxall = makeNumericReducingOperator(
function(accumulator,value) {return Math.max(accumulator,value)},
-Infinity // Initial value
);
exports.minall = makeNumericArrayOperator(
exports.minall = makeNumericReducingOperator(
function(accumulator,value) {return Math.min(accumulator,value)},
Infinity // Initial value
);
@ -118,7 +118,7 @@ function makeNumericBinaryOperator(fnCalc) {
};
}
function makeNumericArrayOperator(fnCalc,initialValue) {
function makeNumericReducingOperator(fnCalc,initialValue) {
initialValue = initialValue || 0;
return function(source,operator,options) {
var result = [];