Wednesday, December 28, 2011

jquery reverse order of children

Reverse the order of children:

(function($){
 $.fn.reverse = function(){  
  return this.each(function(){
   var i,
    $this = $(this),
    children = $this.children(),
    last_index = children.length - 1,
    last_child = $(children[last_index]);
   
   for (i = 0; i < last_index; ++i)
   {
    last_child.after($(children[i]).detach());
   };
  });
 };
})(jQuery);

Thursday, December 22, 2011

download jquery

Once or twice a month I'll find myself working on some random little project with a web UI and so of course I need jquery at which point I wonder if I have the latest version.. hence, a little script to get the latest and greatest:

#!/bin/bash

wget $(\
 wget -qO - http://docs.jquery.com/Downloading_jQuery |\
 sed -rn 's#^.*(http://code.jquery.com/jquery-[[:digit:]]+.[[:digit:]]+.[[:digit:]]+.min.js).*$#\1#p' |\
 head -n 1
 )

todo: check local jquery copy to see if there actually is a new version we need to download

Monday, December 5, 2011

string find and replace in files

The -i option to sed does in place file editing, however it "modifies" each file it touches whether it actually does any replacing or not (so the file mod time is updated). By filtering through grep first, we can be sure we are only messing with files we actually want to. Replace "happy" with "blammo":
find . -type f -print0 | xargs -0 grep -lZ "happy" | xargs -0 sed -i 's/happy/blammo/g'
The -l option to grep prints file names only and stops with the first match (so no duplicates). -Z uses null byte for separator instead of newline.