The main key for the entire concept is, at minimum, distinguish between empty and non-empty variables. So i attempt to give a useful empty check, and if possible give an actual length. but the fallback should always assume that complex types are non-empty unless stated otherwise.
On Fri, Feb 12, 2010 at 10:42, ibolmo <[email protected]> wrote: > Why is the number returning itself? Not sure how a number has a > length. > For the number, i was contemplating returning the number of digits, (simply do (obj+"").length). not sure which is better. Functions do have length, try: > > (function(a, b, c){}).length vs (function(){}).length (the length is > the # of args defined -- note: not the # passed). > > A function always has members attached to it (at least with Mootools, which extends the native Function), so using it's length property isn't very useful. But the only scenario i can see a function passed to this function is to see if it's empty, and so i check if the function has any logic attached to it or not. > 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. > Tricky indeed. The only mistake i can see happening with this implementation is when an element with only text in it will return 0, which isn't completely true, as it isn't actually empty. would love to hear some better suggestions on how to better implement this. For now i assume that if you pass an element, you expect to know how many child elements it has. It's all about defining an expected behavior. > > 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. > > Almost all high-level objects will return NaN on enumeration. this includes class, regexp etc. for these, until i figure a better implementation, i prefer to assume non-empty, so i return 1. Dates on the other hand, are enumerable and return their timestamp. Again - most complex types have no logical length, so it's all about deciding what will be expected of the function to check, rather than finding out the most precise representation. The main reason i posted this here is to see what other people have to say about these complex types.
