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 type: application/javascript
module-type: filteroperator 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(){ (function(){
@ -16,22 +17,34 @@ Filter operator that selects one tiddler for each unique value of the specified
Export our filter function Export our filter function
*/ */
exports.each = function(source,operator,options) { exports.each = function(source,operator,options) {
var results = [], var results =[] ,
values = {}; value,values = {},
source(function(tiddler,title) { field = operator.operand || "title";
if(tiddler) { if(operator.suffix !== "list") {
var value; source(function(tiddler,title) {
if((operator.operand === "") || (operator.operand === "title")) { if(tiddler) {
value = title; value = (field === "title") ? title : tiddler.getFieldString(field);
} else { if(!$tw.utils.hop(values,value)) {
value = tiddler.getFieldString(operator.operand); values[value] = true;
results.push(title);
}
} }
if(!$tw.utils.hop(values,value)) { });
values[value] = true; } else {
results.push(title); 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; return results;
}; };

View File

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

View File

@ -6,5 +6,6 @@ type: text/vnd.tiddlywiki
<<.operator-example 1 "[each[color]]">> <<.operator-example 1 "[each[color]]">>
<<.operator-example 2 "[sort[title]each[type]]" "the alphabetically first tiddler of each type">> <<.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]]. For an example of using the <<.op each>> operator to generate a two-tier list of groups and members, see [[GroupedLists]].