1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-01 01:33:16 +00:00
TiddlyWiki5/core/modules/filters/sortsub.js
2020-04-25 11:26:19 +01:00

45 lines
1.2 KiB
JavaScript

/*\
title: $:/core/modules/filters/sortsub.js
type: application/javascript
module-type: filteroperator
Filter operator for sorting by a subfilter
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.sortsub = function(source,operator,options) {
// Collect the input titles
var inputTitles = [];
source(function(tiddler,title) {
inputTitles.push(title);
});
// Pass them through the subfilter to get the sort keys
var sortKeys = options.wiki.filterTiddlers(operator.operand,options.widget,options.wiki.makeTiddlerIterator(inputTitles));
// Rather than sorting the titles array, we'll sort the indexes so that we can consult both arrays
var indexes = [];
while(inputTitles.length > indexes.length) {
indexes.push(indexes.length);
}
// Sort the indexes
var compareFn = $tw.utils.makeCompareFunction(operator.suffix,{defaultType: "string",invert: operator.prefix === "!"});
indexes = indexes.sort(function(a,b) {
return compareFn(sortKeys[a],sortKeys[b]);
});
// Make the results array in order
var results = [];
$tw.utils.each(indexes,function(index) {
results.push(inputTitles[index]);
});
return results;
};
})();