Created
February 11, 2010 22:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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