Add "match" operator for string comparison

Fixes #4130
fix-syncer
Jeremy Ruston 2019-07-31 09:11:12 +01:00
rodzic 7f78065992
commit 5faae2547d
5 zmienionych plików z 102 dodań i 1 usunięć

Wyświetl plik

@ -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;
};
})();

Wyświetl plik

@ -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:

Wyświetl plik

@ -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");

Wyświetl plik

@ -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]]">>

Wyświetl plik

@ -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>[<operand>]]
```
* ''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">>