From a37ba2afbeb7c759164f2d5410143223cdf5db01 Mon Sep 17 00:00:00 2001 From: nameanyone Date: Tue, 30 Jun 2015 14:05:03 -0700 Subject: [PATCH] New filter operator "recent" Select tiddlers with a specified date field (default "modified") within the last N days (default 0, meaning today). --- core/modules/filters/recent.js | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 core/modules/filters/recent.js diff --git a/core/modules/filters/recent.js b/core/modules/filters/recent.js new file mode 100644 index 000000000..3a0e9baa0 --- /dev/null +++ b/core/modules/filters/recent.js @@ -0,0 +1,35 @@ +/*\ +title: $:/core/modules/filters/recent.js +type: application/javascript +module-type: filteroperator + +Filter operator that selects tiddlers with a specified date field within the last N days. + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.recent = function(source,operator,options) { + var results = [], + fieldName = operator.suffix || "modified", + targetTimeStamp = (new Date()).setHours(0,0,0,0) - 1000*60*60*24*(parseInt(operator.operand,10)||0); + var isRecent = function(dateField) { + return targetTimeStamp <= (new Date(dateField)).setHours(0,0,0,0); + }; + source(function(tiddler,title) { + if(tiddler && tiddler.fields[fieldName]) { + if(isRecent($tw.utils.parseDate(tiddler.fields[fieldName]))) { + results.push(title); + } + } + }); + return results; +}; + +})();