From 17711657b6028a177c8aef97f7a0a1c084d65436 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Fri, 2 Aug 2019 14:27:58 +0100 Subject: [PATCH] Add then and else operators Fixes #4147 --- core/modules/filters/else.js | 30 +++++++++++++++++++ core/modules/filters/then.js | 26 ++++++++++++++++ editions/test/tiddlers/tests/test-filters.js | 9 ++++++ .../filters/Conditional Operators.tid | 24 +++++++++++++++ .../tiddlers/filters/else Operator.tid | 15 ++++++++++ .../examples/else Operator (Examples).tid | 9 ++++++ .../examples/then Operator (Examples).tid | 9 ++++++ .../tiddlers/filters/then Operator.tid | 15 ++++++++++ 8 files changed, 137 insertions(+) create mode 100644 core/modules/filters/else.js create mode 100644 core/modules/filters/then.js create mode 100644 editions/tw5.com/tiddlers/filters/Conditional Operators.tid create mode 100644 editions/tw5.com/tiddlers/filters/else Operator.tid create mode 100644 editions/tw5.com/tiddlers/filters/examples/else Operator (Examples).tid create mode 100644 editions/tw5.com/tiddlers/filters/examples/then Operator (Examples).tid create mode 100644 editions/tw5.com/tiddlers/filters/then Operator.tid diff --git a/core/modules/filters/else.js b/core/modules/filters/else.js new file mode 100644 index 000000000..c3829371e --- /dev/null +++ b/core/modules/filters/else.js @@ -0,0 +1,30 @@ +/*\ +title: $:/core/modules/filters/else.js +type: application/javascript +module-type: filteroperator + +Filter operator for replacing an empty input list with a constant, passing a non-empty input list straight through + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.else = function(source,operator,options) { + var results = []; + source(function(tiddler,title) { + results.push(title); + }); + if(results.length === 0) { + return [operator.operand]; + } else { + return results; + } +}; + +})(); diff --git a/core/modules/filters/then.js b/core/modules/filters/then.js new file mode 100644 index 000000000..994775c08 --- /dev/null +++ b/core/modules/filters/then.js @@ -0,0 +1,26 @@ +/*\ +title: $:/core/modules/filters/then.js +type: application/javascript +module-type: filteroperator + +Filter operator for replacing any titles with a constant + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.then = function(source,operator,options) { + var results = []; + source(function(tiddler,title) { + results.push(operator.operand); + }); + return results; +}; + +})(); diff --git a/editions/test/tiddlers/tests/test-filters.js b/editions/test/tiddlers/tests/test-filters.js index ab544c40f..13e939c4d 100644 --- a/editions/test/tiddlers/tests/test-filters.js +++ b/editions/test/tiddlers/tests/test-filters.js @@ -128,6 +128,15 @@ function setupWiki(wikiOptions) { // Our tests function runTests(wiki) { + it("should handle the then and else operators", function() { + expect(wiki.filterTiddlers("[modifier[JoeBloggs]then[JaneBloggs]]").join(",")).toBe("JaneBloggs"); + expect(wiki.filterTiddlers("[!modifier[JoeBloggs]then[JaneBloggs]]").join(",")).toBe("JaneBloggs,JaneBloggs,JaneBloggs,JaneBloggs,JaneBloggs"); + expect(wiki.filterTiddlers("[modifier[DaveBloggs]then[JaneBloggs]]").join(",")).toBe(""); + expect(wiki.filterTiddlers("[modifier[JoeBloggs]else[JaneBloggs]]").join(",")).toBe("TiddlerOne"); + expect(wiki.filterTiddlers("[!modifier[JoeBloggs]else[JaneBloggs]]").join(",")).toBe("$:/ShadowPlugin,$:/TiddlerTwo,Tiddler Three,a fourth tiddler,one"); + expect(wiki.filterTiddlers("[modifier[DaveBloggs]else[JaneBloggs]]").join(",")).toBe("JaneBloggs"); + }); + it("should handle the ~ prefix", function() { expect(wiki.filterTiddlers("[modifier[JoeBloggs]] ~[[No such tiddler]]").join(",")).toBe("TiddlerOne"); expect(wiki.filterTiddlers("[modifier[JaneBloggs]] ~[[No such tiddler]]").join(",")).toBe("No such tiddler"); diff --git a/editions/tw5.com/tiddlers/filters/Conditional Operators.tid b/editions/tw5.com/tiddlers/filters/Conditional Operators.tid new file mode 100644 index 000000000..989f81f15 --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/Conditional Operators.tid @@ -0,0 +1,24 @@ +created: 20190802113703788 +modified: 20190802132727925 +tags: Filters +title: Conditional Operators +type: text/vnd.tiddlywiki + +<<.from-version "5.1.20">>The conditional filter operators allow ''if-then-else'' logic to be expressed within filters. + +The foundation is the convention that an empty list can be used to represent the boolean value ''false'' and a list with at one (or more) entries to represent ''true''. + +The conditional operators are: + +* [[then Operator]] replaces any input values with a constant string. For example: +** <<.inline-operator-example "[[HelloThere]is[missing]then[FOO]]">> +** <<.inline-operator-example "[[Missing Tiddler]is[missing]then[FOO]]">> +* [[else Operator]] if the title list is empty then returns a constant string, otherwise returns the original title list +** <<.inline-operator-example "[[HelloThere]is[tiddler]else[BAR]]">> +** <<.inline-operator-example "[[Missing Tiddler]is[tiddler]else[BAR]]">> + +These operators can be combined. For example: + +* <<.inline-operator-example "[[New Tiddler]is[missing]then[I am missing]else[No I am not missing]]">> + +<> diff --git a/editions/tw5.com/tiddlers/filters/else Operator.tid b/editions/tw5.com/tiddlers/filters/else Operator.tid new file mode 100644 index 000000000..83f6dbe7d --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/else Operator.tid @@ -0,0 +1,15 @@ +caption: else +created: 20190802113024942 +modified: 20190802113919945 +op-input: a [[selection of titles|Title Selection]] +op-output: the original input titles unless empty, in which case return a list with the single entry <<.place E>> +op-parameter: a string +op-parameter-name: E +op-purpose: if the list of input titles is empty then return a list consisting of a single constant string, otherwise return the original titles +tags: [[Conditional Operators]] [[Filter Operators]] +title: else Operator +type: text/vnd.tiddlywiki + +<<.from-version "5.1.20">> See [[Conditional Operators]] for an overview. + +<<.operator-examples "else">> diff --git a/editions/tw5.com/tiddlers/filters/examples/else Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/else Operator (Examples).tid new file mode 100644 index 000000000..590559d8a --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/examples/else Operator (Examples).tid @@ -0,0 +1,9 @@ +created: 20190802113334259 +modified: 20190802113551566 +tags: [[else Operator]] [[Operator Examples]] +title: else Operator (Examples) +type: text/vnd.tiddlywiki + +<<.operator-example 1 "[[HelloThereMissing]is[missing]else[yes]]">> +<<.operator-example 2 "[[HelloThere]is[missing]else[yes]]">> + diff --git a/editions/tw5.com/tiddlers/filters/examples/then Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/then Operator (Examples).tid new file mode 100644 index 000000000..2bf0e1341 --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/examples/then Operator (Examples).tid @@ -0,0 +1,9 @@ +created: 20190802113310992 +modified: 20190802113555129 +tags: [[then Operator]] [[Operator Examples]] +title: then Operator (Examples) +type: text/vnd.tiddlywiki + +<<.operator-example 1 "[[HelloThereMissing]is[missing]then[yes]]">> +<<.operator-example 2 "[[HelloThere]is[missing]then[yes]]">> + diff --git a/editions/tw5.com/tiddlers/filters/then Operator.tid b/editions/tw5.com/tiddlers/filters/then Operator.tid new file mode 100644 index 000000000..e4ea5901e --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/then Operator.tid @@ -0,0 +1,15 @@ +caption: then +created: 20190802112756430 +modified: 20190802113849579 +op-input: a [[selection of titles|Title Selection]] +op-output: the input titles with each one replaced by the string <<.place T>> +op-parameter: a string +op-parameter-name: T +op-purpose: replace input titles by a constant string +tags: [[Conditional Operators]] [[Filter Operators]] +title: then Operator +type: text/vnd.tiddlywiki + +<<.from-version "5.1.20">> See [[Conditional Operators]] for an overview. + +<<.operator-examples "then">>