2020-10-06 18:20:03 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/filters/reduce.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: filteroperator
|
|
|
|
|
2020-10-08 16:21:31 +00:00
|
|
|
Filter operator evaluates a subfilter for each item, making the running total available in the variable `accumulator`, and the current index available in the variable `index`
|
2020-10-06 18:20:03 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Export our filter function
|
|
|
|
*/
|
|
|
|
exports.reduce = function(source,operator,options) {
|
|
|
|
// Accumulate the list
|
|
|
|
var results = [];
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
results.push(title);
|
|
|
|
});
|
|
|
|
// Run the filter over each item
|
|
|
|
var filterFn = options.wiki.compileFilter(operator.operand),
|
2020-12-04 18:31:23 +00:00
|
|
|
accumulator = operator.operands[1] || "";
|
2020-10-06 18:20:03 +00:00
|
|
|
for(var index=0; index<results.length; index++) {
|
|
|
|
var title = results[index],
|
2022-10-25 13:13:38 +00:00
|
|
|
list = filterFn.call(options.wiki,options.wiki.makeTiddlerIterator([title]),options.widget.makeFakeWidgetWithVariables({
|
|
|
|
"currentTiddler": "" + title,
|
|
|
|
"..currentTiddler": options.widget.getVariable("currentTiddler"),
|
|
|
|
"accumulator": "" + accumulator,
|
|
|
|
"index": "" + index,
|
|
|
|
"revIndex": "" + (results.length - 1 - index),
|
|
|
|
"length": "" + results.length
|
|
|
|
}));
|
2020-10-06 18:20:03 +00:00
|
|
|
if(list.length > 0) {
|
|
|
|
accumulator = "" + list[0];
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 10:07:52 +00:00
|
|
|
if(results.length > 0) {
|
|
|
|
return [accumulator];
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
2020-10-06 18:20:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|