Add "before" and "after" filter operators

Fixes #357 by adding new “before” and “after” filter operators.
This commit is contained in:
Jermolene 2014-05-12 15:16:44 +01:00
parent 2633247492
commit e83759e86d
5 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,31 @@
/*\
title: $:/core/modules/filters/after.js
type: application/javascript
module-type: filteroperator
Filter operator returning the tiddler from the current list that is after the tiddler named in the operand.
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.after = function(source,operator,options) {
var results = [];
source(function(tiddler,title) {
results.push(title);
});
var index = results.indexOf(operator.operand);
if(index === -1 || index > (results.length - 2)) {
return [];
} else {
return [results[index + 1]];
}
};
})();

View File

@ -0,0 +1,31 @@
/*\
title: $:/core/modules/filters/before.js
type: application/javascript
module-type: filteroperator
Filter operator returning the tiddler from the current list that is before the tiddler named in the operand.
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.before = function(source,operator,options) {
var results = [];
source(function(tiddler,title) {
results.push(title);
});
var index = results.indexOf(operator.operand);
if(index <= 0) {
return [];
} else {
return [results[index - 1]];
}
};
})();

View File

@ -269,6 +269,15 @@ describe("Filter tests", function() {
expect(wiki.filterTiddlers("[modifier{!!modifier}] +[sort[title]]",fakeWidget).join(",")).toBe("$:/TiddlerTwo,a fourth tiddler,one,Tiddler Three");
});
it("should handle the before and after operators", function() {
expect(wiki.filterTiddlers("[list[TiddlerSeventh]after[TiddlerOne]]").join(",")).toBe("Tiddler Three");
expect(wiki.filterTiddlers("[list[TiddlerSeventh]after[a fourth tiddler]]").join(",")).toBe("MissingTiddler");
expect(wiki.filterTiddlers("[list[TiddlerSeventh]after[MissingTiddler]]").join(",")).toBe("");
expect(wiki.filterTiddlers("[list[TiddlerSeventh]before[TiddlerOne]]").join(",")).toBe("");
expect(wiki.filterTiddlers("[list[TiddlerSeventh]before[a fourth tiddler]]").join(",")).toBe("Tiddler Three");
expect(wiki.filterTiddlers("[list[TiddlerSeventh]before[MissingTiddler]]").join(",")).toBe("a fourth tiddler");
});
});
})();

View File

@ -0,0 +1,20 @@
created: 20140512103123179
modified: 20140512103123179
tags: filters
title: FilterOperator: after
type: text/vnd.tiddlywiki
The ''after'' filter operator returns the tiddler from the current list that is after the tiddler named in the operand.
The following examples assume a tiddler called `MyList` with a `list` field containing:
```
one two three four five
```
|!Filter String |!Description |
|`[list[MyList]after[two]]` |Returns `three` |
|`[list[MyList]after[five]]` |Returns an empty list |
|`[list[MyList]after{!!title}]` |Returns `five` if the current tiddler is `four` |
See also [[FilterOperator: before]], [[FilterOperator: previous]] and [[FilterOperator: next]].

View File

@ -0,0 +1,20 @@
created: 20140512103123179
modified: 20140512103123179
tags: filters
title: FilterOperator: before
type: text/vnd.tiddlywiki
The ''before'' filter operator returns the tiddler from the current list that is before the tiddler named in the operand.
The following examples assume a tiddler called `MyList` with a `list` field containing:
```
one two three four five
```
|!Filter String |!Description |
|`[list[MyList]before[two]]` |Returns `one` |
|`[list[MyList]before[one]]` |Returns an empty list |
|`[list[MyList]before{!!title}]` |Returns `three` if the current tiddler is `four` |
See also [[FilterOperator: after]], [[FilterOperator: previous]] and [[FilterOperator: next]].