Refactor the let filter run prefix to assign the input list to the variable named by the filter run

These semantics are much simpler, and allow the variable name to be computed.
This commit is contained in:
Jeremy Ruston
2025-03-18 15:53:28 +00:00
parent 0db188f365
commit 211b135265
5 changed files with 28 additions and 29 deletions

View File

@@ -16,27 +16,27 @@ Assign a value to a variable
Export our filter prefix function Export our filter prefix function
*/ */
exports.let = function(operationSubFunction,options) { exports.let = function(operationSubFunction,options) {
if(options.suffixes[0] && options.suffixes[0][0]) { // Return the filter run prefix function
// Save the variable name return function(results,source,widget) {
var name = options.suffixes[0][0]; // Evaluate the subfunction to get the variable name
// Return the filter run prefix function var subFunctionResults = operationSubFunction(source,widget);
return function(results,source,widget) { if(subFunctionResults.length === 0) {
// Set the input source to the incoming results return;
var inputSource = widget.wiki.makeTiddlerIterator(results.toArray()); }
// Assign the result of the subfunction to the variable var name = subFunctionResults[0];
var variables = {}; if(typeof name !== "string" || name.length === 0) {
variables[name] = operationSubFunction(inputSource,widget); return;
// Clear the results }
results.clear(); // Assign the result of the subfunction to the variable
// Return the variables var variables = {};
return { variables[name] = results.toArray()
variables: variables // Clear the results
}; results.clear();
// Return the variables
return {
variables: variables
}; };
} else { };
// Return nothing if there is no variable name
return function(results,source,widget) {};
}
}; };
})(); })();

View File

@@ -5,7 +5,7 @@ tags: [[$:/tags/wiki-test-spec]]
title: Output title: Output
<$text text={{{ :let:varname[all[tiddlers]] [varlist[varname]sort[]join[,]] }}}/> <$text text={{{ [all[tiddlers]] :let[[varname]] [varlist[varname]sort[]join[,]] }}}/>
+ +
title: ExpectedResult title: ExpectedResult

View File

@@ -5,7 +5,7 @@ tags: [[$:/tags/wiki-test-spec]]
title: Output title: Output
<$text text={{{ :let:varname[[magpie]] [<varname>] +[join[-]] }}}/> <$text text={{{ [[magpie]] :let[[varname]] [<varname>] +[join[-]] }}}/>
+ +
title: ExpectedResult title: ExpectedResult

View File

@@ -5,7 +5,7 @@ tags: [[$:/tags/wiki-test-spec]]
title: Output title: Output
<$text text={{{ [[colossus]] :let:another[all[]] [<another>] +[join[-]] }}}/> <$text text={{{ [[colossus]] :let[[another]] [<another>] +[join[-]] }}}/>
+ +
title: ExpectedResult title: ExpectedResult

View File

@@ -3,7 +3,7 @@ from-version: 5.3.7
modified: 20250307212252946 modified: 20250307212252946
rp-input: all titles from previous filter runs rp-input: all titles from previous filter runs
rp-output: an empty title list is always returned from the "let" filter run prefix rp-output: an empty title list is always returned from the "let" filter run prefix
rp-purpose: assign the results of a filter run to a variable rp-purpose: assign the title list resulting from previous filter runs to a multi-valued variable
tags: [[Named Filter Run Prefix]] tags: [[Named Filter Run Prefix]]
title: Let Filter Run Prefix title: Let Filter Run Prefix
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
@@ -11,14 +11,13 @@ type: text/vnd.tiddlywiki
<$railroad text=""" <$railroad text="""
\start none \start none
\end none \end none
( ":let" ) ( ":let" )
( ":" )
( : "variable" )
( ":" )
[[run|"Filter Run"]] [[run|"Filter Run"]]
"""/> """/>
<<.from-version "5.3.7">> The `:let` filter run prefix assigns the result list of a filter run to a variable which is made available to the remaining [[filter runs|Filter Run]] in the [[filter expression|Filter Expression]]. Only the first item in the result list is returned when the variable is accessed in the usual way (or an empty string if the result list is empty). The special [varlist Operator] is used to access all the items in the result list. The `:let` filter run prefix assigns the title list resulting from previous filter runs to a [[multi-valued variable|Multi-Valued Variable]]. The variable is named with the first result returned by the filter run.
The variable is made available to the remaining [[filter runs|Filter Run]] in the [[filter expression|Filter Expression]]. Only the first item in the result list is returned when the variable is accessed in the usual way (or an empty string if the result list is empty). The special [varlist Operator] is used to access all the items in the result list.
Within the filter run the [[all Operator]] with an empty parameter retrieves all the titles from the previous filter runs, instead of the usual behaviour of retieving all the titles that were passed to the filter expression. Within the filter run the [[all Operator]] with an empty parameter retrieves all the titles from the previous filter runs, instead of the usual behaviour of retieving all the titles that were passed to the filter expression.