diff --git a/lib/OpenLayers/BaseTypes.js b/lib/OpenLayers/BaseTypes.js index cd8b830738..17287cbd0c 100644 --- a/lib/OpenLayers/BaseTypes.js +++ b/lib/OpenLayers/BaseTypes.js @@ -349,3 +349,58 @@ if (!Function.prototype.bindAsEventListener) { return OpenLayers.Function.bindAsEventListener(this, object); }; } + +/********************* + * * + * ARRAY * + * * + *********************/ + +OpenLayers.Array = { + + /** + * APIMethod: OpenLayers.Array.filter + * Filter an array. Provides the functionality of the + * Array.prototype.filter extension to the ECMA-262 standard. Where + * available, Array.prototype.filter will be used. + * + * Based on well known example from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter + * + * Parameters: + * array - {Array} The array to be filtered. This array is not mutated. + * Elements added to this array by the callback will not be visited. + * callback - {Function} A function that is called for each element in + * the array. If this function returns true, the element will be + * included in the return. The function will be called with three + * arguments: the element in the array, the index of that element, and + * the array itself. If the optional caller parameter is specified + * the callback will be called with this set to caller. + * caller - {Object} Optional object to be set as this when the callback + * is called. + * + * Returns: + * {Array} An array of elements from the passed in array for which the + * callback returns true. + */ + filter: function(array, callback, caller) { + var selected = []; + if (Array.prototype.filter) { + selected = array.filter(callback, caller); + } else { + var len = array.length; + if (typeof callback != "function") { + throw new TypeError(); + } + for(var i=0; i)} A list of controls matching the given criteria. + * An empty array is returned if no matches are found. + */ + getControlsBy: function(property, match) { + var test = (typeof match.test == "function"); + var found = OpenLayers.Array.filter(this.controls, function(item) { + return item[property] == match || (test && match.test(item[property])); + }); + return found; + }, + + /** + * APIMethod: getControlsByName + * Get a list of contorls with names matching the given name. + * + * Parameter: + * match - {String | Object} A control name. The name can also be a regular + * expression literal or object. In addition, it can be any object + * with a method named test. For reqular expressions or other, if + * name.test(control.name) evaluates to true, the control will be included + * in the list of controls returned. If no controls are found, an empty + * array is returned. + * + * Returns: + * {Array()} A list of controls matching the given name. + * An empty array is returned if no matches are found. + */ + getControlsByName: function(match) { + return this.getControlsBy("name", match); + }, + + /** + * APIMethod: getControlsByClass + * Get a list of controls of a given type (CLASS_NAME). + * + * Parameter: + * match - {String | Object} A control class name. The type can also be a + * regular expression literal or object. In addition, it can be any + * object with a method named test. For reqular expressions or other, + * if type.test(control.CLASS_NAME) evaluates to true, the control will + * be included in the list of controls returned. If no controls are + * found, an empty array is returned. + * + * Returns: + * {Array()} A list of controls matching the given type. + * An empty array is returned if no matches are found. + */ + getControlsByClass: function(match) { + return this.getControlsBy("CLASS_NAME", match); + }, + CLASS_NAME: "OpenLayers.Control.Panel" }); diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index eb9716e5bc..aaa496557f 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -475,19 +475,10 @@ OpenLayers.Map = OpenLayers.Class({ * criteria. */ getBy: function(array, property, match) { - var found = []; - var item; - var list = this[array]; var test = (typeof match.test == "function"); - if(list instanceof Array) { - for(var i=0; i 3;}}), + expected: [panel.controls[2], panel.controls[3]], + message: "(custom object) got controls with foo length greater than 3" + } + ]; + t.plan(cases.length); + for(var i=0; i diff --git a/tests/test_BaseTypes.html b/tests/test_BaseTypes.html index 1c18d8c3a6..1f9815a664 100644 --- a/tests/test_BaseTypes.html +++ b/tests/test_BaseTypes.html @@ -187,6 +187,40 @@ t.ok(g_X == window.event, "window.event properly passed as first argument when nothing specified"); } + function test_Array_filter(t) { + + t.plan(8); + + OpenLayers.Array.filter(["foo"], function(item, index, array) { + t.eq(item, "foo", "callback called with proper item"); + t.eq(index, 0, "callback called with proper index"); + t.eq(array, ["foo"], "callback called with proper array"); + t.eq(this, {"foo": "bar"}, "callback called with this set properly"); + }, {"foo": "bar"}); + + var array = [0, 1, 2, 3]; + var select = OpenLayers.Array.filter(array, function(value) { + return value > 1; + }); + t.eq(select, [2, 3], "filter works for basic callback"); + t.eq(array, [0, 1, 2, 3], "filter doesn't modify original"); + + var obj = { + test: function(value) { + if(value > 1) { + return true; + } + } + }; + var select = OpenLayers.Array.filter(array, function(value) { + return this.test(value); + }, obj); + t.eq(select, [2, 3], "filter works for callback and caller"); + t.eq(array, [0, 1, 2, 3], "filter doesn't modify original"); + + + } +