mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-08-05 21:33:52 +00:00
parent
394725f00c
commit
17711657b6
30
core/modules/filters/else.js
Normal file
30
core/modules/filters/else.js
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
})();
|
26
core/modules/filters/then.js
Normal file
26
core/modules/filters/then.js
Normal 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;
|
||||||
|
};
|
||||||
|
|
||||||
|
})();
|
@ -128,6 +128,15 @@ function setupWiki(wikiOptions) {
|
|||||||
// Our tests
|
// Our tests
|
||||||
function runTests(wiki) {
|
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() {
|
it("should handle the ~ prefix", function() {
|
||||||
expect(wiki.filterTiddlers("[modifier[JoeBloggs]] ~[[No such tiddler]]").join(",")).toBe("TiddlerOne");
|
expect(wiki.filterTiddlers("[modifier[JoeBloggs]] ~[[No such tiddler]]").join(",")).toBe("TiddlerOne");
|
||||||
expect(wiki.filterTiddlers("[modifier[JaneBloggs]] ~[[No such tiddler]]").join(",")).toBe("No such tiddler");
|
expect(wiki.filterTiddlers("[modifier[JaneBloggs]] ~[[No such tiddler]]").join(",")).toBe("No such tiddler");
|
||||||
|
24
editions/tw5.com/tiddlers/filters/Conditional Operators.tid
Normal file
24
editions/tw5.com/tiddlers/filters/Conditional Operators.tid
Normal 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]]">>
|
15
editions/tw5.com/tiddlers/filters/else Operator.tid
Normal file
15
editions/tw5.com/tiddlers/filters/else Operator.tid
Normal 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">>
|
@ -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]]">>
|
||||||
|
|
@ -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]]">>
|
||||||
|
|
15
editions/tw5.com/tiddlers/filters/then Operator.tid
Normal file
15
editions/tw5.com/tiddlers/filters/then Operator.tid
Normal 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">>
|
Loading…
x
Reference in New Issue
Block a user