mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-23 10:07:19 +00:00
Add the each
filter operator
This makes it possible to do grouping within lists
This commit is contained in:
parent
887d955fc4
commit
ca51634041
42
core/modules/filters/each.js
Normal file
42
core/modules/filters/each.js
Normal file
@ -0,0 +1,42 @@
|
||||
/*\
|
||||
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
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
/*
|
||||
Export our filter function
|
||||
*/
|
||||
exports.each = function(source,operator,options) {
|
||||
// Convert the source to an array if necessary
|
||||
if(!$tw.utils.isArray(source)) {
|
||||
var copy = [];
|
||||
$tw.utils.each(source,function(element,title) {
|
||||
copy.push(title);
|
||||
});
|
||||
source = copy;
|
||||
}
|
||||
// Collect up the first tiddler with each unique value of the specified field
|
||||
var results = [],values = {};
|
||||
$tw.utils.each(source,function(title) {
|
||||
var tiddler = options.wiki.getTiddler(title);
|
||||
if(tiddler) {
|
||||
var value = tiddler.getFieldString(operator.operand);
|
||||
if(!$tw.utils.hop(values,value)) {
|
||||
values[value] = true;
|
||||
results.push(title);
|
||||
}
|
||||
}
|
||||
});
|
||||
return results;
|
||||
};
|
||||
|
||||
})();
|
Loading…
Reference in New Issue
Block a user