mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-28 11:00:27 +00:00
Merge pull request #1422 from tobibeer/each-list
added list suffix for each filter
This commit is contained in:
commit
583ed07af2
@ -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;
|
||||
};
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -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]].
|
||||
|
Loading…
Reference in New Issue
Block a user