mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-09-20 19:29:43 +00:00
6570561d4e
* The `!has[tags]` filter didn't work because "tags" is an array The negated `has` filter only considered empty strings, but not empty arrays (such as the `tags` field). * Add tests for `has` filter operator with array-like fields (tags, list)
71 lines
1.7 KiB
JavaScript
71 lines
1.7 KiB
JavaScript
/*\
|
|
title: $:/core/modules/filters/has.js
|
|
type: application/javascript
|
|
module-type: filteroperator
|
|
|
|
Filter operator for checking if a tiddler has the specified field or index
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
/*
|
|
Export our filter function
|
|
*/
|
|
exports.has = function(source,operator,options) {
|
|
var results = [],
|
|
invert = operator.prefix === "!";
|
|
|
|
if(operator.suffix === "field") {
|
|
if(invert) {
|
|
source(function(tiddler,title) {
|
|
if(!tiddler || (tiddler && (!$tw.utils.hop(tiddler.fields,operator.operand)))) {
|
|
results.push(title);
|
|
}
|
|
});
|
|
} else {
|
|
source(function(tiddler,title) {
|
|
if(tiddler && $tw.utils.hop(tiddler.fields,operator.operand)) {
|
|
results.push(title);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
else if(operator.suffix === "index") {
|
|
if(invert) {
|
|
source(function(tiddler,title) {
|
|
if(!tiddler || (tiddler && (!$tw.utils.hop(options.wiki.getTiddlerDataCached(tiddler,Object.create(null)),operator.operand)))) {
|
|
results.push(title);
|
|
}
|
|
});
|
|
} else {
|
|
source(function(tiddler,title) {
|
|
if(tiddler && $tw.utils.hop(options.wiki.getTiddlerDataCached(tiddler,Object.create(null)),operator.operand)) {
|
|
results.push(title);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
else {
|
|
if(invert) {
|
|
source(function(tiddler,title) {
|
|
if(!tiddler || !$tw.utils.hop(tiddler.fields,operator.operand) || (tiddler.fields[operator.operand].length === 0)) {
|
|
results.push(title);
|
|
}
|
|
});
|
|
} else {
|
|
source(function(tiddler,title) {
|
|
if(tiddler && $tw.utils.hop(tiddler.fields,operator.operand) && (tiddler.fields[operator.operand].length !== 0)) {
|
|
results.push(title);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
return results;
|
|
};
|
|
|
|
})();
|