jsakalos
17 Apr 2008, 6:30 AM
I needed some additional methods for Array object and I haven't found them googling (better to say I found, bug they were GPL and I needed LGPL) so I've written them. Maybe also somebody else needs them....
// vim: ts=4:sw=4:nu:fdc=4:nospell
/**
* Ext.ux.Overrides
*
* This files contains various fixes and/or overridesds:
*
* @author Ing. Jozef Sakalos
* @version $Id: Ext.ux.Overrides.js 158 2008-04-10 00:03:18Z jozo $
* @date 13. March 2008
*
* @license Ext.ux.Overrides is licensed under the terms of
* the Open Source LGPL 3.0 license. Commercial use is permitted to the extent
* that the code/component(s) do NOT become part of another Open Source or Commercially
* licensed development library or toolkit without explicit permission.
*
* License details: http://www.gnu.org/licenses/lgpl.html
*/
/*global Ext */
// {{{
// conditional override
/**
* Same as Ext.override but overrides only if method doesn not exist in target class
*/
Ext.overrideIf = function(origclass, overrides) {
if(overrides) {
var p = origclass.prototype;
for(var method in overrides) {
if(!p[method]) {
p[method] = overrides[method];
}
}
}
};
// }}}
// {{{
// methods for Array object
Ext.overrideIf(Array, {
// {{{
/**
* One dimensional copy
* @return {Array} New array that is copy of this
*/
copy:function() {
var a = [];
for(var i = 0, l = this.length; i < l; i++) {
a.push(this[i]);
}
return a;
} // eo function copy
// }}}
// {{{
/**
* @return {Integer} index of v or -1 if not found
* @param {Mixed} v Value to find indexOf
* @param {Integer} b Starting index
*/
,indexOf:function(v, b) {
for(var i = +b || 0, l = this.length; i < l; i++) {
if(this[i] === v) {
return i;
}
}
return -1;
} // eo function indexOf
// }}}
// {{{
/**
* @return {Array} intersection of this and passed arguments
*/
,intersect:function() {
if(!arguments.length) {
return [];
}
var a1 = this, a2, a;
for(var k = 0, ac = arguments.length; k < ac; k++) {
a = [];
a2 = arguments[k] || [];
for(var i = 0, l = a1.length; i < l; i++) {
if(-1 < a2.indexOf(a1[i])) {
a.push(a1[i]);
}
}
a1 = a;
}
return a.unique();
} // eo function intesect
// }}}
// {{{
/**
* @return {Integer} index of v or -1 if not found
* @param {Mixed} v Value to find indexOf
* @param {Integer} b Starting index
*/
,lastIndexOf:function(v, b) {
b = +b || 0;
var i = this.length;
while(i-- > b) {
if(this[i] === v) {
return i;
}
}
return -1;
} // eof function lastIndexOf
// }}}
// {{{
/**
* @return {Array} New array that is union of this and passed arguments
*/
,union:function() {
var a = this.copy(), a1;
for(var k = 0, ac = arguments.length; k < ac; k++) {
a1 = arguments[k] || [];
for(var i = 0, l = a1.length; i < l; i++) {
a.push(a1[i]);
}
}
return a.unique();
} // eo function union
// }}}
// {{{
/**
* Removes duplicates from array
* @return {Array} new array with duplicates removed
*/
,unique:function() {
var a = [], i, l = this.length;
for(i = 0; i < l; i++) {
if(a.indexOf(this[i]) < 0) {
a.push(this[i]);
}
}
return a;
} // eo function unique
// }}}
});
// }}}
// eof
// vim: ts=4:sw=4:nu:fdc=4:nospell
/**
* Ext.ux.Overrides
*
* This files contains various fixes and/or overridesds:
*
* @author Ing. Jozef Sakalos
* @version $Id: Ext.ux.Overrides.js 158 2008-04-10 00:03:18Z jozo $
* @date 13. March 2008
*
* @license Ext.ux.Overrides is licensed under the terms of
* the Open Source LGPL 3.0 license. Commercial use is permitted to the extent
* that the code/component(s) do NOT become part of another Open Source or Commercially
* licensed development library or toolkit without explicit permission.
*
* License details: http://www.gnu.org/licenses/lgpl.html
*/
/*global Ext */
// {{{
// conditional override
/**
* Same as Ext.override but overrides only if method doesn not exist in target class
*/
Ext.overrideIf = function(origclass, overrides) {
if(overrides) {
var p = origclass.prototype;
for(var method in overrides) {
if(!p[method]) {
p[method] = overrides[method];
}
}
}
};
// }}}
// {{{
// methods for Array object
Ext.overrideIf(Array, {
// {{{
/**
* One dimensional copy
* @return {Array} New array that is copy of this
*/
copy:function() {
var a = [];
for(var i = 0, l = this.length; i < l; i++) {
a.push(this[i]);
}
return a;
} // eo function copy
// }}}
// {{{
/**
* @return {Integer} index of v or -1 if not found
* @param {Mixed} v Value to find indexOf
* @param {Integer} b Starting index
*/
,indexOf:function(v, b) {
for(var i = +b || 0, l = this.length; i < l; i++) {
if(this[i] === v) {
return i;
}
}
return -1;
} // eo function indexOf
// }}}
// {{{
/**
* @return {Array} intersection of this and passed arguments
*/
,intersect:function() {
if(!arguments.length) {
return [];
}
var a1 = this, a2, a;
for(var k = 0, ac = arguments.length; k < ac; k++) {
a = [];
a2 = arguments[k] || [];
for(var i = 0, l = a1.length; i < l; i++) {
if(-1 < a2.indexOf(a1[i])) {
a.push(a1[i]);
}
}
a1 = a;
}
return a.unique();
} // eo function intesect
// }}}
// {{{
/**
* @return {Integer} index of v or -1 if not found
* @param {Mixed} v Value to find indexOf
* @param {Integer} b Starting index
*/
,lastIndexOf:function(v, b) {
b = +b || 0;
var i = this.length;
while(i-- > b) {
if(this[i] === v) {
return i;
}
}
return -1;
} // eof function lastIndexOf
// }}}
// {{{
/**
* @return {Array} New array that is union of this and passed arguments
*/
,union:function() {
var a = this.copy(), a1;
for(var k = 0, ac = arguments.length; k < ac; k++) {
a1 = arguments[k] || [];
for(var i = 0, l = a1.length; i < l; i++) {
a.push(a1[i]);
}
}
return a.unique();
} // eo function union
// }}}
// {{{
/**
* Removes duplicates from array
* @return {Array} new array with duplicates removed
*/
,unique:function() {
var a = [], i, l = this.length;
for(i = 0; i < l; i++) {
if(a.indexOf(this[i]) < 0) {
a.push(this[i]);
}
}
return a;
} // eo function unique
// }}}
});
// }}}
// eof