From cf75b209bac8b1f5c13636d08e0a936ffeb41747 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Mon, 27 May 2013 17:59:33 +0100 Subject: [PATCH] Add `eachday` and `sameday` filter operators These operators will enable us to group the recent changes list by day --- core/modules/filters/eachday.js | 42 ++++++++++++++++++++++++++++++ core/modules/filters/sameday.js | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 core/modules/filters/eachday.js create mode 100644 core/modules/filters/sameday.js diff --git a/core/modules/filters/eachday.js b/core/modules/filters/eachday.js new file mode 100644 index 000000000..dae0f26af --- /dev/null +++ b/core/modules/filters/eachday.js @@ -0,0 +1,42 @@ +/*\ +title: $:/core/modules/filters/eachday.js +type: application/javascript +module-type: filteroperator + +Filter operator that selects one tiddler for each unique day covered by the specified date field + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.eachday = 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 day 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).substr(0,8); + if(!$tw.utils.hop(values,value)) { + values[value] = true; + results.push(title); + } + } + }); + return results; +}; + +})(); diff --git a/core/modules/filters/sameday.js b/core/modules/filters/sameday.js new file mode 100644 index 000000000..6fdb2d30c --- /dev/null +++ b/core/modules/filters/sameday.js @@ -0,0 +1,46 @@ +/*\ +title: $:/core/modules/filters/sameday.js +type: application/javascript +module-type: filteroperator + +Filter operator that selects tiddlers with a modified date field on the same day as the provided value. + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.sameday = function(source,operator,options) { + var results = []; + // Function to check an individual title + function checkTiddler(title) { + var tiddler = options.wiki.getTiddler(title); + if(tiddler) { + var match = tiddler.getFieldString("modified").substr(0,8) === operator.operand.substr(0,8); + if(operator.prefix === "!") { + match = !match; + } + if(match) { + results.push(title); + } + } + }; + // Iterate through the source tiddlers + if($tw.utils.isArray(source)) { + $tw.utils.each(source,function(title) { + checkTiddler(title); + }); + } else { + $tw.utils.each(source,function(element,title) { + checkTiddler(title); + }); + } + return results; +}; + +})();