cjc/littering

Pass not just the current item but the list of all items

Closed this issue · 3 comments

gka commented

It would be nice if the customize function would pass not just the current item but the list of all items. You're example would look like this:

$("#demo7 h1").littering('chars',function(items,i){
  var str = items[i].charCodeAt(0) > 64 && items[i].charCodeAt(0) < 91 ? " upper" : "" ;
  return '<span class="char'+(i+1)+str+'">'+items[i]+'</span>';
});

The hard-core use case for this is to implement a simple kerning table. Say you have a font where you need less spacing between every pair of "AT". This would add the class kern_AT to every T that follows an A.

$('h1').littering('chars', function(items, i) {
  var str = i > 0 && items[i-1] == "A" && items[i] == "T" ? " kern_AT" : "";
  return '<span class="char'+(i+1)+str+'">'+items[i]+'</span>';
});
cjc commented

I like it.

To keep backwards compatibility, might implement it as a third parameter, like

$('h1').littering('chars', function(item, i, items) {

so code that only needs the current item can stay as is and just ignore the extra param.

cjc commented

Doneskies. NPM now has version 0.0.4 with the change, puny example at http://cjc.github.com/littering/.

gka commented

thx!