Filter allowing for-loop type iterations

46 views
Skip to first unread message

David Szego

unread,
Feb 12, 2017, 12:01:56 PM2/12/17
to tiddly...@googlegroups.com
Hi all,

I whipped up a filter that returns a progressive count up to the number specified. No actual Tiddlers returned, just numbers.

This allows an iterative loop of anything within a list, a set number of times. For instance:

<$list filter="[numberOfItems[5]]">
  <$button>Button #<
<currentTiddler>>
    <$action-dosomething ... />
  </$button>
</$list>

would give you 5 buttons [1] [2] [3] [4] [5] with their associated actions.

<$list filter="[numberOfItems[3]]">
  <$action-sendmessage $message="tm-new-tiddler" title=<
<currentTiddler>>/>
</$list>

would create 3 new Tiddlers, "1", "2", and "3".

There's minimal error checking right now. If you say numberOfItems[0] it will return nothing, rather than 1 result called "0". It counts properly from 1 rather than from 0. That's about it!!

I'd be happy to have someone expand on this. If someone's already done it, I'd love to see - I've missed it.

Code:

exports.numberOfItems = function(source,operator,options) {
   
var results = [];
   
//source(function(tiddler,title) {
     
if (operator.operand != "" && operator.operand !="0") {
       
var operandCount;
       
for (operandCount = 0; operandCount < parseInt(operator.operand, 10); operandCount++) {
          results
.push(String(operandCount+1));
       
}
     
}      
   
//});
   
return results;
};

JSON Tiddler attached.

Cheers,
David.


filter-numberOfItems.json

PMario

unread,
Feb 12, 2017, 3:46:02 PM2/12/17
to TiddlyWikiDev
Hi David,

You should still / always use parsInt(x,10) since older browsers may interpret 10 as 8 using octal. see: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseInt

-m

David Szego

unread,
Feb 12, 2017, 3:51:18 PM2/12/17
to TiddlyWikiDev
Good call, in fact I did figure that one out after posting! =-) Example edited. Code reattached. Thanks!
filter-numberOfItems.json
Reply all
Reply to author
Forward
0 new messages