From 445c15e719d3451b2d96c04aee77fe893272957a Mon Sep 17 00:00:00 2001 From: saqimtiaz Date: Mon, 9 Nov 2020 19:27:45 +0100 Subject: [PATCH] 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 --- core/modules/filters/x-listops.js | 11 +++++++++-- editions/test/tiddlers/tests/test-filters.js | 7 +++++++ .../filters/toggle Operator (Examples).tid | 8 +++++++- .../tiddlers/filters/toggle Operator.tid | 18 ++++++++++++++---- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/core/modules/filters/x-listops.js b/core/modules/filters/x-listops.js index ca04b0e2b..60801bffc 100644 --- a/core/modules/filters/x-listops.js +++ b/core/modules/filters/x-listops.js @@ -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; }; diff --git a/editions/test/tiddlers/tests/test-filters.js b/editions/test/tiddlers/tests/test-filters.js index 31c66b86f..b40cea45c 100644 --- a/editions/test/tiddlers/tests/test-filters.js +++ b/editions/test/tiddlers/tests/test-filters.js @@ -752,6 +752,13 @@ function runTests(wiki) { expect(wiki.filterTiddlers("[!sortsub:string]",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]",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"); diff --git a/editions/tw5.com/tiddlers/filters/toggle Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/toggle Operator (Examples).tid index e53b4ceb0..95e3d56a8 100644 --- a/editions/tw5.com/tiddlers/filters/toggle Operator (Examples).tid +++ b/editions/tw5.com/tiddlers/filters/toggle Operator (Examples).tid @@ -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]]" /> +``` diff --git a/editions/tw5.com/tiddlers/filters/toggle Operator.tid b/editions/tw5.com/tiddlers/filters/toggle Operator.tid index ffb84b00b..d01cb522a 100644 --- a/editions/tw5.com/tiddlers/filters/toggle Operator.tid +++ b/editions/tw5.com/tiddlers/filters/toggle Operator.tid @@ -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'' : 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">>