1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-26 17:06:51 +00:00

Refactoring of new filter list operators

This commit is contained in:
Jermolene 2014-01-25 17:44:36 +00:00
parent 02d3861d7d
commit e6843aabff

View File

@ -3,7 +3,7 @@ title: $:/core/modules/filters/listops.js
type: application/javascript type: application/javascript
module-type: filteroperator module-type: filteroperator
Filter operator for manipulating lists Filter operators for manipulating the current selection list
\*/ \*/
(function(){ (function(){
@ -13,104 +13,75 @@ Filter operator for manipulating lists
"use strict"; "use strict";
/* /*
Export our filter function Reverse list
*/ */
exports.reverse = function(source,operator,options) { exports.reverse = function(source,operator,options) {
var results = []; var results = [];
// Iterate through the source tiddlers if(!$tw.utils.isArray(source)) {
if($tw.utils.isArray(source)) { source = Object.keys(source).sort();
}
$tw.utils.each(source,function(title) { $tw.utils.each(source,function(title) {
results.unshift(title); results.unshift(title);
}); });
} else {
$tw.utils.each(source,function(element,title) {
results.unshift(title);
});
}
return results; return results;
}; };
/*
First entry/entries in list
*/
exports.first = function(source,operator,options) { exports.first = function(source,operator,options) {
var results = []; var count = parseInt(operator.operand) || 1;
var count = operator.operand || 1; if(!$tw.utils.isArray(source)) {
// Iterate through the source tiddlers source = Object.keys(source).sort();
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; return source.slice(0,Math.min(count,source.length));
}; };
/*
Last entry/entries in list
*/
exports.last = function(source,operator,options) { exports.last = function(source,operator,options) {
var results = []; var count = parseInt(operator.operand) || 1;
var count = operator.operand || 1; if(!$tw.utils.isArray(source)) {
// Iterate through the source tiddlers source = Object.keys(source).sort();
if($tw.utils.isArray(source)) {
results = source.slice(-count);
} else {
for(var title in source) {
results.push(title);
};
results = results.slice(-count);
} }
return results; return source.slice(-count);
}; };
/*
All but the first entry/entries of the list
*/
exports.rest = function(source,operator,options) { exports.rest = function(source,operator,options) {
var results = []; var count = parseInt(operator.operand) || 1;
var count = operator.operand || 1; if(!$tw.utils.isArray(source)) {
// Iterate through the source tiddlers source = Object.keys(source).sort();
if($tw.utils.isArray(source)) {
results = source.slice(count);
} else {
for(var title in source) {
if(--count < 0) {
results.push(title);
} }
return source.slice(count);
}; };
}
return results;
};
exports.butfirst = exports.rest; exports.butfirst = exports.rest;
exports.bf = exports.rest; exports.bf = exports.rest;
/*
All but the last entry/entries of the list
*/
exports.butlast = function(source,operator,options) { exports.butlast = function(source,operator,options) {
var results = []; var count = parseInt(operator.operand) || 1;
var count = operator.operand || 1; if(!$tw.utils.isArray(source)) {
// Iterate through the source tiddlers source = Object.keys(source).sort();
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; return source.slice(0,-count);
}; };
exports.bl = exports.butlast; exports.bl = exports.butlast;
/*
The nth member of the list
*/
exports.nth = function(source,operator,options) { exports.nth = function(source,operator,options) {
var results = []; var count = parseInt(operator.operand) || 1;
var count = operator.operand || 1; if(!$tw.utils.isArray(source)) {
// Iterate through the source tiddlers source = Object.keys(source).sort();
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; return source.slice(count-1,count);
}; };
})(); })();