forked from 0x2620/oxjs
misc updates to ox.js
This commit is contained in:
parent
d64e39c5b2
commit
2ef642fdeb
11 changed files with 136 additions and 93 deletions
|
|
@ -188,16 +188,19 @@ Ox.forEach = function(col, fn, includePrototype) {
|
|||
};
|
||||
|
||||
/*@
|
||||
Ox.getObjectById <f> Returns an array element with a given id
|
||||
> Ox.getObjectById([{id: "foo", title: "Foo"}, {id: "bar", title: "Bar"}], "foo")
|
||||
{id: "foo", title: "Foo"}
|
||||
Ox.getIndex <f> Returns the first array index of an object where obj[key] is val
|
||||
> Ox.getIndex([{a: 1}, {a: 2}, {a: 1}], 'a', 2)
|
||||
1
|
||||
> Ox.getIndex([{a: 1}, {a: 2}, {a: 1}], 'a', 1)
|
||||
0
|
||||
> Ox.getIndex([{a: 1}, {a: 2}, {a: 1}], 'a', 0)
|
||||
-1
|
||||
@*/
|
||||
// fixme: should this be getElementById() ?
|
||||
Ox.getObjectById = function(arr, id) {
|
||||
var ret = null;
|
||||
Ox.forEach(arr, function(v) {
|
||||
if (v.id == id) {
|
||||
ret = v;
|
||||
Ox.getIndex = function(arr, key, val) {
|
||||
var ret = -1;
|
||||
Ox.forEach(arr, function(obj, ind) {
|
||||
if (obj[key] === val) {
|
||||
ret = ind;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
|
@ -205,23 +208,43 @@ Ox.getObjectById = function(arr, id) {
|
|||
};
|
||||
|
||||
/*@
|
||||
Ox.getPositionById <f> Returns the index of an array element with a given id
|
||||
> Ox.getPositionById([{id: "foo", title: "Foo"}, {id: "bar", title: "Bar"}], "foo")
|
||||
Ox.getIndexById <f> Returns the first array index of an object with a given id
|
||||
> Ox.getIndexById([{id: 'foo', str: 'Foo'}, {id: 'bar', str: 'Bar'}], 'foo')
|
||||
0
|
||||
@*/
|
||||
// fixme: this should be getIndexById()
|
||||
Ox.getPositionById = function(arr, id) {
|
||||
var ret = -1;
|
||||
Ox.forEach(arr, function(v, i) {
|
||||
if (v.id == id) {
|
||||
ret = i;
|
||||
// FIXME: this should be getIndexById() only
|
||||
Ox.getIndexById = Ox.getPositionById = function(arr, id) {
|
||||
return Ox.getIndex(arr, 'id', id);
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.getObject <f> Returns the first object in an array where obj[key] is val
|
||||
> Ox.getObject([{a: 1, i: 0}, {a: 2, i: 1}, {a: 1, i: 2}], 'a', 2)
|
||||
{a: 2, i: 1}
|
||||
> Ox.getObject([{a: 1, i: 0}, {a: 2, i: 1}, {a: 1, i: 2}], 'a', 1)
|
||||
{a: 1, i: 0}
|
||||
> Ox.getObject([{a: 1, i: 0}, {a: 2, i: 1}, {a: 1, i: 2}], 'a', 0)
|
||||
null
|
||||
@*/
|
||||
Ox.getObject = function(arr, key, val) {
|
||||
var ret = null;
|
||||
Ox.forEach(arr, function(obj) {
|
||||
if (obj[key] === val) {
|
||||
ret = obj;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return ret;
|
||||
};
|
||||
|
||||
// fixme: and what about getElementBy() and getIndexBy() ?
|
||||
/*@
|
||||
Ox.getObjectById <f> Returns the first object in an array with a given id
|
||||
> Ox.getObjectById([{id: 'foo', str: 'Foo'}, {id: 'bar', str: 'Bar'}], 'foo')
|
||||
{id: "foo", str: "Foo"}
|
||||
@*/
|
||||
Ox.getObjectById = function(arr, id) {
|
||||
return Ox.getObject(arr, 'id', id);
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.getset <f> Generic getter and setter function
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue