Add then and else operators

Fixes #4147
This commit is contained in:
Jeremy Ruston 2019-08-02 14:27:58 +01:00
parent 394725f00c
commit 17711657b6
8 changed files with 137 additions and 0 deletions

View File

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

View File

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

View File

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

View File

@ -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]]">>
<<list-links "[tag[Conditional Operators]]">>

View File

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

View File

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

View File

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

View File

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