Cleanest filter for "if tid has X then do Y otherwise just PASS IT ON"

Most(?) filter ops test for someting and if the item doesn’t fulfill it, then it is not passed on.

I want to, say, remove a prefix X but if the item doesn’t have that prefix then just accept it as it is and pass it on.

I can achieve this, I just want to know what the simplest way is.

Actually… wouldn’t it make sense with some general operator suffix to make the relevant operators less “all-or-nothing”? I would think the common denominator for the relevant filter operators are that they modify the input, and what I’m after is that they pass on the input unchanged if they fail to modify it, rather than to pass on nothing.

Thanx

Does this work for you?

:map[removeprefix[X]else<currentTiddler>]
1 Like

Thanx @Mohammad but that not what I’m hoping for. I’m hoping something more direct, within the same run, is possible.

(I’m forlulating another thread to illustrate the background for my question - see Find all stylesheet tids that are NOT tagged such

1 Like

For the above logic, the shortest filter would be to use the trim:prefix[X] operator, which does what you describe. One caveat: if the specified prefix value is repeated, then trim[...] will remove all repeated instances, like this:

[[XXXfoo]trim:prefix[X]]

results in just foo, rather than XXfoo.

To limit the matching to just the first instance of the target text, you could use someting like this instead:

[[XXXfoo]search-replace:g:regexp[^X],[]]

which will result in XXfoo, but will also pass through any input that doesn’t have the target text as a prefix, e.g.:

[[AXXXfoo]search-replace:g:regexp[^X],[]]

will result in AXXXfoo, unchanged.

Dang, thank you!.. @EricShulman , I gotto ask; Do you know this by heart, or do you know exactly what to look up - and then just do it super fast?
Impressive.

1 Like

I generally know this stuff, but I usually check the documentation and test my answers on TiddlyWiki.com, just to make sure my answers are accurate.

2 Likes

Just for completeness see How to Remove N repeating, leading or trailing characters/strings for another approach that can be used, using the spilt and join on a given character or string.