diff --git a/core/modules/filters/next.js b/core/modules/filters/next.js new file mode 100644 index 0000000000..22c80cc038 --- /dev/null +++ b/core/modules/filters/next.js @@ -0,0 +1,43 @@ +/*\ +title: $:/core/modules/filters/next.js +type: application/javascript +module-type: filteroperator + +Filter operator returning the tiddler whose title occurs next in the list supplied in the operand tiddler + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.next = function(source,operator,options) { + var results = [], + list = options.wiki.getTiddlerList(operator.operand); + + function checkTiddler(title) { + var match = list.indexOf(title); + // increment match and then test if result is in range + match++; + if(match > 0 && match < list.length) { + results.push(list[match]); + } + } + // 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; +}; + +})(); diff --git a/core/modules/filters/previous.js b/core/modules/filters/previous.js new file mode 100644 index 0000000000..cc536b8af0 --- /dev/null +++ b/core/modules/filters/previous.js @@ -0,0 +1,43 @@ +/*\ +title: $:/core/modules/filters/previous.js +type: application/javascript +module-type: filteroperator + +Filter operator returning the tiddler whose title occurs immediately prior in the list supplied in the operand tiddler + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.previous = function(source,operator,options) { + var results = [], + list = options.wiki.getTiddlerList(operator.operand); + + function checkTiddler(title) { + var match = list.indexOf(title); + // decrement match and then test if result is in range + match--; + if( match >= 0 ) { + results.push(list[match]); + } + } + // 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; +}; + +})(); diff --git a/editions/test/tiddlers/tests/test-filters.js b/editions/test/tiddlers/tests/test-filters.js index fe32ea72b7..19d74f35e1 100644 --- a/editions/test/tiddlers/tests/test-filters.js +++ b/editions/test/tiddlers/tests/test-filters.js @@ -148,6 +148,16 @@ describe("Filter tests", function() { expect(wiki.filterTiddlers("[tag[one]list[TiddlerSeventh]sort[title]]").join(",")).toBe("Tiddler Three,TiddlerOne"); }); + it("should handle the next operator", function() { + expect(wiki.filterTiddlers("[[Tiddler Three]next[TiddlerSeventh]]").join(",")).toBe("a fourth tiddler"); + expect(wiki.filterTiddlers("[[MissingTiddler]next[TiddlerSeventh]]").join(",")).toBe(""); + }); + + it("should handle the previous operator", function() { + expect(wiki.filterTiddlers("[[Tiddler Three]previous[TiddlerSeventh]]").join(",")).toBe("TiddlerOne"); + expect(wiki.filterTiddlers("[[TiddlerOne]previous[TiddlerSeventh]]").join(",")).toBe(""); + }); + it("should handle the search operator", function() { expect(wiki.filterTiddlers("[search[the]sort[title]]").join(",")).toBe("$:/TiddlerTwo,a fourth tiddler,one,Tiddler Three,TiddlerOne"); expect(wiki.filterTiddlers("[search{Tiddler8}sort[title]]").join(",")).toBe("$:/TiddlerTwo,a fourth tiddler,one,Tiddler Three,TiddlerOne"); diff --git a/editions/tw5.com/tiddlers/concepts/TiddlerFilters.tid b/editions/tw5.com/tiddlers/concepts/TiddlerFilters.tid index 1e74fa99f5..2976427f44 100644 --- a/editions/tw5.com/tiddlers/concepts/TiddlerFilters.tid +++ b/editions/tw5.com/tiddlers/concepts/TiddlerFilters.tid @@ -48,6 +48,8 @@ A filter string consists of one or more runs of filter operators that each look * ''links'': selects the outgoing links on the currently selected tiddlers * ''backlinks'': selects the tiddlers that link to the currently selected tiddlers * ''list'': selects the tiddlers listed in a specified [[TiddlerList|TiddlerLists]] +* ''next'': selects the next item in a TiddlerList after the current tiddler +* ''previous'': selects the previous item in a TiddlerList before the current tiddler * ''listed'': selects the TiddlerLists that include the current tiddler * ''each'': selects one tiddler for each discrete value of the specified field * ''eachday'': selects one tiddler for each discrete day in the specified date field