Tiddlywiki5: [Request] Else-if logic in list filter

Created on 20 Nov 2018  路  9Comments  路  Source: Jermolene/TiddlyWiki5

Please consider introducing a param like "emptyFilter" for list widget which will run if the initial filter turns up empty. Like emptymessage, but a filter. So it would help creating logic like -
"List all tiddlers tagged fruits, and if there are no fruits, list all tiddlers tagged vegetables"

All 9 comments

Hi @ibnishak I'd been thinking of tackling this via a new filter run prefix that only processes the run if the current result set is empty. For example, if we use ~ as the operator:

[enlist{MyTiddler}] ~[[Fallback]]

Then

  • If "MyTiddler" contained "one two three", the list one two three would be returned
  • If "MyTiddler" contained "[[one two]]", the list [[one two]] would be returned
  • If "MyTiddler" were blank, a list containing the single entry Fallback would be returned

That would be better actually. That way it would permit piping more than one condition with filter steps. Sort of like switch-case. Any chance we could see this soon?

It would also enable modification of defaults without changing core. Like <$list filter="<<user-choices>> ~[[defaults]]"/>

Hi @ibnishak I've made a branch with a quick implementation. Much harder and more tedious is that we also need a bunch of documentation updates (including the railroad diagram in https://tiddlywiki.com/#Filter%20Expression)...

I was just looking at updating the documentation for this, will this else prefix be able to be used more than once in a set of filter runs? and even respond to a previous else prefix?

Eg
[all[tiddlers]has[fieldname]] ~[all[tiddlers]has[fieldname2]] ~[all[tiddlers]has[fieldname3]]

I have included an attempt at updating https://tiddlywiki.com/#Filter%20Expression

<$railroad text="""
[{:
  [: [[whitespace|"Filter Whitespace"]] ]
  ("+"|:-|"-"|"~")
  [[run|"Filter Run"]]
}]
"""/>

A <<.def "filter expression">> is the outermost level of the [[filter syntax|Filter Syntax]]. It consists of one or more [[runs|Filter Run]].

If a run has:

* no prefix, its output titles are [[dominantly appended|Dominant Append]] to the filter's output
* the prefix `-`, output titles are <<.em removed>> from the filter's output (if such tiddlers exist)
* the prefix `+`, it receives the filter output so far as its input; its output then <<.em "replaces">> all filter ouput so far and forms the input for the next run
* the prefix `~`, A filter that is used if there is no titles selected so far, we could call this an else filter.

In technical / logical terms:

|!Run |!Interpretation |!Output |
|`run` |union of sets |... OR run |
|`+run` |intersection of sets |... AND run |
|`-run` |difference of sets |... AND NOT run |
|`~run` |if no result so far | run |

A run's input is normally a list of all the non-[[shadow|ShadowTiddlers]] tiddler titles in the wiki (in no particular order). But the `+` prefix can change this:

|Prefix|Input|h
|`-` or none| <$link to="all Operator">`[all[]]`</$link> tiddler titles, unless otherwise determined by the first [[filter operator|Filter Operators]]|
|`+`|the filter output of all previous runs so far|
|`~`|A new filter output if all previous runs so far resulted in no title|

Precisely because of varying inputs, be aware that both prefixes `-` and `+` do not behave inverse to one another!

For example, in both of the following, `$:/baz` will only be removed if it actually exists:

* <$link to="is Operator"> `foo bar $:/baz -[is[system]]`</$link>
* <$link to="prefix Operator">`foo bar $:/baz -[prefix[$:/]]`</$link>

To understand why, consider the input for both final runs with their `-` prefix.

In order to remove `$:/baz` in any case, existing or not, simply use the `+` prefix with [[negated filter operators|Filter Operators]]:

* <$link to="is Operator">`foo bar $:/baz +[!is[system]]`</$link>
* <$link to="prefix Operator">`foo bar $:/baz +[!prefix[$:/]]`</$link>

Regards
Tony

Hi @AnthonyMuscio thank you! I actually pushed some doc updates yesterday that you can see here:

https://github.com/Jermolene/TiddlyWiki5/pull/3558/files

I think we've made roughly equivalent changes, have I missed anything?

Looks good

Just tested from Pre-release !

The result of this does not look correct

<<currentTiddler>><br>
<$set name=display-title filter="[all[current]get[caption]] ~[all[current]get[title]]">
<<display-title>><br>
<$link to=<<currentTiddler>> tooltip="Open tiddler"><$text text=<<display-title>>/></$link><br>

</$set>

@ibnishak ... can this issue be closed? There is a new possibility in 5.1.18

Yes. Sorry for the delay.

Was this page helpful?
0 / 5 - 0 ratings