1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-03 10:43:16 +00:00
TiddlyWiki5/core/modules/filters/math.js

143 lines
3.3 KiB
JavaScript
Raw Normal View History

/*\
title: $:/core/modules/filters/math.js
type: application/javascript
module-type: filteroperator
Filter operators for math. Unary/binary operators work on each item in turn, and return a new item list.
Sum/product/maxall/minall operate on the entire list, returning a single item.
Note that strings are converted to numbers automatically. Trailing non-digits are ignored.
* "" converts to 0
* "12kk" converts to 12
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.negate = makeNumericBinaryOperator(
function(a) {return -a}
);
exports.abs = makeNumericBinaryOperator(
function(a) {return Math.abs(a)}
);
exports.ceil = makeNumericBinaryOperator(
function(a) {return Math.ceil(a)}
);
exports.floor = makeNumericBinaryOperator(
function(a) {return Math.floor(a)}
);
exports.round = makeNumericBinaryOperator(
function(a) {return Math.round(a)}
);
exports.trunc = makeNumericBinaryOperator(
function(a) {return Math.trunc(a)}
);
exports.sign = makeNumericBinaryOperator(
function(a) {return Math.sign(a)}
);
exports.add = makeNumericBinaryOperator(
function(a,b) {return a + b;}
);
exports.subtract = makeNumericBinaryOperator(
function(a,b) {return a - b;}
);
exports.multiply = makeNumericBinaryOperator(
function(a,b) {return a * b;}
);
exports.divide = makeNumericBinaryOperator(
function(a,b) {return a / b;}
);
exports.remainder = makeNumericBinaryOperator(
function(a,b) {return a % b;}
);
exports.max = makeNumericBinaryOperator(
function(a,b) {return Math.max(a,b);}
);
exports.min = makeNumericBinaryOperator(
function(a,b) {return Math.min(a,b);}
);
2019-02-07 11:18:32 +00:00
exports.fixed = makeNumericBinaryOperator(
function(a,b) {return Number.prototype.toFixed.call(a,Math.min(Math.max(b,0),100));}
2019-02-07 11:18:32 +00:00
);
exports.precision = makeNumericBinaryOperator(
function(a,b) {return Number.prototype.toPrecision.call(a,Math.min(Math.max(b,1),100));}
2019-02-07 11:18:32 +00:00
);
exports.exponential = makeNumericBinaryOperator(
function(a,b) {return Number.prototype.toExponential.call(a,Math.min(Math.max(b,0),100));}
2019-02-07 11:18:32 +00:00
);
exports.sum = makeNumericReducingOperator(
function(accumulator,value) {return accumulator + value},
0 // Initial value
);
exports.product = makeNumericReducingOperator(
function(accumulator,value) {return accumulator * value},
1 // Initial value
);
exports.maxall = makeNumericReducingOperator(
function(accumulator,value) {return Math.max(accumulator,value)},
-Infinity // Initial value
);
exports.minall = makeNumericReducingOperator(
function(accumulator,value) {return Math.min(accumulator,value)},
Infinity // Initial value
);
function makeNumericBinaryOperator(fnCalc) {
return function(source,operator,options) {
var result = [],
numOperand = parseNumber(operator.operand);
source(function(tiddler,title) {
result.push(stringifyNumber(fnCalc(parseNumber(title),numOperand)));
});
return result;
};
2019-02-07 11:18:32 +00:00
}
function makeNumericReducingOperator(fnCalc,initialValue) {
initialValue = initialValue || 0;
return function(source,operator,options) {
var result = [];
source(function(tiddler,title) {
result.push(title);
});
return [stringifyNumber(result.reduce(function(accumulator,currentValue) {
return fnCalc(accumulator,parseNumber(currentValue));
},initialValue))];
};
2019-02-07 11:18:32 +00:00
}
function parseNumber(str) {
return parseFloat(str) || 0;
}
function stringifyNumber(num) {
return num + "";
}
})();