1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-04-05 18:26:56 +00:00

Introduce let filter run prefix for assigning filter run result to a variable

This commit is contained in:
Jeremy Ruston 2025-03-07 21:48:36 +00:00
parent c3695765ad
commit b8bdb0aeeb
6 changed files with 92 additions and 1 deletions

View File

@ -0,0 +1,37 @@
/*\
title: $:/core/modules/filterrunprefixes/let.js
type: application/javascript
module-type: filterrunprefix
Assign a value to a variable
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter prefix function
*/
exports.let = function(operationSubFunction,options) {
// Save the variable name
var name = options.suffixes[0][0];
// Return the filter run prefix function
return function(results,source,widget) {
// Set the input source to the incoming results
var inputSource = widget.wiki.makeTiddlerIterator(results.toArray());
// Assign the result of the subfunction to the variable
var variables = {};
variables[name] = operationSubFunction(inputSource,widget)[0] || "";
// Clear the results
results.clear();
// Return the variables
return {
variables: variables
};
};
};
})();

View File

@ -348,7 +348,13 @@ exports.compileFilter = function(filterString) {
self.filterRecursionCount = (self.filterRecursionCount || 0) + 1;
if(self.filterRecursionCount < MAX_FILTER_DEPTH) {
$tw.utils.each(operationFunctions,function(operationFunction) {
operationFunction(results,source,widget);
var operationResult = operationFunction(results,source,widget);
if(operationResult) {
if(operationResult.variables) {
// If the filter run prefix has returned variables, create a new fake widget with those variables
widget = widget.makeFakeWidgetWithVariables(operationResult.variables);
}
}
});
} else {
results.push("/**-- Excessive filter recursion --**/");

View File

@ -0,0 +1,12 @@
title: LetFilterRunPrefix/All
description: Usage of "all[]" operator within "let" filter run prefix
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
<$text text={{{ [[colossus]] :let:another[all[]] [<another>] +[join[-]] }}}/>
+
title: ExpectedResult
<p>colossus</p>

View File

@ -0,0 +1,12 @@
title: LetFilterRunPrefix/Simple
description: Simple usage of "let" filter run prefix
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
<$text text={{{ :let:varname[[magpie]] [<varname>] +[join[-]] }}}/>
+
title: ExpectedResult
<p>magpie</p>

View File

@ -0,0 +1,23 @@
created: 20250307212252946
from-version: 5.3.7
modified: 20250307212252946
rp-input: all titles from previous filter runs
rp-output: an empty title list is always returned from the "let" filter run prefix
rp-purpose: assign the result of a filter run to a variable
tags: [[Named Filter Run Prefix]]
title: Let Filter Run Prefix
type: text/vnd.tiddlywiki
<$railroad text="""
\start none
\end none
( ":let" )
( ":" )
( : "variable" )
( ":" )
[[run|"Filter Run"]]
"""/>
The `:let` filter run prefix assigns the first result of a filter run to a variable that is made available to the remaining [[filter runs|Filter Run]] in the [[filter expression|Filter Expression]]. If the filter run does not return any results then the variable is set to an empty string.
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.

View File

@ -23,6 +23,7 @@ A named filter run prefix can precede any [[run|Filter Run]] of a [[filter expre
[[<":or"> |"Or Filter Run Prefix"]] |
[[<":reduce"> |"Reduce Filter Run Prefix"]] |
[[<":sort"> /"v5.2.0"/ |"Sort Filter Run Prefix"]] |
[[<":let"> /"v5.3.7"/ |"Let Filter Run Prefix"]] |
[[<":then"> /"v5.3.0"/ |"Then Filter Run Prefix"]]) [[run|"Filter Run"]]
"""/>