Skip to content

Instantly share code, notes, and snippets.

@arieh
Created February 11, 2010 22:59
Show Gist options
  • Save arieh/302080 to your computer and use it in GitHub Desktop.
Save arieh/302080 to your computer and use it in GitHub Desktop.
A Javascript/Mootools method that implements a generic count method for all variable types
function $count(obj){
var childs = [];
if (!obj) return 0;
switch($type(obj)){
case 'number' : return obj;
case 'function':
return ((obj+"").test(/[{]([\W]*[\w]+[\W\w]*)[}]/)) ? 1: 0;
case 'object' : return $H(obj).getLength();
case 'window' :
case 'document':
case 'element' :
return obj.getChildren().length;
}
return (obj.length) ?
obj.length :
($type(+obj)) ? +obj : 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment