Opened 14 years ago

Closed 13 years ago

Last modified 12 years ago

#562 closed enhancement (fixed)

dojo.lang.getType() - gets dojo.lang.is*() compatible type

Reported by: wolfram.kriesing@… Owned by: anonymous
Priority: high Milestone:
Component: Core Version: 0.3
Keywords: Cc: Brian Douglas Skinner <skinner@…>
Blocked By: Blocking:

Description

lang/common.js has functions dojo.lang.is*() but a compatible getType() is missing it seems? Here it is

Index: src/lang/common.js
===================================================================
--- src/lang/common.js  (revision 3329)
+++ src/lang/common.js  (working copy)
@@ -160,3 +160,30 @@
 }
 
 // end Crockford functions
+
+/**
+* Return the type that dojo detects for this variable.
+* The result is compatible to the dojo.lang.is*() functions.
+* So if dojo.lang.isNumber(x) returns true then 
+* dojo.lang.getType(x) return "Number". Which is exactly the
+* same as the is*() function name, just leaving out the "is".
+*
+* @param mixed The variable for which to return the type.
+* @return string The type, as dojo sees it.
+*/
+dojo.lang.getType = function(variable) {
+       // Types like "Array" go before "ArrayLike" so we use a
+       // sorted array which's order is used to find out the type.
+       // Check simple types first.
+       var types = [
+               "String", "Boolean", "Number", "Array"
+               ,"ArrayLike"
+               ,"Object", "Function", "Alien"
+               ,"Undefined"
+       ];
+       for (var i in types) {
+               if (dojo.lang["is"+types[i]](variable)) {
+                       return types[i];
+               }
+       }
+}
 No newline at end of file
lang.common.diff (END) 

Attachments (2)

lang.common.diff (1.1 KB) - added by wolfram.kriesing@… 14 years ago.
the same as shown in the comment
lang.common.2.diff (1.1 KB) - added by wolfram.kriesing@… 14 years ago.
The new diff as that goes with the last comment

Download all attachments as: .zip

Change History (11)

Changed 14 years ago by wolfram.kriesing@…

Attachment: lang.common.diff added

the same as shown in the comment

comment:1 Changed 14 years ago by skinner

Cc: Brian Douglas Skinner <skinner@…> added

In src/lang/type.js there's a function dojo.lang.whatAmI(), which is similar to the proposed dojo.lang.getType() function.

I think getType() is better name than whatAmI(), so I'd be in favor taking the existing whatAmI() function and changing the name to getType(), and then making a whatAmI() function that wraps getType() but which is marked as deprecated.

It might make sense to move getType() from type.js into common.js, or move the other type checking functions from common.js over to type.js, but David Schontzler put a lot of thought into the re-org of dojo.lang back in January 2006, so it may be best the way it is.

We could think about replacing the current whatAmI() implementation, but there are some tricky corner cases with this type stuff, so we should do a lot of unit testing if we change anything.

  • Brian Skinner

comment:2 Changed 14 years ago by wolfram.kriesing@…

In my opinion the types should be consistent in dojo. If dojo.lang.is[Type]() returns true, dojo.lang.getType() should return [Type] too. Or what tricky corner cases are there, that make this more difficult?

comment:3 Changed 14 years ago by wolfram.kriesing@…

And for stuffing it into lang/type.js or lang/common.js - I think lang/type.js is very obvious, since all this is type checking stuff. Just my 2 cents ...

comment:4 Changed 14 years ago by wolfram.kriesing@…

"Function" needs to be checked before "Object" otherwise a function will be 'seen' as a object, which is not wanted. Are there any furhter thoughts on this topic?

Changed 14 years ago by wolfram.kriesing@…

Attachment: lang.common.2.diff added

The new diff as that goes with the last comment

comment:5 Changed 14 years ago by skinner

Hi Wolfram -- I take it back about the tricky corner cases. I ran into those when I was doing tests with isNaN() and isNumeric(), but that doesn't matter for getType() if getType never returns "Numeric" as a type. The only trick is to do the checks in the right order, like what you mentioned about checking for "Function" before "Object".

I agree that the dojo.lang.is[Type] methods should be consistent with dojo.lang.getType(). We should add unit tests for that, similar to the current tests/lang/test_type.js test "300:", which makes sure dojo.lang.isOfType() and dojo.lang.whatAmI() are consistent.

comment:6 Changed 14 years ago by Tom Trenka

Resolution: fixed
Status: newclosed

The function your looking for is at the bottom of lang/type.js: dojo.lang.getObject. Pass it a string and it will return you that object if it exists.

The reality is that there is no "types" in Javascript, just objects; what you'd consider to be a type (i.e. constructor) is also an object, and will be returned as well. For example:

var getType=dojo.lang.getObject("dojo.lang.getObject");
alert(getType("document"));

comment:7 Changed 13 years ago by skinner

Milestone: 0.4
Resolution: fixed
Status: closedreopened
Version: 0.20.3

I think this bug was closed without actually being resolved.

As I understand it, the dojo.lang.getType() function that wolfram suggested is not the same thing as the existing dojo.lang.getObject() function. I do think wolfram's suggested dojo.lang.getType() is exactly the same thing as the existing dojo.lang.whatAmI() function. So the bug isn't that getType() is missing; the bug is that whatAmI() isn't as good a name as getType(). I'm re-opening the bug, and I'll fix it.

comment:8 Changed 13 years ago by skinner

Resolution: fixed
Status: reopenedclosed

fixed in rev 4677

comment:9 Changed 12 years ago by (none)

Milestone: 0.4

Milestone 0.4 deleted

Note: See TracTickets for help on using tickets.