1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Extend toggle operator (#5015)

* Extend toggle operator to support optional second operand to toggle a value pair

* Added tests for extended toggle filter

* Updated docs for toggle operator
This commit is contained in:
saqimtiaz 2020-11-09 19:27:45 +01:00 committed by GitHub
parent 27bed615ab
commit 445c15e719
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 7 deletions

View File

@ -193,11 +193,18 @@ Extended filter operators to manipulate the current list.
*/
exports.toggle = function(source, operator) {
var results = prepare_results(source),
index = results.indexOf(operator.operand);
index = results.indexOf(operator.operand),
pairIndex = (operator.operands[1] ? results.indexOf(operator.operands[1]) : -1);
if(index === -1) {
results.push(operator.operand);
if(pairIndex !== -1) {
results.splice(pairIndex,1);
}
} else {
results.splice(index, 1);
results.splice(index,1);
if(operator.operands[1]) {
results.push(operator.operands[1]);
}
}
return results;
};

View File

@ -752,6 +752,13 @@ function runTests(wiki) {
expect(wiki.filterTiddlers("[!sortsub:string<sort2>]",anchorWidget).join(",")).toBe("filter regexp test,$:/TiddlerTwo,Tiddler Three,a fourth tiddler,$:/ShadowPlugin,has filter,hasList,TiddlerOne,one");
expect(wiki.filterTiddlers("[[TiddlerOne]] [[$:/TiddlerTwo]] [[Tiddler Three]] [[a fourth tiddler]] +[!sortsub:number<sort3>]",anchorWidget).join(",")).toBe("$:/TiddlerTwo,Tiddler Three,TiddlerOne,a fourth tiddler");
});
it("should handle the toggle operator", function() {
expect(wiki.filterTiddlers("[[Tiddler Three]tags[]] +[toggle[one]]").join(",")).toBe("two");
expect(wiki.filterTiddlers("[[Tiddler Three]tags[]] -[[one]] +[toggle[one]]").join(",")).toBe("two,one");
expect(wiki.filterTiddlers("[[Tiddler Three]tags[]] +[toggle[three],[four]]").join(",")).toBe("one,two,three");
expect(wiki.filterTiddlers("[[Tiddler Three]tags[]] [[three]] +[toggle[three],[four]]").join(",")).toBe("one,two,four");
});
it("should handle multiple operands for search-replace", function() {
var widget = require("$:/core/modules/widgets/widget.js");

View File

@ -1,5 +1,5 @@
created: 20201107154352695
modified: 20201107155030228
modified: 20201109104951727
title: toggle Operator (Examples)
type: text/vnd.tiddlywiki
@ -18,3 +18,9 @@ To toggle a title in a field:
```
<$action-listops $tiddler="target" $field="my-field" $subfilter="+[toggle[done]]" />
```
To toggle between a value pair:
```
<$action-listops $tiddler="target" $field="my-field" $subfilter="+[toggle[todo],[done]]" />
```

View File

@ -1,13 +1,23 @@
caption: toggle
created: 20201107153758990
modified: 20201107154300086
modified: 20201109104654310
op-input: a list of items
op-output: the input list with the title <<.place R>> toggled
op-parameter: an item to toggle in the input list
op-parameter-name: R
op-output: the input list with the title specified in the parameter toggled
op-parameter: the <<.op toggle>> operator accepts 1 or 2 parameters, see below for details
op-purpose: toggle the title specified in the operand in the input
tags: [[Filter Operators]] [[Listops Operators]] [[Order Operators]]
title: toggle Operator
type: text/vnd.tiddlywiki
<<.from-version "5.1.23">>
The <<.op search-replace>> operator requires at least one parameter and can accept a second optional parameter:
```
[toggle[<title1>],[<title2>]]
```
* ''title1'' : a title to toggle in the input list. If it is already present, it is removed. Otherwise, it is added.
* ''title2'': (optional). When the second parameter is provided, the operator toggles between the two values in the input list. If neither is present, the first parameter takes precedence is added to the list.
<<.operator-examples "toggle">>