3 Language Utilities. Easier inheritance, scope handling, type checks.
9 Clone native types like Object, Array, RegExp, Date and primitives.
11 This method will not clone values that are referenced within `val`. It will
12 only copy the value reference to the new value. If the value is not a plain
13 object but is an object, it will return the value unchanged.
18 var a = { foo: 'bar' };
20 console.log(a === b); // false
21 console.log(a.foo === b.foo); // true
25 console.log(c === d); // false
26 console.log(c); // [1, 2, 3]
29 See: [`deepClone()`](#deepClone)
33 ## createObject(parent, [props]):Object
35 Create Object using prototypal inheritance and setting custom properties.
37 Mix between [Douglas Crockford Prototypal Inheritance](http://javascript.crockford.com/prototypal.html) and [`object/mixIn`](./object.html#mixIn).
41 1. `parent` (Object) : Parent Object
42 2. `[props]` (Object) : Object properties
49 console.log(this.name);
53 var myObject = createObject(base, {
57 myObject.trace(); // "Lorem Ipsum"
62 ## ctorApply(constructor, args):Object
64 Do `Function.prototype.apply()` on a constructor while maintaining prototype
68 function Person(name, surname) {
70 this.surname = surname;
73 Person.prototype.walk = function(){
74 console.log(this.name +' is walking');
77 var args = ['John', 'Doe'];
79 // "similar" effect as calling `new Person("John", "Doe")`
80 var john = ctorApply(Person, args);
81 john.walk(); // "John is walking"
86 ## deepClone(val, [instanceClone]):*
88 Deep clone native types like Object, Array, RegExp, Date and primitives.
90 The `instanceClone` function will be invoked to clone objects that are not
91 "plain" objects (as defined by [`isPlainObject`](#isPlainObject)) if it is
92 provided. If `instanceClone` is not specified, it will not attempt to clone
93 non-plain objects, and will copy the object reference.
98 var a = {foo:'bar', obj: {a:1, b:2}};
99 var b = deepClone(a); // {foo:'bar', obj: {a:1, b:2}}
100 console.log( a === b ); // false
101 console.log( a.obj === b.obj ); // false
103 var c = [1, 2, [3, 4]];
104 var d = deepClone(c); // [1, 2, [3, 4]]
105 var e = c.concat(); // [1, 2, [3, 4]]
107 console.log( c[2] === d[2] ); // false
108 // concat doesn't do a deep clone, arrays are passed by reference
109 console.log( e[2] === d[2] ); // true
111 function Custom() { }
112 function cloneCustom(x) { return new Custom(); }
113 var f = { test: new Custom() };
114 var g = deepClone(f, cloneCustom);
115 g.test === f.test // false, since new Custom instance will be created
118 See: [`clone()`](#clone)
122 ## deepEquals(a, b, [callback]):Boolean
124 Recursively tests whether two values contains the same keys and values.
126 `callback` specifies the equality comparison function used to compare
127 non-object values. It defaults to using the [`is()`](#is) function.
129 If the values are both an object or array, it will recurse into both values,
130 checking if their keys/values are equal. It will only check the keys and values
131 contained by the objects; it will not check the objects' prototypes. If either
132 of the values are not objects, they will be checked using the `callback`
138 deepEquals({ a: 1 }, { a: 1 }); // true
139 deepEquals({ value: { a: 1 } }, { value: { a: 1 } }); // true
140 deepEquals({ value: { a: 1 } }, { value: { a: 2 } }); // false
141 deepEquals({ value: { a: 1 } }, { value: { a: 1, b: 2 } }); // false
142 deepEquals({}, null); // false
143 deepEquals(null, null); // true
147 function(a, b) { return a == b; }); // true
150 See: [object/equals](object.html#equals), [array/equals](array.html#equals)
154 ## defaults(val, ...defaults):void
156 Return first value that isn't `null` or `undefined`.
158 function doSomethingAwesome(foo, bar) {
160 foo = defaults(foo, 'lorem');
161 bar = defaults(bar, 123);
169 Reference to the global context (`window` inside a browser, `global` on
170 node.js). Works on ES3 and ES5 strict-mode.
174 ## inheritPrototype(childCtor, parentCtor):Object
176 Inherit the prototype methods from one constructor into another.
178 Similar to [node.js util/inherits](http://nodejs.org/docs/latest/api/util.html#util_util_inherits_constructor_superconstructor).
180 It returns the the `childCtor.prototype` for convenience.
187 getName : function(){
193 Foo.call(this, name);
195 //should be called before calling constructor
196 var proto = inheritPrototype(Bar, Foo);
198 // for convenience we return the new prototype object
199 console.log(proto === Bar.prototype); // true
201 var myObj = new Bar('lorem ipsum');
202 myObj.getName(); // "lorem ipsum"
204 console.log(myObj instanceof Foo); // true
206 // you also have access to the "super" constructor
207 console.log(Bar.super_ === Foo); // true
213 Check if both values are identical/egal.
217 NaN === NaN; // false
220 is(NaN, NaN); // true
222 is('a', 'b'); // false
225 See: [`isnt()`](#isnt)
229 ## isnt(x, y):Boolean
231 Check if both values are not identical/egal.
235 NaN === NaN; // false
238 isnt(NaN, NaN); // false
239 isnt(-0, +0); // true
240 isnt('a', 'b'); // true
248 ## isArguments(val):Boolean
250 If value is an "Arguments" object.
254 ## isArray(val):Boolean
256 If value is an Array. Uses native ES5 `Array.isArray()` if available.
260 ## isBoolean(val):Boolean
262 If value is a Boolean.
266 ## isDate(val):Boolean
272 ## isEmpty(val):Boolean
274 Checks if Array/Object/String is empty.
276 Will return `true` for any object that doesn't contain enumerable properties
277 and also to any type of value that isn't considered a collection (boolean,
278 null, undefined, function, etc).
282 isEmpty('bar'); // false
284 isEmpty([1, 2]); // false
286 isEmpty({a:1, b:2}); // false
287 // null, undefined, booleans, numbers are considered as "empty" values
288 isEmpty(null); // true
289 isEmpty(undefined); // true
290 isEmpty(123); // true
291 isEmpty(true); // true
295 ## isFinite(val):Boolean
297 Checks if value is Finite.
299 **IMPORTANT:** This is not the same as native `isFinite`, which will return
300 `true` for values that can be coerced into finite numbers. See
301 http://es5.github.com/#x15.1.2.5.
304 isFinite(123); // true
305 isFinite(Infinity); // false
307 // this is different than native behavior
308 isFinite(''); // false
309 isFinite(true); // false
310 isFinite([]); // false
311 isFinite(null); // false
315 ## isFunction(val):Boolean
317 If value is a Function.
321 ## isKind(val, kind):Boolean
323 If value is of "kind". (used internally by some of the *isSomething* checks).
325 Favor the other methods since strings are commonly mistyped and also because
326 some "kinds" can only be accurately checked by using other methods (e.g.
327 `Arguments`), some of the other checks are also faster.
330 isKind([1,2], 'Array'); // true
331 isKind(3, 'Array'); // false
332 isKind(3, 'Number'); // true
335 See: [`kindOf()`](#kindOf)
339 ## isInteger(val):Boolean
341 Check if value is an integer.
344 isInteger(123); // true
345 isInteger(123.45); // false
346 isInteger({}); // false
347 isInteger('foo'); // false
348 isInteger('123'); // false
353 ## isNaN(val):Boolean
355 Check if value is not a number.
357 It doesn't coerce value into number before doing the check, giving better
358 results than native `isNaN`. Returns `true` for everything besides numeric
361 **IMPORTANT:** behavior is very different than the native `isNaN` and way more
362 useful!!! See: http://es5.github.io/#x15.1.2.4
369 isNaN(undefined); // true
370 isNaN([4,5]); // true
372 // these are all "false" on native isNaN and main reason why this module exists
376 isNaN(false); // true
377 isNaN("123"); // true
384 ## isNull(val):Boolean
390 ## isNumber(val):Boolean
392 If value is a Number.
396 ## isObject(val):Boolean
398 If value is an Object.
402 ## isPlainObject(val):Boolean
404 If the value is an Object created by the Object constructor.
408 ## isRegExp(val):Boolean
410 If value is a RegExp.
414 ## isString(val):Boolean
416 If value is a String.
420 ## isUndefined(val):Boolean
422 If value is `undefined`.
426 ## kindOf(val):String
428 Gets kind of value (e.g. "String", "Number", "RegExp", "Null", "Date").
429 Used internally by `isKind()` and most of the other *isSomething* checks.
432 kindOf([1,2]); // "Array"
433 kindOf('foo'); // "String"
434 kindOf(3); // "Number"
437 See: [`isKind()`](#isKind)
440 ## toArray(val):Array
442 Convert array-like object into Array or wrap value into Array.
449 }); // ["foo", "bar"]
452 return toArray(arguments);
454 foo("lorem", 123); // ["lorem", 123]
456 toArray("lorem ipsum"); // ["lorem ipsum"]
457 toArray(window); // [window]
458 toArray({foo:"bar", lorem:123}); // [{foo:"bar", lorem:123}]
465 ## toNumber(val):Number
467 Convert value into number.
470 // numeric values are typecasted as Number
471 toNumber('123'); // 123
472 toNumber(-567); // -567
474 // falsy values returns zero
477 toNumber(undefined); // 0
478 toNumber(false); // 0
480 // non-numeric values returns NaN
481 toNumber('asd'); // NaN
485 // Date objects return milliseconds since epoch
486 toNumber(new Date(1985, 6, 23)); // 490935600000
491 ## toString(val):String
493 Convert any value to its string representation.
495 Will return an empty string for `undefined` or `null`, otherwise will convert
496 the value to its string representation.
499 // null and undefined are converted into empty strings
500 toString(null); // ""
501 toString(undefined); // ""
504 toString([1,2,3]); // "1,2,3"
505 toString(false); // "false"
507 // uses `val.toString()` to convert value
508 toString({toString:funtion(){ return 'foo'; }}); // "foo"
513 -------------------------------------------------------------------------------
515 For more usage examples check specs inside `/tests` folder. Unit tests are the
516 best documentation you can get...