Adding OpenLayers.Array.filter to mimic Array.prototype.filter. Adding panel.getControlsBy and related methods that use filter. Reworking map.getBy to use filter as well. r=elemoine (closes #1203)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@5532 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -47,6 +47,63 @@
|
||||
"activated the other tool control, the first one is inactive and the toggle control still active.");
|
||||
}
|
||||
|
||||
function test_Control_Panel_getBy(t) {
|
||||
|
||||
var panel = {
|
||||
getBy: OpenLayers.Control.Panel.prototype.getBy,
|
||||
getControlsBy: OpenLayers.Control.Panel.prototype.getControlsBy,
|
||||
controls: [
|
||||
{foo: "foo", id: Math.random()},
|
||||
{foo: "bar", id: Math.random()},
|
||||
{foo: "foobar", id: Math.random()},
|
||||
{foo: "foo bar", id: Math.random()},
|
||||
{foo: "foo", id: Math.random()}
|
||||
]
|
||||
};
|
||||
|
||||
var cases = [
|
||||
{
|
||||
got: panel.getControlsBy("foo", "foo"),
|
||||
expected: [panel.controls[0], panel.controls[4]],
|
||||
message: "(string literal) got two controls matching foo"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", "bar"),
|
||||
expected: [panel.controls[1]],
|
||||
message: "(string literal) got one control matching foo"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", "barfoo"),
|
||||
expected: [],
|
||||
message: "(string literal) got empty array for no foo match"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", /foo/),
|
||||
expected: [panel.controls[0], panel.controls[2], panel.controls[3], panel.controls[4]],
|
||||
message: "(regexp literal) got three controls containing string"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", /foo$/),
|
||||
expected: [panel.controls[0], panel.controls[4]],
|
||||
message: "(regexp literal) got three controls ending with string"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", /\s/),
|
||||
expected: [panel.controls[3]],
|
||||
message: "(regexp literal) got control containing space"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", new RegExp("BAR", "i")),
|
||||
expected: [panel.controls[1], panel.controls[2], panel.controls[3]],
|
||||
message: "(regexp object) got layers ignoring case"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", {test: function(str) {return str.length > 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<cases.length; ++i) {
|
||||
t.eq(cases[i].got, cases[i].expected, cases[i].message);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -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");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user