From 763f8afaf2dc7dcaa2d4a492887a944d84c4fed2 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Wed, 15 Aug 2018 13:50:07 +0100 Subject: [PATCH] Add "contains" filter operator for searching list fields --- core/modules/filters/contains.js | 45 +++++++++++++++++++ core/modules/tiddler.js | 12 +++++ .../tiddlers/filters/contains Operator.tid | 16 +++++++ .../examples/contains Operator (Examples).tid | 8 ++++ 4 files changed, 81 insertions(+) create mode 100644 core/modules/filters/contains.js create mode 100644 editions/tw5.com/tiddlers/filters/contains Operator.tid create mode 100644 editions/tw5.com/tiddlers/filters/examples/contains Operator (Examples).tid diff --git a/core/modules/filters/contains.js b/core/modules/filters/contains.js new file mode 100644 index 000000000..964c6700f --- /dev/null +++ b/core/modules/filters/contains.js @@ -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; +}; + +})(); diff --git a/core/modules/tiddler.js b/core/modules/tiddler.js index e3382af4a..efec78b5c 100644 --- a/core/modules/tiddler.js +++ b/core/modules/tiddler.js @@ -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 diff --git a/editions/tw5.com/tiddlers/filters/contains Operator.tid b/editions/tw5.com/tiddlers/filters/contains Operator.tid new file mode 100644 index 000000000..608d22def --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/contains Operator.tid @@ -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">> diff --git a/editions/tw5.com/tiddlers/filters/examples/contains Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/contains Operator (Examples).tid new file mode 100644 index 000000000..9140068fc --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/examples/contains Operator (Examples).tid @@ -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">>