Imagine the search-replace filter operator could operate like the javascript function of the same name. That is, it would now be able to call code for produciong the replacement!
I have an example ready (which I currently solved with a custon javascript filter operator).
be that filter:
[[one two*2 three four*4]search-replace:g:regexp[/\b(\w+)\*(\d+)\b/],[.expand]]
it would produce “one two two three four four four four”! with the following function:
\function .expand(matched, word, count) [<word>repeat<count>]
This is totally the same logic than in javascript.
Oh, I nearly forgot: “repeat” is not yet available as a standard filter op, but IMHO it really should be. As you have guessed, it is just duplicating the input by the indicated number. Not duplicating as new inputs, but by modifying each input titles…
Thus [[a]repeat[3]]
is equal to [[aaa]]
not to [[a] [a] [a]]
.
Here it is.
/*\
title: $:/user/common/filters/repeat.js
type: application/javascript
module-type: filteroperator
repeat the operand as many times as the value of the operand.
If the operand is negative, equivalent to zero or empty, returns an empty string.
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: true */
"use strict";
exports.repeat = function(source, operator, options) {
let factor = (operator.operand === "") ? 0 : parseInt(operator.operand, 10);
if (isNaN(factor) || factor < 0) { factor = 0; }
let tids = [];
source(function(tiddler, title) {
if (title) {
tids.push(title.repeat(factor));
}
});
return tids;
};
})();