diff --git a/core/modules/filters/match.js b/core/modules/filters/match.js new file mode 100644 index 000000000..34caf487e --- /dev/null +++ b/core/modules/filters/match.js @@ -0,0 +1,53 @@ +/*\ +title: $:/core/modules/filters/match.js +type: application/javascript +module-type: filteroperator + +Filter operator for checking if a title matches a string + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.match = function(source,operator,options) { + var results = [], + suffixes = (operator.suffixes || [])[0] || []; + if(suffixes.indexOf("caseinsensitive") !== -1) { + if(operator.prefix === "!") { + source(function(tiddler,title) { + if(title.toLowerCase() !== (operator.operand || "").toLowerCase()) { + results.push(title); + } + }); + } else { + source(function(tiddler,title) { + if(title.toLowerCase() === (operator.operand || "").toLowerCase()) { + results.push(title); + } + }); + } + } else { + if(operator.prefix === "!") { + source(function(tiddler,title) { + if(title !== operator.operand) { + results.push(title); + } + }); + } else { + source(function(tiddler,title) { + if(title === operator.operand) { + results.push(title); + } + }); + } + } + return results; +}; + +})(); diff --git a/editions/prerelease/tiddlers/Release 5.1.20.tid b/editions/prerelease/tiddlers/Release 5.1.20.tid index 9be098d38..3af277070 100644 --- a/editions/prerelease/tiddlers/Release 5.1.20.tid +++ b/editions/prerelease/tiddlers/Release 5.1.20.tid @@ -11,7 +11,7 @@ type: text/vnd.tiddlywiki Added several new [[Mathematics Operators]] for working with numbers: [[negate|negate Operator]], [[abs|abs Operator]], [[ceil|ceil Operator]], [[floor|floor Operator]], [[round|round Operator]], [[trunc|trunc Operator]], [[untrunc|untrunc Operator]], [[sign|sign Operator]], [[add|add Operator]], [[subtract|subtract Operator]], [[multiply|multiply Operator]], [[divide|divide Operator]], [[remainder|remainder Operator]], [[max|max Operator]], [[min|min Operator]], [[fixed|fixed Operator]], [[precision|precision Operator]], [[exponential|exponential Operator]], [[sum|sum Operator]], [[product|product Operator]], [[maxall|maxall Operator]] and [[minall|minall Operator]]. -Added several new string operators: [[length|length Operator]], [[uppercase|uppercase Operator]], [[lowercase|lowercase Operator]], [[titlecase|titlecase Operator]], [[sentencecase|sentencecase Operator]], [[trim|trim Operator]], [[split|split Operator]], [[splitregexp|splitregexp Operator]] and [[join|join Operator]]. +Added several new string operators: [[match|match Operator]], [[length|length Operator]], [[uppercase|uppercase Operator]], [[lowercase|lowercase Operator]], [[titlecase|titlecase Operator]], [[sentencecase|sentencecase Operator]], [[trim|trim Operator]], [[split|split Operator]], [[splitregexp|splitregexp Operator]] and [[join|join Operator]]. There is now finer control over TiddlyWiki's default behaviour of removing duplicates from filter results, making it much easier to work with the new mathematics and string operators: diff --git a/editions/test/tiddlers/tests/test-filters.js b/editions/test/tiddlers/tests/test-filters.js index 7735d283b..ae6f66de7 100644 --- a/editions/test/tiddlers/tests/test-filters.js +++ b/editions/test/tiddlers/tests/test-filters.js @@ -238,6 +238,16 @@ function runTests(wiki) { expect(wiki.filterTiddlers("[[TiddlerOne]tags[]sort[title]]").join(",")).toBe("one"); }); + it("should handle the match operator", function() { + expect(wiki.filterTiddlers("[match[TiddlerOne]]").join(",")).toBe("TiddlerOne"); + expect(wiki.filterTiddlers("TiddlerOne TiddlerOne =[match[TiddlerOne]]").join(",")).toBe("TiddlerOne,TiddlerOne"); + expect(wiki.filterTiddlers("[!match[TiddlerOne]]").join(",")).toBe("$:/ShadowPlugin,$:/TiddlerTwo,Tiddler Three,a fourth tiddler,one"); + expect(wiki.filterTiddlers("[match:casesensitive[tiddlerone]]").join(",")).toBe(""); + expect(wiki.filterTiddlers("[!match:casesensitive[tiddlerone]]").join(",")).toBe("$:/ShadowPlugin,TiddlerOne,$:/TiddlerTwo,Tiddler Three,a fourth tiddler,one"); + expect(wiki.filterTiddlers("[match:caseinsensitive[tiddlerone]]").join(",")).toBe("TiddlerOne"); + expect(wiki.filterTiddlers("[!match:caseinsensitive[tiddlerone]]").join(",")).toBe("$:/ShadowPlugin,$:/TiddlerTwo,Tiddler Three,a fourth tiddler,one"); + }); + it("should handle the tagging operator", function() { expect(wiki.filterTiddlers("[[one]tagging[]sort[title]]").join(",")).toBe("Tiddler Three,Tiddler8,TiddlerOne,TiddlerSeventh"); expect(wiki.filterTiddlers("[[one]tagging[]]").join(",")).toBe("Tiddler Three,TiddlerOne,TiddlerSeventh,Tiddler8"); diff --git a/editions/tw5.com/tiddlers/filters/examples/match Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/match Operator (Examples).tid new file mode 100644 index 000000000..46e82ab0b --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/examples/match Operator (Examples).tid @@ -0,0 +1,8 @@ +created: 20190731080350471 +modified: 20190731080435966 +tags: [[match Operator]] [[Operator Examples]] +title: match Operator (Examples) +type: text/vnd.tiddlywiki + +<<.operator-example 1 "a b c +[match[b]]">> +<<.operator-example 1 "[match[HelloThere]]">> diff --git a/editions/tw5.com/tiddlers/filters/match Operator.tid b/editions/tw5.com/tiddlers/filters/match Operator.tid new file mode 100644 index 000000000..9228b618e --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/match Operator.tid @@ -0,0 +1,30 @@ +caption: match +created: 20190731080209404 +modified: 20190731081047732 +op-input: a [[selection of titles|Title Selection]] +op-neg-output: each item in the input list that does not match the operand string +op-output: each item in the input list that matches the operand string (potentially including duplicates) +op-parameter: the string to be matched +op-purpose: returns each item in the list that matches the operand string +op-suffix: the <<.op match>> operator uses a rich suffix, see below for details +tags: [[Filter Operators]] +title: match Operator +type: text/vnd.tiddlywiki + +<<.from-version "5.1.20">> + +The <<.op match>> operator uses an extended syntax that permits multiple flags to be passed: + +``` +[match:[]] +``` + +* ''flag list'': a comma delimited list of flags +* ''operand'': filter operand + +The available flags are: + +* ''casesensitive'': (default), this flag forces a case-sensitive match, where upper and lower case letters are considered different +* "caseinsensitive". overrides the default so that upper and lower case letters are considered identical for matching purposes + +<<.operator-examples "match">>