1 if(!dojo._hasResource["dojo._base.lang"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo._base.lang"] = true;
3 dojo.provide("dojo._base.lang");
5 // Crockford (ish) functions
7 dojo.isString = function(/*anything*/ it){
9 // Return true if it is a String
10 return !!arguments.length && it != null && (typeof it == "string" || it instanceof String); // Boolean
13 dojo.isArray = function(/*anything*/ it){
15 // Return true if it is an Array
16 return it && (it instanceof Array || typeof it == "array"); // Boolean
20 dojo.isFunction = function(it){
21 // summary: Return true if it is a Function
27 dojo.isFunction = (function(){
28 var _isFunction = function(/*anything*/ it){
29 return it && (typeof it == "function" || it instanceof Function); // Boolean
32 return dojo.isSafari ?
33 // only slow this down w/ gratuitious casting in Safari since it's what's b0rken
34 function(/*anything*/ it){
35 if(typeof it == "function" && it == "[object NodeList]"){ return false; }
36 return _isFunction(it); // Boolean
40 dojo.isObject = function(/*anything*/ it){
42 // Returns true if it is a JavaScript object (or an Array, a Function
44 return it !== undefined &&
45 (it === null || typeof it == "object" || dojo.isArray(it) || dojo.isFunction(it)); // Boolean
48 dojo.isArrayLike = function(/*anything*/ it){
50 // similar to dojo.isArray() but more permissive
52 // Doesn't strongly test for "arrayness". Instead, settles for "isn't
53 // a string or number and has a length property". Arguments objects
54 // and DOM collections will return true when passed to
55 // dojo.isArrayLike(), but will return false when passed to
58 // If it walks like a duck and quicks like a duck, return `true`
60 return it && it !== undefined &&
61 // keep out built-in constructors (Number, String, ...) which have length
63 !d.isString(it) && !d.isFunction(it) &&
64 !(it.tagName && it.tagName.toLowerCase() == 'form') &&
65 (d.isArray(it) || isFinite(it.length)); // Boolean
68 dojo.isAlien = function(/*anything*/ it){
70 // Returns true if it is a built-in function or some other kind of
71 // oddball that *should* report as a function but doesn't
72 return it && !dojo.isFunction(it) && /\{\s*\[native code\]\s*\}/.test(String(it)); // Boolean
75 dojo.extend = function(/*Object*/ constructor, /*Object...*/ props){
77 // Adds all properties and methods of props to constructor's
78 // prototype, making them available to all instances created with
80 for(var i=1, l=arguments.length; i<l; i++){
81 dojo._mixin(constructor.prototype, arguments[i]);
83 return constructor; // Object
86 dojo._hitchArgs = function(scope, method /*,...*/){
87 var pre = dojo._toArray(arguments, 2);
88 var named = dojo.isString(method);
91 var args = dojo._toArray(arguments);
93 var f = named ? (scope||dojo.global)[method] : method;
94 // invoke with collected args
95 return f && f.apply(scope || this, pre.concat(args)); // mixed
99 dojo.hitch = function(/*Object*/scope, /*Function|String*/method /*,...*/){
101 // Returns a function that will only ever execute in the a given scope.
102 // This allows for easy use of object member functions
103 // in callbacks and other places in which the "this" keyword may
104 // otherwise not reference the expected scope.
105 // Any number of default positional arguments may be passed as parameters
107 // Each of these values will be used to "placehold" (similar to curry)
108 // for the hitched function.
110 // The scope to use when method executes. If method is a string,
111 // scope is also the object containing method.
113 // A function to be hitched to scope, or the name of the method in
114 // scope to be hitched.
116 // | dojo.hitch(foo, "bar")();
117 // runs foo.bar() in the scope of foo
119 // | dojo.hitch(foo, myFunction);
120 // returns a function that runs myFunction in the scope of foo
121 if(arguments.length > 2){
122 return dojo._hitchArgs.apply(dojo, arguments); // Function
128 if(dojo.isString(method)){
129 scope = scope || dojo.global;
130 if(!scope[method]){ throw(['dojo.hitch: scope["', method, '"] is null (scope="', scope, '")'].join('')); }
131 return function(){ return scope[method].apply(scope, arguments || []); }; // Function
133 return !scope ? method : function(){ return method.apply(scope, arguments || []); }; // Function
137 dojo.delegate = function(obj, props){
139 // returns a new object which "looks" to obj for properties which it
140 // does not have a value for. Optionally takes a bag of properties to
141 // seed the returned object with initially.
143 // This is a small implementaton of the Boodman/Crockford delegation
144 // pattern in JavaScript. An intermediate object constructor mediates
145 // the prototype chain for the returned object, using it to delegate
146 // down to obj for property lookup when object-local lookup fails.
147 // This can be thought of similarly to ES4's "wrap", save that it does
148 // not act on types but rather on pure objects.
150 // The object to delegate to for properties not found directly on the
151 // return object or in props.
153 // an object containing properties to assign to the returned object
155 // an Object of anonymous type
157 // | var foo = { bar: "baz" };
158 // | var thinger = dojo.delegate(foo, { thud: "xyzzy"});
159 // | thinger.bar == "baz"; // delegated to foo
160 // | foo.thud == undefined; // by definition
161 // | thinger.thud == "xyzzy"; // mixed in from props
162 // | foo.bar = "thonk";
163 // | thinger.bar == "thonk"; // still delegated to foo's bar
168 dojo.delegate = dojo._delegate = function(obj, props){
170 // boodman/crockford delegation
175 dojo.mixin(tmp, props);
177 return tmp; // Object
180 dojo.partial = function(/*Function|String*/method /*, ...*/){
182 // similar to hitch() except that the scope object is left to be
183 // whatever the execution context eventually becomes.
185 // Calling dojo.partial is the functional equivalent of calling:
186 // | dojo.hitch(null, funcName, ...);
188 return dojo.hitch.apply(dojo, arr.concat(dojo._toArray(arguments))); // Function
191 dojo._toArray = function(/*Object*/obj, /*Number?*/offset, /*Array?*/ startWith){
193 // Converts an array-like object (i.e. arguments, DOMCollection) to an
194 // array. Returns a new Array with the elements of obj.
196 // the object to "arrayify". We expect the object to have, at a
197 // minimum, a length property which corresponds to integer-indexed
200 // the location in obj to start iterating from. Defaults to 0.
203 // An array to pack with the properties of obj. If provided,
204 // properties in obj are appended at the end of startWith and
205 // startWith is the returned array.
206 var arr = startWith||[];
207 for(var x = offset || 0; x < obj.length; x++){
213 dojo.clone = function(/*anything*/ o){
215 // Clones objects (including DOM nodes) and all children.
216 // Warning: do not clone cyclic structures.
220 for(var i = 0; i < o.length; ++i){
221 r.push(dojo.clone(o[i]));
225 if(!dojo.isObject(o)){
226 return o; /*anything*/
228 if(o.nodeType && o.cloneNode){ // isNode
229 return o.cloneNode(true); // Node
231 if(o instanceof Date){
232 return new Date(o.getTime()); // Date
235 var r = new o.constructor(); // specific to dojo.declare()'d classes!
237 if(!(i in r) || r[i] != o[i]){
238 r[i] = dojo.clone(o[i]);
244 dojo.trim = function(/*String*/ str){
246 // trims whitespaces from both sides of the string
248 // This version of trim() was selected for inclusion into the base due
249 // to its compact size and relatively good performance (see Steven
251 // http://blog.stevenlevithan.com/archives/faster-trim-javascript).
252 // The fastest but longest version of this function is located at
253 // dojo.string.trim()
254 return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); // String