butlast filter operator

84 views
Skip to first unread message

History Buff

unread,
Jun 27, 2021, 2:28:50 PM6/27/21
to TiddlyWiki
All,

I have a general question about the butlast filter operator. I had a filter where I had the butlast operator as the last item with a variable for the parameter. All worked fine until the variable was 0.  I expected that it wouldn't remove any of the last items, but what I got was no results at all. I fixed it be making sure that the variable was never 0, but I don't understand why a 0 in the butlast operator would yield no results. Can anyone explain that to me? 

Thanks so much!

Eric Shulman

unread,
Jun 27, 2021, 3:07:27 PM6/27/21
to TiddlyWiki
On Sunday, June 27, 2021 at 11:28:50 AM UTC-7 History Buff wrote:
I don't understand why a 0 in the butlast operator would yield no results

The butlast[] operator is defined in this shadow tiddler:

Here's the javascript code from that tiddler:
exports.butlast = function(source,operator,options) {
var count = $tw.utils.getInt(operator.operand,1),
results = [];
source(function(tiddler,title) {
results.push(title);
});
return results.slice(0,-count);
};

The first line gets the operand value, defaulting to 1.
The next 4 lines copy the entire list of items into a results array
The last line uses the javascript slice() function to remove items from the results array and then return that array

Here's the documentation for the javascript slice() function:

Note the description of the "end" parameter:
Zero-based index before which to end extraction. slice extracts up to but not including end.

Thus, when you used butlast[0], the filter code invokes slice(0,-0),
i.e., "starting from the first item up to but not including the first item"
which results in no items being returned.

enjoy,
-e

Damon Pritchett

unread,
Jun 27, 2021, 11:07:37 PM6/27/21
to tiddl...@googlegroups.com
Thanks Eric,

I appreciate that. I looked at it, but I really don’t know JavaScript. Makes me want to learn though. 



--
You received this message because you are subscribed to a topic in the Google Groups "TiddlyWiki" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tiddlywiki/lROJc--2xFI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/c1f40a73-746d-4c39-89b9-6786c9caee0fn%40googlegroups.com.

TW Tones

unread,
Jun 28, 2021, 10:34:46 AM6/28/21
to TiddlyWiki
Another way to look at it ;  
Butlast purpose discard the last N input title(s)

So saying  "discard the last 0 input titles" is non sensical 
If you actually want to discard 3, then 2 then 1 perhaps you filter needs find another way once it reaches 0

Tones

Damon Pritchett

unread,
Jun 28, 2021, 10:46:49 AM6/28/21
to tiddl...@googlegroups.com
Hey Tony,

Yeah that’s what I eventually realized so I changed what happens when I reach zero. I was just wanting to understand the inner workings better. 

Saq Imtiaz

unread,
Jun 28, 2021, 1:45:52 PM6/28/21
to TiddlyWiki
This is essentially an oversight and will be fixed. Thank you for bringing this up.

History Buff

unread,
Jun 28, 2021, 2:18:55 PM6/28/21
to TiddlyWiki
Thanks!
Reply all
Reply to author
Forward
0 new messages