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

Add support for = prefix for filter runs that doesn't remove duplicates

This commit is contained in:
Jeremy Ruston 2019-06-10 17:54:20 +01:00
parent ac1d5b828d
commit 37bb75f0cf
3 changed files with 16 additions and 6 deletions

View File

@ -119,7 +119,7 @@ exports.parseFilter = function(filterString) {
p = 0, // Current position in the filter string p = 0, // Current position in the filter string
match; match;
var whitespaceRegExp = /(\s+)/mg, var whitespaceRegExp = /(\s+)/mg,
operandRegExp = /((?:\+|\-|~)?)(?:(\[)|(?:"([^"]*)")|(?:'([^']*)')|([^\s\[\]]+))/mg; operandRegExp = /((?:\+|\-|~|=)?)(?:(\[)|(?:"([^"]*)")|(?:'([^']*)')|([^\s\[\]]+))/mg;
while(p < filterString.length) { while(p < filterString.length) {
// Skip any whitespace // Skip any whitespace
whitespaceRegExp.lastIndex = p; whitespaceRegExp.lastIndex = p;
@ -248,6 +248,10 @@ exports.compileFilter = function(filterString) {
return function(results,source,widget) { return function(results,source,widget) {
$tw.utils.pushTop(results,operationSubFunction(source,widget)); $tw.utils.pushTop(results,operationSubFunction(source,widget));
}; };
case "=": // The results of the operation are pushed into the result without deduplication
return function(results,source,widget) {
Array.prototype.push.apply(results,operationSubFunction(source,widget));
};
case "-": // The results of this operation are removed from the main result case "-": // The results of this operation are removed from the main result
return function(results,source,widget) { return function(results,source,widget) {
$tw.utils.removeArrayEntries(results,operationSubFunction(source,widget)); $tw.utils.removeArrayEntries(results,operationSubFunction(source,widget));

View File

@ -1,5 +1,5 @@
created: 20150123220223000 created: 20150123220223000
modified: 20150917193646675 modified: 20190610165255223
tags: Filters tags: Filters
title: Dominant Append title: Dominant Append
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
@ -9,3 +9,7 @@ type: text/vnd.tiddlywiki
This is done in such a way that, if a title would be duplicated, the earlier copy of that title is discarded. The titles being appended are dominant. This is done in such a way that, if a title would be duplicated, the earlier copy of that title is discarded. The titles being appended are dominant.
For example, if a selection contains `Andrew Becky Clara Daniel` and `Andrew Barney Clara Daisy` is then appended to it, the result is `Becky Daniel Andrew Barney Clara Daisy`. For example, if a selection contains `Andrew Becky Clara Daniel` and `Andrew Barney Clara Daisy` is then appended to it, the result is `Becky Daniel Andrew Barney Clara Daisy`.
This behaviour can cause unexpected results when working with [[Mathematics Operators]]. For example, `1 2 3 +[sum[]]` evaluates to `6`, as expected. But `1 1 1 +[sum[]]` evaluates to `1`. Removing the `+[sum[]]` from each filter reveals the problem: `1 2 3` evaluates to the list `1`, `2`, `3`, while `1 1 1` evaluates to the single item `1` due to de-duplication.
In such situations, the `=` prefix can be used to disable the de-duplication. For example, `=1 =1 =1 +[sum[]]` evaluates to `3` as expected. Alternatively, the [[split Operator]] can be used: `[[1,1,1]split[,]sum[]]`.

View File

@ -1,5 +1,5 @@
created: 20150124182421000 created: 20150124182421000
modified: 20181120125803533 modified: 20190610165329062
tags: [[Filter Syntax]] tags: [[Filter Syntax]]
title: Filter Expression title: Filter Expression
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
@ -7,7 +7,7 @@ type: text/vnd.tiddlywiki
<$railroad text=""" <$railroad text="""
[{: [{:
[: [[whitespace|"Filter Whitespace"]] ] [: [[whitespace|"Filter Whitespace"]] ]
("+"|"~"|:-|"-") ("+"|"~"|:-|"-"|"=")
[[run|"Filter Run"]] [[run|"Filter Run"]]
}] }]
"""/> """/>
@ -17,6 +17,7 @@ A <<.def "filter expression">> is the outermost level of the [[filter syntax|Fil
If a run has: If a run has:
* no prefix, its output titles are [[dominantly appended|Dominant Append]] to the filter's output * no prefix, its output titles are [[dominantly appended|Dominant Append]] to the filter's output
* <<.from-version "5.1.20">> the prefix `=`, output titles are appended to the filter's output without de-duplication
* the prefix `-`, output titles are <<.em removed>> from the filter's output (if such tiddlers exist) * 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 `+`, 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
* <<.from-version "5.1.18">> the prefix `~`, if the filter output so far is an empty list then the output titles of the run are [[dominantly appended|Dominant Append]] to the filter's output. If the filter output so far is not an empty list then the run is ignored * <<.from-version "5.1.18">> the prefix `~`, if the filter output so far is an empty list then the output titles of the run are [[dominantly appended|Dominant Append]] to the filter's output. If the filter output so far is not an empty list then the run is ignored
@ -24,7 +25,8 @@ If a run has:
In technical / logical terms: In technical / logical terms:
|!Run |!Interpretation |!Output | |!Run |!Interpretation |!Output |
|`run` |union of sets |... OR run | |`run` |de-duplicated union of sets |... OR run |
|`=run` |union of sets without de-duplication |... OR run |
|`+run` |intersection of sets |... AND run | |`+run` |intersection of sets |... AND run |
|`-run` |difference of sets |... AND NOT run | |`-run` |difference of sets |... AND NOT run |
|`~run` |else |... ELSE run | |`~run` |else |... ELSE run |
@ -32,7 +34,7 @@ In technical / logical terms:
The input of a run 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: The input of a run 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 |Prefix|Input|h
|`-`, `~` or none| <$link to="all Operator">`[all[]]`</$link> tiddler titles, unless otherwise determined by the first [[filter operator|Filter Operators]]| |`-`, `~`, `=` 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| |`+`|the filter output of all previous runs so far|
Precisely because of varying inputs, be aware that both prefixes `-` and `+` do not behave inverse to one another! Precisely because of varying inputs, be aware that both prefixes `-` and `+` do not behave inverse to one another!