From ca51634041d0c7dd358f04b06d760bb5b0418def Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Mon, 27 May 2013 17:59:03 +0100 Subject: [PATCH] Add the `each` filter operator This makes it possible to do grouping within lists --- core/modules/filters/each.js | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 core/modules/filters/each.js diff --git a/core/modules/filters/each.js b/core/modules/filters/each.js new file mode 100644 index 000000000..aad58cea1 --- /dev/null +++ b/core/modules/filters/each.js @@ -0,0 +1,42 @@ +/*\ +title: $:/core/modules/filters/each.js +type: application/javascript +module-type: filteroperator + +Filter operator that selects one tiddler for each unique value of the specified field + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.each = function(source,operator,options) { + // Convert the source to an array if necessary + if(!$tw.utils.isArray(source)) { + var copy = []; + $tw.utils.each(source,function(element,title) { + copy.push(title); + }); + source = copy; + } + // Collect up the first tiddler with each unique value of the specified field + var results = [],values = {}; + $tw.utils.each(source,function(title) { + var tiddler = options.wiki.getTiddler(title); + if(tiddler) { + var value = tiddler.getFieldString(operator.operand); + if(!$tw.utils.hop(values,value)) { + values[value] = true; + results.push(title); + } + } + }); + return results; +}; + +})();