mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-30 13:29:56 +00:00
CurrentTiddler variable consistency in subfilters and prefixes (#5691)
* Make currentTiddler variable consistent in subfilters and filter run prefixes * Updated filterun prefix and subfilter operators to use ..currentTiddler instead of outerCurrentTiddler
This commit is contained in:
parent
bf25c4d34a
commit
3fc7895af2
@ -17,10 +17,21 @@ exports.filter = function(operationSubFunction,options) {
|
|||||||
return function(results,source,widget) {
|
return function(results,source,widget) {
|
||||||
if(results.length > 0) {
|
if(results.length > 0) {
|
||||||
var resultsToRemove = [];
|
var resultsToRemove = [];
|
||||||
results.each(function(result) {
|
results.each(function(title) {
|
||||||
var filtered = operationSubFunction(options.wiki.makeTiddlerIterator([result]),widget);
|
var filtered = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
|
||||||
|
getVariable: function(name) {
|
||||||
|
switch(name) {
|
||||||
|
case "currentTiddler":
|
||||||
|
return "" + title;
|
||||||
|
case "..currentTiddler":
|
||||||
|
return widget.getVariable("currentTiddler");
|
||||||
|
default:
|
||||||
|
return widget.getVariable(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
if(filtered.length === 0) {
|
if(filtered.length === 0) {
|
||||||
resultsToRemove.push(result);
|
resultsToRemove.push(title);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
results.remove(resultsToRemove);
|
results.remove(resultsToRemove);
|
||||||
|
@ -19,23 +19,25 @@ exports.reduce = function(operationSubFunction,options) {
|
|||||||
var index = 0;
|
var index = 0;
|
||||||
results.each(function(title) {
|
results.each(function(title) {
|
||||||
var list = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
|
var list = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
|
||||||
getVariable: function(name) {
|
getVariable: function(name) {
|
||||||
switch(name) {
|
switch(name) {
|
||||||
case "currentTiddler":
|
case "currentTiddler":
|
||||||
return "" + title;
|
return "" + title;
|
||||||
case "accumulator":
|
case "..currentTiddler":
|
||||||
return "" + accumulator;
|
return widget.getVariable("currentTiddler");
|
||||||
case "index":
|
case "accumulator":
|
||||||
return "" + index;
|
return "" + accumulator;
|
||||||
case "revIndex":
|
case "index":
|
||||||
return "" + (results.length - 1 - index);
|
return "" + index;
|
||||||
case "length":
|
case "revIndex":
|
||||||
return "" + results.length;
|
return "" + (results.length - 1 - index);
|
||||||
default:
|
case "length":
|
||||||
return widget.getVariable(name);
|
return "" + results.length;
|
||||||
}
|
default:
|
||||||
|
return widget.getVariable(name);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
});
|
||||||
if(list.length > 0) {
|
if(list.length > 0) {
|
||||||
accumulator = "" + list[0];
|
accumulator = "" + list[0];
|
||||||
}
|
}
|
||||||
|
@ -26,15 +26,17 @@ exports.sort = function(operationSubFunction,options) {
|
|||||||
compareFn;
|
compareFn;
|
||||||
results.each(function(title) {
|
results.each(function(title) {
|
||||||
var key = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
|
var key = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
|
||||||
getVariable: function(name) {
|
getVariable: function(name) {
|
||||||
switch(name) {
|
switch(name) {
|
||||||
case "currentTiddler":
|
case "currentTiddler":
|
||||||
return "" + title;
|
return "" + title;
|
||||||
default:
|
case "..currentTiddler":
|
||||||
return widget.getVariable(name);
|
return widget.getVariable("currentTiddler");
|
||||||
}
|
default:
|
||||||
|
return widget.getVariable(name);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
});
|
||||||
sortKeys.push(key[0] || "");
|
sortKeys.push(key[0] || "");
|
||||||
});
|
});
|
||||||
results.clear();
|
results.clear();
|
||||||
|
@ -20,7 +20,18 @@ exports.filter = function(source,operator,options) {
|
|||||||
results = [],
|
results = [],
|
||||||
target = operator.prefix !== "!";
|
target = operator.prefix !== "!";
|
||||||
source(function(tiddler,title) {
|
source(function(tiddler,title) {
|
||||||
var list = filterFn.call(options.wiki,options.wiki.makeTiddlerIterator([title]),options.widget);
|
var list = filterFn.call(options.wiki,options.wiki.makeTiddlerIterator([title]),{
|
||||||
|
getVariable: function(name) {
|
||||||
|
switch(name) {
|
||||||
|
case "currentTiddler":
|
||||||
|
return "" + title;
|
||||||
|
case "..currentTiddler":
|
||||||
|
return options.widget.getVariable("currentTiddler");
|
||||||
|
default:
|
||||||
|
return options.widget.getVariable(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
if((list.length > 0) === target) {
|
if((list.length > 0) === target) {
|
||||||
results.push(title);
|
results.push(title);
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,8 @@ exports.reduce = function(source,operator,options) {
|
|||||||
switch(name) {
|
switch(name) {
|
||||||
case "currentTiddler":
|
case "currentTiddler":
|
||||||
return "" + title;
|
return "" + title;
|
||||||
|
case "..currentTiddler":
|
||||||
|
return options.widget.getVariable("currentTiddler");
|
||||||
case "accumulator":
|
case "accumulator":
|
||||||
return "" + accumulator;
|
return "" + accumulator;
|
||||||
case "index":
|
case "index":
|
||||||
|
@ -27,10 +27,13 @@ exports.sortsub = function(source,operator,options) {
|
|||||||
iterator(options.wiki.getTiddler(title),title);
|
iterator(options.wiki.getTiddler(title),title);
|
||||||
},{
|
},{
|
||||||
getVariable: function(name) {
|
getVariable: function(name) {
|
||||||
if(name === "currentTiddler") {
|
switch(name) {
|
||||||
return title;
|
case "currentTiddler":
|
||||||
} else {
|
return "" + title;
|
||||||
return options.widget.getVariable(name);
|
case "..currentTiddler":
|
||||||
|
return options.widget.getVariable("currentTiddler");
|
||||||
|
default:
|
||||||
|
return options.widget.getVariable(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
caption: filter
|
caption: filter
|
||||||
created: 20200929174420821
|
created: 20200929174420821
|
||||||
modified: 20201027185144953
|
modified: 20210522162551921
|
||||||
op-input: a [[selection of titles|Title Selection]] passed as input to the filter
|
op-input: a [[selection of titles|Title Selection]] passed as input to the filter
|
||||||
op-neg-input: a [[selection of titles|Title Selection]] passed as input to the filter
|
op-neg-input: a [[selection of titles|Title Selection]] passed as input to the filter
|
||||||
op-neg-output: those input titles that <<.em "do not">> pass the filter <<.place S>>
|
op-neg-output: those input titles that <<.em "do not">> pass the filter <<.place S>>
|
||||||
@ -28,6 +28,8 @@ Simple filter operations can be concatenated together directly (eg `[tag[HelloTh
|
|||||||
</$vars>
|
</$vars>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Note that within the subfilter, the "currentTiddler" variable is set to the title of the tiddler being processed. The value of currentTiddler outside the subfilter is available in the variable "..currentTiddler". <<.from-version "5.1.24">>
|
||||||
|
|
||||||
<<.tip "Compare with the similar [[subfilter|subfilter Operator]] operator which runs a subfilter and directly returns the results">>
|
<<.tip "Compare with the similar [[subfilter|subfilter Operator]] operator which runs a subfilter and directly returns the results">>
|
||||||
|
|
||||||
<<.tip "Compare with the analagous named filter run prefix `:filter`">>
|
<<.tip "Compare with the analagous named filter run prefix `:filter`">>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
caption: reduce
|
caption: reduce
|
||||||
created: 20201004154131193
|
created: 20201004154131193
|
||||||
modified: 20201208185109549
|
modified: 20210522162536854
|
||||||
op-input: a [[selection of titles|Title Selection]] passed as input to the filter
|
op-input: a [[selection of titles|Title Selection]] passed as input to the filter
|
||||||
op-output: the final result of running the subfilter <<.place S>>
|
op-output: the final result of running the subfilter <<.place S>>
|
||||||
op-parameter: a [[filter expression|Filter Expression]]. Optional second parameter for initial value for accumulator
|
op-parameter: a [[filter expression|Filter Expression]]. Optional second parameter for initial value for accumulator
|
||||||
@ -18,6 +18,7 @@ The following variables are available within the subfilter:
|
|||||||
|
|
||||||
* ''accumulator'' - the result of the previous subfilter run
|
* ''accumulator'' - the result of the previous subfilter run
|
||||||
* ''currentTiddler'' - the input title
|
* ''currentTiddler'' - the input title
|
||||||
|
* ''..currentTiddler'' - the value of the variable `currentTiddler` outside the subfilter. <<.from-version "5.1.24">>
|
||||||
* ''index'' - the numeric index of the current list item (with zero being the first item in the list)
|
* ''index'' - the numeric index of the current list item (with zero being the first item in the list)
|
||||||
* ''revIndex'' - the reverse numeric index of the current list item (with zero being the last item in the list)
|
* ''revIndex'' - the reverse numeric index of the current list item (with zero being the last item in the list)
|
||||||
* ''length'' - the total length of the input list
|
* ''length'' - the total length of the input list
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
caption: sortsub
|
caption: sortsub
|
||||||
created: 20200424160155182
|
created: 20200424160155182
|
||||||
modified: 20210428152533501
|
modified: 20210522162521222
|
||||||
op-input: a [[selection of titles|Title Selection]]
|
op-input: a [[selection of titles|Title Selection]]
|
||||||
op-neg-output: the input, sorted into reverse order by the result of evaluating subfilter <<.param S>>
|
op-neg-output: the input, sorted into reverse order by the result of evaluating subfilter <<.param S>>
|
||||||
op-output: the input, sorted into ascending order by the result of evaluating subfilter <<.param S>>
|
op-output: the input, sorted into ascending order by the result of evaluating subfilter <<.param S>>
|
||||||
@ -15,7 +15,9 @@ type: text/vnd.tiddlywiki
|
|||||||
|
|
||||||
Each item in the list of input titles is passed to the subfilter in turn. The subfilter transforms the input titles into the form needed for sorting. For example, the subfilter `[length[]]` transforms each input title in the number representing its length, and thus sorts the input titles according to their length.
|
Each item in the list of input titles is passed to the subfilter in turn. The subfilter transforms the input titles into the form needed for sorting. For example, the subfilter `[length[]]` transforms each input title in the number representing its length, and thus sorts the input titles according to their length.
|
||||||
|
|
||||||
Note that within the subfilter, the "currentTiddler" variable is set to the title of the tiddler being processed. This permits subfilters like `[{!!value}divide{!!cost}]` to be used for computation.
|
Note that within the subfilter, the "currentTiddler" variable is set to the title of the tiddler being processed. This permits subfilters like `[{!!value}divide{!!cost}]` to be used for computation.
|
||||||
|
|
||||||
|
The value of currentTiddler outside the subfilter is available in the variable "..currentTiddler". <<.from-version "5.1.24">>
|
||||||
|
|
||||||
The suffix <<.place T>> determines how the items are compared and can be:
|
The suffix <<.place T>> determines how the items are compared and can be:
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
created: 20150124182421000
|
created: 20150124182421000
|
||||||
modified: 20210428084144231
|
modified: 20210522162642994
|
||||||
tags: [[Filter Syntax]]
|
tags: [[Filter Syntax]]
|
||||||
title: Filter Expression
|
title: Filter Expression
|
||||||
type: text/vnd.tiddlywiki
|
type: text/vnd.tiddlywiki
|
||||||
@ -33,6 +33,8 @@ If a run has:
|
|||||||
|
|
||||||
<<.tip "Compare named filter run prefix `:reduce` with [[reduce Operator]] which is used to used to flatten a list of items down to a single item by repeatedly applying a subfilter.">>
|
<<.tip "Compare named filter run prefix `:reduce` with [[reduce Operator]] which is used to used to flatten a list of items down to a single item by repeatedly applying a subfilter.">>
|
||||||
|
|
||||||
|
<<.tip """Within the filter runs prefixed with `:reduce`, `:sort` and `:filter`, the "currentTiddler" variable is set to the title of the tiddler being processed. The value of currentTiddler outside the subfilter is available in the variable "..currentTiddler".<<.from-version "5.1.24">>""" >>
|
||||||
|
|
||||||
In technical / logical terms:
|
In technical / logical terms:
|
||||||
|
|
||||||
|!Run |!Equivalent named prefix |!Interpretation |!Output |
|
|!Run |!Equivalent named prefix |!Interpretation |!Output |
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
created: 20210428083929749
|
created: 20210428083929749
|
||||||
modified: 20210428140713422
|
modified: 20210522162628946
|
||||||
tags: [[Filter Syntax]] [[Filter Run Prefix]]
|
tags: [[Filter Syntax]] [[Filter Run Prefix]]
|
||||||
title: Sort Filter Run Prefix
|
title: Sort Filter Run Prefix
|
||||||
type: text/vnd.tiddlywiki
|
type: text/vnd.tiddlywiki
|
||||||
@ -13,7 +13,7 @@ type: text/vnd.tiddlywiki
|
|||||||
|
|
||||||
Each input title from previous runs is passed to this run in turn. The filter run transforms the input titles into the form needed for sorting. For example, the filter run `[length[]]` transforms each input title in to the number representing its length, and thus sorts the input titles according to their length.
|
Each input title from previous runs is passed to this run in turn. The filter run transforms the input titles into the form needed for sorting. For example, the filter run `[length[]]` transforms each input title in to the number representing its length, and thus sorts the input titles according to their length.
|
||||||
|
|
||||||
Note that within the filter run, the "currentTiddler" variable is set to the title of the tiddler being processed. This permits filter runs like `:sort:number[{!!value}divide{!!cost}]` to be used for computation.
|
Note that within the filter run, the "currentTiddler" variable is set to the title of the tiddler being processed. This permits filter runs like `:sort:number[{!!value}divide{!!cost}]` to be used for computation. The value of currentTiddler outside the run is available in the variable "..currentTiddler".
|
||||||
|
|
||||||
The `:sort` filter run prefix uses an extended syntax that allows for multiple suffixes, some of which are required:
|
The `:sort` filter run prefix uses an extended syntax that allows for multiple suffixes, some of which are required:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user