/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: * ******************************************************************* * Project: Array Enchant * * Version: 1.1 * * Author: RabiatoR * ******************************************************************* * JavaScript "Array" object functionnaly enchant library * Includes: * Bool Array().contains( Mixed needle [, String needle_key] ); * Bool Array().forEach( Function callback ); * Bool Array().every( Function callback ); * Bool Array().some( Function callback ); * Array Array().filter( Function callback ); * Array Array().map( Function callback ); * Bool Array().dumpKey( String key ); * String Array().dump( void ); * void Array().alert( void ); * Array Array().keyCase( [Boolean upperCase = false] ); * Array Array().chunk( Number size [, Boolean preserveKeys = false] ); * Array Array().combine( Array _array [, Boolean arrayKeys = false] ); * Array Array().countValues( void ); * Array Array().diff( Array _array [, Boolean strict = false] ); * Array Array().fill( [Number start = 0],[Number count = Start], Mixed val ); * Array Array().fillKeys( Array keys, Mixed val ); * Array Array().flip( void ); * Array Array().unique( void ); * Bool Array().keyExists( String key ); * String Array().search( Mixed needle ); * Self Array().empty( void ); * * Changelog: * [1.1] * Syntax reformatted. * Array().chunk - negative size bug fixed. * Array().search - strict test applied. * [1.0] * First release. * * */ /**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**/ /* * Syntax: Array().contains( Mixed needle [, String needle_key] ); * Return: Boolean; * Failur: false; *****************************************************************/ if( typeof( Array.prototype.contains ) == 'undefined' ){ Array.prototype.contains = function( needle, needle_key ){ if( typeof( needle ) == 'undefined' ){ return false; } if( typeof( needle_key ) != 'undefined' ){ return ( this[needle_key] == needle ); } for( var k in this ){ if( this[k] == needle ){ return true; } } return false; }; } /* * Syntax: Array().forEach( Function callback ); * Return: Boolean; * Failur: false; *****************************************************************/ if( typeof( Array.prototype.forEach ) == 'undefined' ){ Array.prototype.forEach = function( callback ){ if( typeof( callback ) != 'function' ){ return false; } for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ callback( this[k], k, this ); } } return true; }; } /* * Syntax: Array().every( Function callback ); * Return: Boolean; * Failur: false; *****************************************************************/ if( typeof( Array.prototype.every ) == 'undefined' ){ Array.prototype.every = function( callback ){ if( typeof( callback ) != 'function' ){ return false; } for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ if( callback( this[k], k, this ) === false ){ return false; } } } return true; }; } /* * Syntax: Array().some( Function callback ); * Return: Boolean; * Failur: false; *****************************************************************/ if( typeof( Array.prototype.some ) == 'undefined' ){ Array.prototype.some = function( callback ){ if( typeof( callback ) != 'function' ){ return false; } for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ if( callback( this[k], k, this ) === true ){ return true; } } } return false; }; } /* * Syntax: Array().filter( Function callback ); * Return: Array(); * Failur: false; *****************************************************************/ if( typeof( Array.prototype.filter ) == 'undefined' ){ Array.prototype.filter = function( callback ){ if( typeof( callback ) != 'function' ){ return false; } var retn = []; for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ if( callback( this[k], k, this ) === true ){ retn[k] = this[k]; } } } return retn; }; } /* * Syntax: Array().map( Function callback ); * Return: Array(); * Failur: false; *****************************************************************/ if( typeof( Array.prototype.map ) == 'undefined' ){ Array.prototype.map = function( callback ){ if( typeof( callback ) != 'function' ){ return false; } var retn = []; for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ this[k] = callback( this[k], k, this ); } } return retn; }; } /* * Syntax: Array().dumpKey( String key ); * Return: Boolean; * Failur: false; *****************************************************************/ if( typeof( Array.prototype.dumpKey ) == 'undefined' ){ Array.prototype.dumpKey = function( key ){ var k = (parseInt(this[key],10).toString()==this[key])?key:"\""+ key +"\""; return k +": "+ this[key]; }; } /* * Syntax: Array().dump( void ); * Return: String; * Failur: false; * Rquars: Array().dumpKey; *****************************************************************/ if( typeof( Array.prototype.dump ) == 'undefined' ){ Array.prototype.dump = function(){ if( typeof( Array.prototype.dumpKey ) != 'function' ){ return false; } var retn = ["Array(" + this.length + "){"]; for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ if( typeof( this[k] ) == 'object' && this[k] instanceof Array ){ retn[retn.length] = " " + this[k].dump(); }else{ retn[retn.length] = " " + this.dumpKey( k ); } } } retn[retn.length] = ident + "}"; return retn.join( ident + "\n" ); }; } /* * Syntax: Array().alert( void ); * Return: void; * Failur: false; * Rquars: Array().dump; *****************************************************************/ if( typeof( Array.prototype.alert ) == 'undefined' ){ Array.prototype.alert = function(){ if( typeof( Array.prototype.dump ) != 'function' ){ return false; } alert( this.dump() ); }; } /* * Syntax: Array().keyCase( [Boolean upperCase = false] ); * Return: Array(); * Failur: false; *****************************************************************/ if( typeof( Array.prototype.keyCase ) == 'undefined' ){ Array.prototype.keyCase = function( upperCase ){ var apply = (upperCase)?"toUpperCase":"toLowerCase"; var retn = []; for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ retn[k.toString()[apply]()] = this[k]; } } return retn; } } /* * Syntax: Array().chunk( Number size [, Boolean preserveKeys = false] ); * Return: Array(); * Failur: false; *****************************************************************/ if( typeof( Array.prototype.chunk ) == 'undefined' ){ Array.prototype.chunk = function( size, preserveKeys ){ size = parseInt( size, 10 ); if( isNaN( size ) || size < 0 ){ return false; } var retn = []; var count = 0; var index = 0; for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ if( typeof( retn[index] ) == 'undefined' ){ retn[index] = []; } retn[index][(preserveKeys)?k:count] = this[k]; if( ++count == size ){ index++; count = 0; } } } return retn; } } /* * Syntax: Array().combine( Array _array [, Boolean arrayKeys = false] ); * Return: Array(); * Failur: false; *****************************************************************/ if( typeof( Array.prototype.combine ) == 'undefined' ){ Array.prototype.combine = function( _array, arrayKeys ){ if( !( typeof( _array ) == 'object' && _array instanceof Array ) || this.length != _array.length || this.length == 0 ){ return false; } var retn = []; var arr1 = []; var arr2 = []; var a = (arrayKeys)?_array:this; for( var k in a ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ arr1[arr1.length] = a[k]; } } a = (arrayKeys)?this:_array; for( var k in a ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ arr2[arr2.length] = a[k]; } } for( var i = 0; i < arr1.length; i++ ){ retn[arr1[i]] = arr2[i]; } return retn; } } /* * Syntax: Array().countValues( void ); * Return: Array(); *****************************************************************/ if( typeof( Array.prototype.countValues ) == 'undefined' ){ Array.prototype.countValues = function(){ var retn = []; for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ if( typeof( retn[this[k]] ) == 'undefined' ){ retn[this[k]] = 0; } retn[this[k]]++; } } return retn; } } /* * Syntax: Array().diff( Array _array [, Boolean strict = false] ); * Return: Array(); * Failur: false; * Rquars: Array().contains; *****************************************************************/ if( typeof( Array.prototype.diff ) == 'undefined' ){ Array.prototype.diff = function( _array, strict ){ if( !( typeof( _array ) == 'object' && _array instanceof Array ) || typeof( Array.prototype.contains ) != 'function' ){ return false; } var retn = []; var undef; for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ if( !_array.contains( this[k], (strict)?k:undef ) ){ retn[k] = this[k]; } } } return retn; } } /* * Syntax: Array().fill( [Number start = 0],[Number count = Start], Mixed val ); * Return: Array(); *****************************************************************/ if( typeof( Array.prototype.fill ) == 'undefined' ){ Array.prototype.fill = function( start, count, val ){ if( typeof( start ) != 'number' ){ start = 0; } if( typeof( count ) != 'number' ){ count = start; } var retn = this; for( count += start; start < count; start++ ){ retn[start] = val; } return retn; } } /* * Syntax: Array().fillKeys( Array keys, Mixed val ); * Return: Array(); * Failur: false; *****************************************************************/ if( typeof( Array.prototype.fillKeys ) == 'undefined' ){ Array.prototype.fillKeys = function( keys, val ){ if( !( typeof( keys ) == 'object' && keys instanceof Array ) ){ return false; } var retn = this; for( var k in keys ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ retn[keys[k]] = val; } } return retn; } } /* * Syntax: Array().flip( void ); * Return: Array(); *****************************************************************/ if( typeof( Array.prototype.flip ) == 'undefined' ){ Array.prototype.flip = function(){ var retn = []; for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ retn[this[k]] = k; } } return retn; } } /* * Syntax: Array().unique( void ); * Return: Array(); * Failur: false; * Rquars: Array().flip; *****************************************************************/ if( typeof( Array.prototype.unique ) == 'undefined' ){ Array.prototype.unique = function(){ if( typeof( Array.prototype.flip ) != 'function' ){ return false; } return this.flip().flip(); } } /* * Syntax: Array().keyExists( String key ); * Return: Boolean; *****************************************************************/ if( typeof( Array.prototype.keyExists ) == 'undefined' ){ Array.prototype.keyExists = function( key ){ return ( typeof(this[key]) != 'undefined' ); } } /* * Syntax: Array().search( Mixed needle ); * Return: String; * Failur: false; *****************************************************************/ if( typeof( Array.prototype.search ) == 'undefined' ){ Array.prototype.search = function( needle ){ for( var k in this ){ if( typeof( Array.prototype[k] ) == 'undefined' ){ if( this[k] === needle ){ return k; } } } return false; } } /* * Syntax: Array().empty( void ); * Return: self; *****************************************************************/ if( typeof( Array.prototype.empty ) == 'undefined' ){ Array.prototype.empty = function(){ this.length = 0; return this; } }