add Ox.getIndex and Ox.getObject; Ox.List: use Ox.getIndex when updating item value

This commit is contained in:
rolux 2014-04-30 14:19:09 +02:00
commit 62c65026b1
2 changed files with 18 additions and 6 deletions

View file

@ -494,6 +494,13 @@ Ox.flatten = function(array) {
return ret;
};
// FIXME: add docs and tests
Ox.getIndex = function(array, key, value) {
return Ox.indexOf(array, function(obj) {
return obj[key] === value;
});
};
/*@
Ox.getIndexById <f> Returns the first array index of an object with a given id
(array, id) -> <n> Index (or `-1`)
@ -505,9 +512,13 @@ Ox.getIndexById <f> Returns the first array index of an object with a given id
-1
@*/
Ox.getIndexById = function(array, id) {
return Ox.indexOf(array, function(obj) {
return obj.id === id;
});
return Ox.getIndex(array, 'id', id);
};
// FIXME: add docs and tests
Ox.getObject = function(array, key, value) {
var index = Ox.getIndex(array, key, value);
return index > -1 ? array[index] : null;
};
/*@
@ -521,8 +532,7 @@ Ox.getObjectById <f> Returns the first object in an array with a given id
null
@*/
Ox.getObjectById = function(array, id) {
var index = Ox.getIndexById(array, id);
return index > -1 ? array[index] : null;
return Ox.getObject(array, 'id', id);
};
/*