well, i made some more modifications and made this a
gist<http://gist.github.com/302080>.
not sure if this is forge material...
This is the main logic i applied:
return the length for any variable that has a distinguishable length
(Array,Object,String,Number).
for other types return:
- 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 and false) : Will return 0.
- For all others:
1. Check for a length property.
2. If not present, try to enumerate the value.
3. if still unsuccessful (NaN), return 1.
the reasoning behind this is to allow the method to distinguish between
empty and non-empty variables if they do not have a real length.
-----------
אריה גלזר
052-5348-561
5561
On Thu, Feb 11, 2010 at 22:57, אריה גלזר <[email protected]> wrote:
> as i said, this is just a thought. The best use case i can think of is
> something like this:
>
> function doSomething(obj){
> if ($count(obj)){
> //if the object is empty or undefined or anything falsy
> }else{
> //if the object is useful
> }
> }
>
> 2 places where i can see thins as useful:
>
> 1. it's easier than to remember what is the right method for your
> specific variable (does it have a length property? what is the method to
> get
> it's length if not?).
> 2. the second is when i want to know if a variable is empty. in PHP,
> empty arrays are falsy. In JS, empty arrays and objects are truthy, and
> there are many case where you would like to easily check if that variable
> is
> empty. In a way, this use case is more like $isEmpty than $count
>
>
> the rest of the use cases ('regexp','function' etc) are just so that there
> will be a standardized way to handle them if passed.
>
> -----------
> אריה גלזר
> 052-5348-561
> 5561
>
>
> 2010/2/11 Fábio M. Costa <[email protected]>
>
> I think that your function looks fine, ive just changed some little stuff:
>>
>> http://www.jsfiddle.net/tvYQT/3/
>>
>> i think you need to use getLength() (not sure) on the hash for example.
>>
>> But if you could explain us why you need this kind of functionality, and
>> whats the problem your having, maybe we could come with a better solution.
>>
>> Cheers,
>>
>> --
>> Fábio Miranda Costa
>> Solucione Sistemas
>> Engenheiro de interfaces
>>
>>
>> On Thu, Feb 11, 2010 at 4:29 PM, אריה גלזר <[email protected]>wrote:
>>
>>> i've been playing around with implementing a count method in JS, that
>>> will return the length of a given variable, no matter what it's type is.
>>> this is mainly useful for switching between array/object/string, but also
>>> might be interesting to see it work with elements.
>>> obviously, the main problem is how to handle types that have no distinct
>>> length, like functions, regexp etc.
>>>
>>> I've come up with this http://www.jsfiddle.net/tvYQT/1 as a proof of
>>> concept.
>>> What do you think? would you find this useful? how would you handle
>>> non-lengthy types?
>>> -----------
>>> אריה גלזר
>>> 052-5348-561
>>> 5561
>>>
>>
>>
>