1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-16 02:19:55 +00:00

Refactored the is operator for simplicity and efficiency. (#3240)

* Refactored the is operator for simplicity and efficiency.

* Improve `is` filter documentation.

* Update is.js

* extracted `subops.length` to `num_of_subops`
* renamed `subop` to `operator` for clarity/differentiation from `subops`
* refactored to avoid using a `Set` object.

* Update is.js
This commit is contained in:
Matt Lauber 2018-05-09 13:07:08 -04:00 committed by Jeremy Ruston
parent 715cb1d1bc
commit 0ab9ec1ad3
2 changed files with 30 additions and 26 deletions

View File

@ -27,40 +27,44 @@ Export our filter function
*/
exports.is = function(source,operator,options) {
var isFilterOperators = getIsFilterOperators(),
subops = operator.operand.split("+"),
num_of_subops = subops.length;
if( !operator.operand) {
// Return all tiddlers if the operand is missing
//Make sure all the operands are defined.
for (var t = 0; t < num_of_subops; t++){
if( !isFilterOperators[subops[t]] ) {
return [$tw.language.getString("Error/IsFilterOperator")];
}
}
if(num_of_subops === 0) { // Return all tiddlers if the operand is missing
var results = [];
source(function(tiddler,title) {
results.push(title);
});
return results;
}
} else if(num_of_subops === 1) { // Shortcut the Single Operator
var operator = isFilterOperators[subops[0]];
return operator(source,operator.prefix,options);
// Get our isfilteroperators
var isFilterOperators = getIsFilterOperators(),
subops = operator.operand.split("+"),
filteredResults = {},
results = [];
for (var t=0; t<subops.length; t++) {
var subop = isFilterOperators[subops[t]];
if(subop) {
filteredResults[subops[t]] = subop(source,operator.prefix,options);
} else {
return [$tw.language.getString("Error/IsFilterOperator")];
} else { // Handle multiple operators
var filtered_results = {},
results = [];
for(var t=0; t < num_of_subops; t++){
var operator = isFilterOperators[subops[t]];
operator(source,operator.prefix,options).forEach(function(element) { filtered_results[element] = "present"});
}
// Sort the output by the input (There may be a better way to do this)
source(function(tiddler,title) {
if(filtered_results[title] === "present") {
results.push(title);
}
});
return results;
}
source(function(tiddler,title) {
for (var t=0; t<subops.length; t++) {
if (filteredResults[subops[t]].indexOf(title) != -1){
results.push(title);
break;
}
}
});
return results;
};
})();

View File

@ -14,7 +14,7 @@ op-neg-output: those input tiddlers that do <<.em not>> belong to category <<.pl
The parameter <<.place C>> specifies zero or more fundamental categories using the following syntax:
<$railroad text="""
[{: ("current" | "missing" |: "orphans" | "shadows" | "tags" | "tiddlers" ) +"+" }]
[{: ("current" | "missing" |: "orphan" | "shadow" | "system" | "tag" | "tiddler" | "image") +"+" }]
"""/>
|!Category |!Matches any tiddler that... |