mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-09-16 01:09:42 +00:00
147 lines
3.4 KiB
JavaScript
147 lines
3.4 KiB
JavaScript
/*\
|
|
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.untrunc = makeNumericBinaryOperator(
|
|
function(a) {return Math.ceil(Math.abs(a)) * Math.sign(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);}
|
|
);
|
|
|
|
exports.fixed = makeNumericBinaryOperator(
|
|
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,Math.min(Math.max(b,1),100));}
|
|
);
|
|
|
|
exports.exponential = makeNumericBinaryOperator(
|
|
function(a,b) {return Number.prototype.toExponential.call(a,Math.min(Math.max(b,0),100));}
|
|
);
|
|
|
|
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;
|
|
};
|
|
}
|
|
|
|
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))];
|
|
};
|
|
}
|
|
|
|
function parseNumber(str) {
|
|
return parseFloat(str) || 0;
|
|
}
|
|
|
|
function stringifyNumber(num) {
|
|
return num + "";
|
|
}
|
|
|
|
})();
|