mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-03-06 11:38:10 +00:00
Merge pull request #326 from Skeeve/listops
New list operations from @Skeeve
This commit is contained in:
commit
592ef257a2
116
core/modules/filters/listops.js
Normal file
116
core/modules/filters/listops.js
Normal file
@ -0,0 +1,116 @@
|
||||
/*\
|
||||
title: $:/core/modules/filters/listops.js
|
||||
type: application/javascript
|
||||
module-type: filteroperator
|
||||
|
||||
Filter operator for manipulating lists
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
/*
|
||||
Export our filter function
|
||||
*/
|
||||
exports.reverse = function(source,operator,options) {
|
||||
var results = [];
|
||||
// Iterate through the source tiddlers
|
||||
if($tw.utils.isArray(source)) {
|
||||
$tw.utils.each(source,function(title) {
|
||||
results.unshift(title);
|
||||
});
|
||||
} else {
|
||||
$tw.utils.each(source,function(element,title) {
|
||||
results.unshift(title);
|
||||
});
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
exports.first = function(source,operator,options) {
|
||||
var results = [];
|
||||
var count = operator.operand || 1;
|
||||
// Iterate through the source tiddlers
|
||||
if($tw.utils.isArray(source)) {
|
||||
results = source.slice(0, Math.min(count, source.length));
|
||||
} else {
|
||||
for(var title in source) {
|
||||
if(count-- < 1) break;
|
||||
results.push(title);
|
||||
};
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
exports.last = function(source,operator,options) {
|
||||
var results = [];
|
||||
var count = operator.operand || 1;
|
||||
// Iterate through the source tiddlers
|
||||
if($tw.utils.isArray(source)) {
|
||||
results = source.slice(-count);
|
||||
} else {
|
||||
for(var title in source) {
|
||||
results.push(title);
|
||||
};
|
||||
results = results.slice(-count);
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
exports.rest = function(source,operator,options) {
|
||||
var results = [];
|
||||
var count = operator.operand || 1;
|
||||
// Iterate through the source tiddlers
|
||||
if($tw.utils.isArray(source)) {
|
||||
results = source.slice(count);
|
||||
} else {
|
||||
for(var title in source) {
|
||||
if(--count < 0) {
|
||||
results.push(title);
|
||||
}
|
||||
};
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
exports.butfirst = exports.rest;
|
||||
exports.bf = exports.rest;
|
||||
|
||||
exports.butlast = function(source,operator,options) {
|
||||
var results = [];
|
||||
var count = operator.operand || 1;
|
||||
// Iterate through the source tiddlers
|
||||
if($tw.utils.isArray(source)) {
|
||||
results = source.slice(0,-count);
|
||||
} else {
|
||||
for(var title in source) {
|
||||
results.push(title);
|
||||
};
|
||||
results = results.slice(0,-count);
|
||||
}
|
||||
return results;
|
||||
};
|
||||
exports.bl = exports.butlast;
|
||||
|
||||
exports.nth = function(source,operator,options) {
|
||||
var results = [];
|
||||
var count = operator.operand || 1;
|
||||
// Iterate through the source tiddlers
|
||||
if($tw.utils.isArray(source)) {
|
||||
results = source.slice(count-1,count);
|
||||
} else {
|
||||
for(var title in source) {
|
||||
--count;
|
||||
if(count > 0) continue;
|
||||
if(count < 0) break;
|
||||
results.push(title);
|
||||
break;
|
||||
};
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
})();
|
@ -59,6 +59,16 @@ A filter string consists of one or more runs of filter operators that each look
|
||||
* ''sameday'': selects all the tiddlers falling into the same day as the provided date in the specified date field
|
||||
* ''fields'': returns the names of the fields present on the selected tiddlers
|
||||
* ''search'': returns all tiddlers that contain the specified text
|
||||
* ''reverse'': reverses the list
|
||||
* ''first'': selects the first tiddler of the list (or the first n if the operand is n)
|
||||
* ''last'': selects the last tiddler of the list (or the last n if the operand is n)
|
||||
* ''rest'': selects all but the first tiddler of the list (or all but the first n if the operand is n)
|
||||
* ''butfirst'': synonym for ''rest''
|
||||
* ''bf'': another synonym for ''rest''
|
||||
* ''butlast'': selects all but the last tiddler of the list (or all but the last n if the operand is n)
|
||||
* ''bl'': another synonym for ''butlast''
|
||||
* ''nth'': selects the n-th tiddler of the list. Defaults to n = 1.
|
||||
|
||||
|
||||
An operator can be negated with by preceding it with `!`, for example `[!tag[Tommy]]` selects the tiddlers that are not tagged with `Tommy`.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user