From 611adadaed9e09adfc5e00dd0e51e851135227d0 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Wed, 12 Feb 2025 10:04:46 +0000 Subject: [PATCH] Add apply filter run prefix The map filter run prefix is often used as a way to move a computed value in the input list into a variable so that it can be used as a parameter of a filter operator. The apply filter run prefix extends this idea to make the input list available as variables $1, $2 etc. Unlike the map prefix, the apply filter run is only evaluated once. --- core/modules/filterrunprefixes/apply.js | 29 +++++++++++++++++++ .../data/filters/FilterRunPrefixesApply.tid | 13 +++++++++ 2 files changed, 42 insertions(+) create mode 100644 core/modules/filterrunprefixes/apply.js create mode 100644 editions/test/tiddlers/tests/data/filters/FilterRunPrefixesApply.tid diff --git a/core/modules/filterrunprefixes/apply.js b/core/modules/filterrunprefixes/apply.js new file mode 100644 index 000000000..7673e7729 --- /dev/null +++ b/core/modules/filterrunprefixes/apply.js @@ -0,0 +1,29 @@ +/*\ +title: $:/core/modules/filterrunprefixes/apply.js +type: application/javascript +module-type: filterrunprefix + + + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.apply = function(operationSubFunction) { + return function(results,source,widget) { + source = widget.wiki.makeTiddlerIterator([]); + var variables = {}, + counter = 1; + results.each(function(title) { + variables["$" + counter] = title; + counter++; + }); + results.clear(); + results.pushTop(operationSubFunction(source,widget.makeFakeWidgetWithVariables(variables))); + }; +}; + +})(); diff --git a/editions/test/tiddlers/tests/data/filters/FilterRunPrefixesApply.tid b/editions/test/tiddlers/tests/data/filters/FilterRunPrefixesApply.tid new file mode 100644 index 000000000..5ed5a728e --- /dev/null +++ b/editions/test/tiddlers/tests/data/filters/FilterRunPrefixesApply.tid @@ -0,0 +1,13 @@ +title: Filters/FilterRunPrefixes/Applu +description: Applu filter run prefix +type: text/vnd.tiddlywiki-multiple +tags: [[$:/tags/wiki-test-spec]] + +title: Output + +\whitespace trim +<$text text={{{ a b c :apply[<$1>addsuffix<$2>addsuffix<$3>] }}}/> ++ +title: ExpectedResult + +

abc

\ No newline at end of file