(function(){ /** * @alias JSCore * JavaScript Core Utility Functions */ var JSCore = window.JSCore = (JSCore || {}); // Assign Version Number JSCore.VERSION = 1.00; /** * @alias JSCore.clone * Shallow lones an object. * Developers using JSOOP should instead use the JSOOP.clone method. * If the type is passed-by-value (such as a string or number) or un-cloneable (such as a function) * the original value is returned. * @param {Object} value * @return {Object} */ JSCore.clone = function clone(value) { switch (JSCore.typeOf(value)) { case 'array': return value.slice(0); case 'object': return JSCore.copy({}, value); } return value; }; /** * @alais JSCore.copy * @param {Object} to * @param {Object} from * @return {Object} */ JSCore.copy = function copy(to, from) { for (var k in from) to[k] = from[k]; return to; }; /** * @alias JSCore.forEach * Iterates through an Iterable. Nothing is done if the object is not iterable. * @param {Object} value * @param {Functipn} callback * @return {Object} the value specified originally */ JSCore.forEach = function forEach(value, callback) { if (typeof value.forEach == 'function') value.forEach(callback); else if (typeof value.length == 'number') for (var i = 0, l = value.length; i < l; ++i) callback(value[i]); return value; }; /** * @alias JSCore.isArray * Returns whether or not the value specified is an Array * @param {Object} [value] * @return {Boolean} */ JSCore.isArray = function isArray(value) { return value instanceof Array; }; /** * @alias JSCore.isBoolean * Returns whether or not the value specified is a Boolean (true or false) * @param {Object} [value] * @return {Boolean} */ JSCore.isBoolean = function isBoolean(value) { return value === true || value === false; }; /** * @alias JSCore.isDefined * Returns whether or not the value specified is defined * @param {Object} [value] * @return {Boolean} */ JSCore.isDefined = function isDefined(value) { return typeof value != 'undefined'; }; /** * @alias JSCore.isBoolean * Returns whether or not the value specified is a Function * @param {Object} [value] * @return {Boolean} */ JSCore.isFunction = function isFunction(value) { return value instanceof Function; }; /** * @alias JSCore.isInteger * Returns whether or not the value specified is an integer * * @param {Object} [value] * @return {Boolean} */ JSCore.isInteger = function isInteger(value) { return Math.floor(value) == value; }; /** * @alias JSCore.isIterable * Returns whether or not the value can be iterated using JSCore.forEach. * The following is supported: * *

* Note: JSOOP has it's own isIterable method, and their own forEach * that also checks for implementations of the JSOOP.Generic.Iterable interface. * For developers using JSOOP, it is preferable to use their methods * @param {Object} [value] * @return {Boolean} */ JSCore.isIterable = function isIterable(value) { return value && (JSCore.isArray(value) || typeof value.length == 'number' || typeof value.forEach == 'function'); }; /** * @alias JSCore.isObject * Returns whether or not the value specified is an Object. Warning - This will return * as false if the type is Array or null; object in this sense means a plain old * javascript object, not necessarily anything that inherits of Object... because everything does. * * @param {Object} [value] * @return {Boolean} */ JSCore.isObject = function isObject(value) { return JSCore.typeOf(value) == 'object'; }; /** * @alias JSCore.isNumber * Returns whether or not the value specified is a number * * @param {Object} [value] * @return {Boolean} */ JSCore.isNumber = function isNumber(value) { return typeof value == 'number'; }; /** * @alias JSCore.toArray * Always returns an Array object, regardless of input. * The following rules are used: * * @param {Array|Object} [value] * Any value type * @return {Array} */ JSCore.toArray = function toArray(value) { switch (JSCore.typeOf(value)) { case 'array': return value.slice(0); case 'object': if (JSCore.isIterable(value)) { var tempArray = []; JSCore.forEach(value, function(e){ tempArray.push(e); }); } break; case 'null': case 'undefined': return []; } return [value]; }; /** * @alias JSCore.typeOf * A cross-browser standard version of the typeof function, which can return the following: * *

* It may be more appropriate and efficient to use the distinct isX functions for type checking. * @param {Object} [value] * @return {String} */ JSCore.typeOf = function typeOf(value) { var identifiedType = typeof value; switch (identifiedType) { case 'object': if (value instanceof Array) return 'array'; if (value === null) return 'null'; break; } return identifiedType; }; })();