diff --git a/core/modules/filters/each.js b/core/modules/filters/each.js index 591dfeae0..c60447446 100644 --- a/core/modules/filters/each.js +++ b/core/modules/filters/each.js @@ -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(){ @@ -16,22 +17,34 @@ Filter operator that selects one tiddler for each unique value of the specified Export our filter function */ exports.each = function(source,operator,options) { - var results = [], - values = {}; - source(function(tiddler,title) { - if(tiddler) { - var value; - if((operator.operand === "") || (operator.operand === "title")) { - value = title; - } else { - value = tiddler.getFieldString(operator.operand); + var results =[] , + value,values = {}, + field = operator.operand || "title"; + if(operator.suffix !== "list") { + source(function(tiddler,title) { + if(tiddler) { + value = (field === "title") ? title : tiddler.getFieldString(field); + if(!$tw.utils.hop(values,value)) { + values[value] = true; + results.push(title); + } } - 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; }; diff --git a/editions/test/tiddlers/tests/test-filters.js b/editions/test/tiddlers/tests/test-filters.js index 45ea8919e..1b16b117c 100644 --- a/editions/test/tiddlers/tests/test-filters.js +++ b/editions/test/tiddlers/tests/test-filters.js @@ -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() { diff --git a/editions/tw5.com/tiddlers/filters/examples/each.tid b/editions/tw5.com/tiddlers/filters/examples/each.tid index 0b5017957..091e1bc37 100644 --- a/editions/tw5.com/tiddlers/filters/examples/each.tid +++ b/editions/tw5.com/tiddlers/filters/examples/each.tid @@ -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]].