Why is the number returning itself? Not sure how a number has a
length.
Functions do have length, try:
(function(a, b, c){}).length vs (function(){}).length (the length is
the # of args defined -- note: not the # passed).
I guess length of an element could represent the number of children.
but what about elements with no children but have text. e.g. span.
length of 0 but an infinite # of chars? Tricky.
Not sure again why you're falling back on where the Number(obj) ?
Number(obj) : 1 makes sense. Also not sure when you'd get to this
point. Your first check for falsies takes care of the majority of
cases.
I guess you're considering for regexp or dates? But why would 1 be a
good length for them?
For regexp, it'd be nice to know how many capturing matchers? /(.)(.)
(?:.)(.)/.length == 3
Date, (I haven't kept up with the newest specs, but if it's a range of
dates then well that's easy to say what the length is. Perhaps if you
query for the month you'd want to know the number of days in the
month. And similar: How many hours left in the day? But again, that
all depends on the Date object and how you interpret it. hehe.
Anyway good stuff.
On Feb 11, 5:31 pm, אריה גלזר <[email protected]> wrote:
> 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 thishttp://www.jsfiddle.net/tvYQT/1as a proof of
> >>> concept.
> >>> What do you think? would you find this useful? how would you handle
> >>> non-lengthy types?
> >>> -----------
> >>> אריה גלזר
> >>> 052-5348-561
> >>> 5561