2014-01-01 21:38:08 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/filters/listops.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: filteroperator
|
|
|
|
|
2014-01-25 17:44:36 +00:00
|
|
|
Filter operators for manipulating the current selection list
|
2014-01-01 21:38:08 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2016-11-28 19:16:08 +00:00
|
|
|
/*
|
|
|
|
Order a list
|
|
|
|
*/
|
|
|
|
exports.order = function(source,operator,options) {
|
|
|
|
var results = [];
|
|
|
|
if(operator.operand.toLowerCase() === "reverse") {
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
results.unshift(title);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
results.push(title);
|
2018-04-02 18:40:47 +00:00
|
|
|
});
|
2016-11-28 19:16:08 +00:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
2014-01-01 21:38:08 +00:00
|
|
|
/*
|
2014-01-25 17:44:36 +00:00
|
|
|
Reverse list
|
2014-01-01 21:38:08 +00:00
|
|
|
*/
|
|
|
|
exports.reverse = function(source,operator,options) {
|
|
|
|
var results = [];
|
2014-04-03 19:49:16 +00:00
|
|
|
source(function(tiddler,title) {
|
2014-01-25 17:44:36 +00:00
|
|
|
results.unshift(title);
|
|
|
|
});
|
2014-01-01 21:38:08 +00:00
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
2014-01-25 17:44:36 +00:00
|
|
|
/*
|
|
|
|
First entry/entries in list
|
|
|
|
*/
|
2014-01-01 21:38:08 +00:00
|
|
|
exports.first = function(source,operator,options) {
|
2018-04-02 18:40:47 +00:00
|
|
|
var count = $tw.utils.getInt(operator.operand,1),
|
2014-04-03 19:49:16 +00:00
|
|
|
results = [];
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
results.push(title);
|
|
|
|
});
|
|
|
|
return results.slice(0,count);
|
2014-01-01 21:38:08 +00:00
|
|
|
};
|
|
|
|
|
2014-01-25 17:44:36 +00:00
|
|
|
/*
|
|
|
|
Last entry/entries in list
|
|
|
|
*/
|
2014-01-01 21:38:08 +00:00
|
|
|
exports.last = function(source,operator,options) {
|
2018-04-02 18:40:47 +00:00
|
|
|
var count = $tw.utils.getInt(operator.operand,1),
|
2014-04-03 19:49:16 +00:00
|
|
|
results = [];
|
2023-10-25 12:14:49 +00:00
|
|
|
if(count === 0) return results;
|
2014-04-03 19:49:16 +00:00
|
|
|
source(function(tiddler,title) {
|
|
|
|
results.push(title);
|
|
|
|
});
|
|
|
|
return results.slice(-count);
|
2014-01-01 21:38:08 +00:00
|
|
|
};
|
|
|
|
|
2014-01-25 17:44:36 +00:00
|
|
|
/*
|
|
|
|
All but the first entry/entries of the list
|
|
|
|
*/
|
2014-01-01 21:38:08 +00:00
|
|
|
exports.rest = function(source,operator,options) {
|
2018-04-02 18:40:47 +00:00
|
|
|
var count = $tw.utils.getInt(operator.operand,1),
|
2014-04-03 19:49:16 +00:00
|
|
|
results = [];
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
results.push(title);
|
|
|
|
});
|
|
|
|
return results.slice(count);
|
2014-01-01 21:38:08 +00:00
|
|
|
};
|
2014-01-03 23:01:17 +00:00
|
|
|
exports.butfirst = exports.rest;
|
|
|
|
exports.bf = exports.rest;
|
|
|
|
|
2014-01-25 17:44:36 +00:00
|
|
|
/*
|
|
|
|
All but the last entry/entries of the list
|
|
|
|
*/
|
2014-01-03 23:01:17 +00:00
|
|
|
exports.butlast = function(source,operator,options) {
|
2018-04-02 18:40:47 +00:00
|
|
|
var count = $tw.utils.getInt(operator.operand,1),
|
2014-04-03 19:49:16 +00:00
|
|
|
results = [];
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
results.push(title);
|
|
|
|
});
|
2022-02-26 09:42:24 +00:00
|
|
|
var index = count === 0 ? results.length : -count;
|
|
|
|
return results.slice(0,index);
|
2014-01-03 23:01:17 +00:00
|
|
|
};
|
|
|
|
exports.bl = exports.butlast;
|
|
|
|
|
2014-01-25 17:44:36 +00:00
|
|
|
/*
|
|
|
|
The nth member of the list
|
|
|
|
*/
|
2014-01-03 23:01:17 +00:00
|
|
|
exports.nth = function(source,operator,options) {
|
2018-04-02 18:40:47 +00:00
|
|
|
var count = $tw.utils.getInt(operator.operand,1),
|
2014-04-03 19:49:16 +00:00
|
|
|
results = [];
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
results.push(title);
|
|
|
|
});
|
|
|
|
return results.slice(count - 1,count);
|
2014-01-03 23:01:17 +00:00
|
|
|
};
|
|
|
|
|
2021-10-30 08:52:38 +00:00
|
|
|
/*
|
|
|
|
The zero based nth member of the list
|
|
|
|
*/
|
|
|
|
exports.zth = function(source,operator,options) {
|
|
|
|
var count = $tw.utils.getInt(operator.operand,0),
|
|
|
|
results = [];
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
results.push(title);
|
|
|
|
});
|
|
|
|
return results.slice(count,count + 1);
|
|
|
|
};
|
|
|
|
|
2014-01-01 21:38:08 +00:00
|
|
|
})();
|