Array Overrides
17. April 2008 – 18:43On the way of development of my project I came to need to have set methods for javascript Array object especially intersection. Googling has shown up some but they were not LGPL, the licence what I need, so I’ve written them.
The following override contains:
- Ext message box input type configuration
- Conditional Ext.overrideIf
- New Array methods:
- copy – one dimensional copy method
- indexOf – index of a value within the array. New browsers may already implement this method, therefore conditional override.
- lastIndexOf – last index of a value within the array. May be already implemented by browser too.
- intersect – returns intersection of arrays, for example:
[1,3,5,7].intersect([2,3,8,7,5])returns[3,5,7] - union – returns union of arrays, for example:
[1,3,5,7].union([2,3,8,7,5])returns[1,2,3,5,7,8] - unique – removes duplicate values from the array and returns result as new array
- Ext.ux.clone – deep object or array cloning function
Overrides code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | // vim: ts=4:sw=4:nu:fdc=4:nospell /** * Ext.ux.Overrides * * This files contains various fixes and/or overridesds: * * 1. inputType selection for Ext.Msg.show * 2. Ext.overrideIf conditional override * 3. Array methods: indexOf, lastIndexOf, copy, unique, intersect, union * 4. Cloning function * * @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 */ // {{{ // Ext.Msg input type and firefox cursor bug fix Ext.Msg.show = Ext.Msg.show.createInterceptor(function(config) { // get inputType var inputType = config.inputType || 'text'; // get dialog content element var content = Ext.Msg.getDialog().body.child('div.ext-mb-content'); // set inputType var input = content.child('input'); if(input) { input.set({type:inputType}); } // this seems to be already handled in Ext // fix firefox cursor bug // var overflow = config.cursorFix ? 'auto' : 'hidden'; // if(Ext.isGecko) { // content.applyStyles({overflow:overflow}); // } }, Ext.Msg); // }}} // {{{ // 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 // }}} }); // }}} // {{{ // object and array cloning function /** * Clone Function * @return {Object/Array} Deep clone of an object or an array */ Ext.ux.clone = function(o) { if(!o || 'object' !== typeof o) { return o; } var c = 'function' === typeof o.pop ? [] : {}; var p, v; for(p in o) { if(o.hasOwnProperty(p)) { v = o[p]; if(v && 'object' === typeof v) { c[p] = Ext.ux.clone(v); } else { c[p] = v; } } } return c; }; // eo function clone // }}} // eof |
2 Responses to “Array Overrides”
Do you also have a String overrider?…
By Lucian on Apr 18, 2008
Not yet, I didn’t need any so far.
By jsakalos on Apr 18, 2008