[TW5] Hours Filter - as a counterpart to the Days Filter (suggestion)

54 views
Skip to first unread message

The Bo

unread,
Mar 16, 2017, 5:11:51 AM3/16/17
to TiddlyWiki
Hey guys,

I was thinking about a hours filter which is working same like the days filter to show tiddlers I created or modified the last x hours.
It was quite simple to change some parts of the days core filter. So I want to share my result:


$:/core/modules/filters/hours.js

/*\
title: $:/core/modules/filters/hours.js
type: application/javascript
module-type: filteroperator

Filter operator that selects tiddlers with a specified date field within a specified hour interval.

\*/

(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Export our filter function
*/

exports
.hours = function(source,operator,options) {
       
var results = [],
                fieldName
= operator.suffix || "modified",
                hourInterval
= (parseInt(operator.operand,10)||0),
                hourIntervalSign
= $tw.utils.sign(hourInterval),
                targetTimeStamp
= (new Date()).setMinutes(0,0,0) + 1000*60*60*hourInterval,
                isWithinHours
= function(dateField) {
                       
var sign = $tw.utils.sign(targetTimeStamp - (new Date(dateField)).setMinutes(0,0,0));
                       
return sign === 0 || sign === hourIntervalSign;
               
};

       
if(operator.prefix === "!") {
                targetTimeStamp
= targetTimeStamp - 1000*60*60*hourIntervalSign;
                source
(function(tiddler,title) {
                       
if(tiddler && tiddler.fields[fieldName]) {
                               
if(!isWithinDays($tw.utils.parseDate(tiddler.fields[fieldName]))) {
                                        results
.push(title);
                               
}
                       
}
               
});
       
} else {
                source
(function(tiddler,title) {
                       
if(tiddler && tiddler.fields[fieldName]) {
                               
if(isWithinHours($tw.utils.parseDate(tiddler.fields[fieldName]))) {
                                        results
.push(title);
                               
}
                       
}
               
});
       
}
       
return results;
};

})();
type: application/javascript
module-type: filteroperator


Usage:
e.g. [hours[-2]]

Is there a need for this filter? Are there any suggestions?

Regards,
The Bo

Thomas Elmiger

unread,
Mar 16, 2017, 4:37:21 PM3/16/17
to TiddlyWiki
Hi The Bo

I have at least one time been thinking about using such a filter – but I don't remember where it was … will try your solution when I stumble upon it again.

Thanks for sharing!
Thomas
Reply all
Reply to author
Forward
0 new messages