Add "contains" filter operator for searching list fields

This commit is contained in:
Jermolene 2018-08-15 13:50:07 +01:00
parent 3140ff9e49
commit 763f8afaf2
4 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,45 @@
/*\
title: $:/core/modules/filters/contains.js
type: application/javascript
module-type: filteroperator
Filter operator for finding values in array fields
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.contains = function(source,operator,options) {
var results = [],
fieldname = (operator.suffix || "list").toLowerCase();
if(operator.prefix === "!") {
source(function(tiddler,title) {
if(tiddler) {
var list = tiddler.getFieldList(fieldname);
if(list.indexOf(operator.operand) === -1) {
results.push(title);
}
} else {
results.push(title);
}
});
} else {
source(function(tiddler,title) {
if(tiddler) {
var list = tiddler.getFieldList(fieldname);
if(list.indexOf(operator.operand) !== -1) {
results.push(title);
}
}
});
}
return results;
};
})();

View File

@ -39,6 +39,18 @@ exports.getFieldString = function(field) {
}
};
/*
Get the value of a field as a list
*/
exports.getFieldList = function(field) {
var value = this.fields[field];
// Check for a missing field
if(value === undefined || value === null) {
return [];
}
return $tw.utils.parseStringArray(value);
};
/*
Get all the fields as a hashmap of strings. Options:
exclude: an array of field names to exclude

View File

@ -0,0 +1,16 @@
caption: contains
created: 20180815124349784
modified: 20180815124607101
op-input: a [[selection of titles|Title Selection]]
op-neg-output: those input tiddlers in which the list field <<.place F>> does <<.em not>> contain the value <<.place S>>
op-output: those input tiddlers in which the list field <<.place F>> contains the value <<.place S>>
op-parameter: a possible value to be found in list field <<.place F>>
op-parameter-name: S
op-purpose: filter the input by searching list fields for a value
op-suffix: the name of a [[field|TiddlerFields]]
op-suffix-name: F
tags: [[Filter Operators]] [[Field Operators]] [[Negatable Operators]]
title: contains Operator
type: text/vnd.tiddlywiki
<<.operator-examples "contains">>

View File

@ -0,0 +1,8 @@
created: 20180815124630157
modified: 20180815124919662
tags: [[contains Operator]] [[Operator Examples]]
title: contains Operator (Examples)
type: text/vnd.tiddlywiki
<<.operator-example 1 "[contains:tags[HelloThere]]" "equivalent to [tag[HelloThere]]">>
<<.operator-example 2 "[[$:/StoryList]contains[HelloThere]]" "checks whether the tiddler 'HelloThere' is currently displayed in the story river">>