$count 1.2.2

A generic method to check the length of any type of variable. It will return a numeric representation of any JS type.



Details

Author
Arieh Glazer
Current version
1.2.2
GitHub
arieh/mt-count
Downloads
9986
Category
Utilities
Tags
Report
GitHub Issues

Releases


Dependencies

  • _self_/_current_: core/1.2.4 : [Core]

How to use

The function wll return a length for any variable that has a distinguishable length (Array,Object,String,Number). It will also return a length for these types:

  • Window, Document, Element: will return the number of child elements.
  • Function : Will return 0 for empty functions, 1 for others.
  • 'Falsy' variables (null,NaN,undefined) : Will return 0.
  • For all others, the function will first check for a length property. If it can't, it will try to enumarate the value. If successfull will return the number. For all else will return 1.

This reasoning behind this is to allow the method to distinguish between empty and non-empty variables.

$count('abc'); //3
$count({'a':'b','c':'d'}); //2
$count([1,2,3]); //3
$count(2); //2
$count(function(){}); //0
$count(function(){return 'a';});//1

A more useful (though very generic) use-case:

if ($count(obs)){
    //use the variable
}else{
    return;
}

Discuss

A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.

Found a bug in this plugin? Please report it this repository's Github Issues.

blog comments powered by Disqus