All pastes #1316103 Raw Edit

JSCore

public javascript v1 · immutable
#1316103 ·published 2009-01-23 06:35 UTC
rendered paste body
(function(){/** * @alias JSCore * JavaScript Core Utility Functions */var JSCore = window.JSCore = (JSCore || {});/** * @alias JSCore.forEach * Iterates through an <code>Iterable</code>. <em>Nothing</code> is done if the object is not iterable</em>. * @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 (<code>true</code> or <code>false</code>) * @param {Object} [value] * @return {Boolean} */JSCore.isBoolean = function isBoolean(value) {	return value === true || value === false;};/** * @alias JSCore.isBoolean * Returns whether or not the value specified is a <code>Function</code> * @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 <code>JSCore.forEach</code>. * The following is supported: * <ul> *   <li><code>Array</code></li> *   <li><code>Object</code>'s that have a numerical <code>length</code> property and accessible elements</li> *   <li><code>Object</code>'s that have a pre-defined <code>forEach</code> method</li> * </ul> * <br><br> * <em>Note: JSOOP has it's own <code>isIterable</code> method, and their own <code>forEach</code> * that also checks for implementations of the <code>JSOOP.Generic.Iterable</code> interface.  * For developers using JSOOP, it is preferable to use their methods</em> * @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 <code>Object</code>. <b>Warning</b> - This will return * as false if the type is <code>Array</code> or <code>null</code>; object in this sense means a plain old * javascript object, not necessarily anything that inherits of <code>Object</code>... 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 <code>Array</code> object, regardless of input.  * The following rules are used: * <ul> * 	<li><code>value</code> is of type <code>Array</code>: Returns a shallow clone of <code>value</code></li> *  <li><code>value</code> is of type <code>Object</code>, but is iterable; that is, has a dynamically allocated *  	<code>length</code> property, and accessible nodes via the bracket index bracket syntax: Returns the value *  	transformed into an <code>Array</code></li> * <li><code>value</code> is of type <code>null</code> or <code>undefined</code>: A blank array is returned</li> * <li>Anything else, an array with a single element of <code>value</code> is returned</li> * </ul> * @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 <code>typeof</code> function, which can return the following: * <ul> * 	<li>array</li> *  <li>boolean</li> *  <li>function</li> *  <li>object</li> *  <li>number</li> *  <li>null</li> *  <li>string</li> *  <li>undefined</li> * </ul> * <br><br> * 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;};})();