1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 13:29:56 +00:00

Merge pull request #1422 from tobibeer/each-list

added list suffix for each filter
This commit is contained in:
Jeremy Ruston 2015-11-05 12:45:27 +00:00
commit 583ed07af2
3 changed files with 33 additions and 15 deletions

View File

@ -3,7 +3,8 @@ title: $:/core/modules/filters/each.js
type: application/javascript
module-type: filteroperator
Filter operator that selects one tiddler for each unique value of the specified field
Filter operator that selects one tiddler for each unique value of the specified field.
With suffix "list", selects all tiddlers that are values in a specified list field.
\*/
(function(){
@ -17,21 +18,33 @@ Export our filter function
*/
exports.each = function(source,operator,options) {
var results =[] ,
values = {};
value,values = {},
field = operator.operand || "title";
if(operator.suffix !== "list") {
source(function(tiddler,title) {
if(tiddler) {
var value;
if((operator.operand === "") || (operator.operand === "title")) {
value = title;
} else {
value = tiddler.getFieldString(operator.operand);
}
value = (field === "title") ? title : tiddler.getFieldString(field);
if(!$tw.utils.hop(values,value)) {
values[value] = true;
results.push(title);
}
}
});
} else {
source(function(tiddler,title) {
if(tiddler) {
$tw.utils.each(
options.wiki.getTiddlerList(title,field),
function(value) {
if(!$tw.utils.hop(values,value)) {
values[value] = true;
results.push(value);
}
}
);
}
});
}
return results;
};

View File

@ -51,12 +51,14 @@ describe("Filter tests", function() {
title: "TiddlerOne",
text: "The quick brown fox in $:/TiddlerTwo",
tags: ["one"],
authors: "Joe Bloggs",
modifier: "JoeBloggs",
modified: "201304152222"});
wiki.addTiddler({
title: "$:/TiddlerTwo",
text: "The rain in Spain\nfalls mainly on the plain and [[a fourth tiddler]]",
tags: ["two"],
authors: "[[John Doe]]",
modifier: "JohnDoe",
modified: "201304152211"});
wiki.addTiddler({
@ -217,6 +219,8 @@ describe("Filter tests", function() {
it("should handle the each operator", function() {
expect(wiki.filterTiddlers("[each[modifier]sort[title]]").join(",")).toBe("$:/TiddlerTwo,TiddlerOne");
expect(wiki.filterTiddlers("[each:list[tags]sort[title]]").join(",")).toBe("one,two");
expect(wiki.filterTiddlers("[each:list[authors]sort[title]]").join(",")).toBe("Bloggs,Joe,John Doe");
});
it("should handle the eachday operator", function() {

View File

@ -6,5 +6,6 @@ type: text/vnd.tiddlywiki
<<.operator-example 1 "[each[color]]">>
<<.operator-example 2 "[sort[title]each[type]]" "the alphabetically first tiddler of each type">>
<<.operator-example 3 "[each:list[list]]" "all tiddlers listed anywhere in the core list field">>
For an example of using the <<.op each>> operator to generate a two-tier list of groups and members, see [[GroupedLists]].