diff --git a/source/Ox/js/Collection.js b/source/Ox/js/Collection.js index f5a5613c..bfcbbd47 100644 --- a/source/Ox/js/Collection.js +++ b/source/Ox/js/Collection.js @@ -343,6 +343,41 @@ Ox.min = function(collection) { return ret; }; +/*@ +Ox.remove Removes an element from an array or object and returns it + (collection, element) -> <*> Element, or undefined if not found + + > Ox.remove(Ox.test.collection[0], 'b') + 'b' + > Ox.remove(Ox.test.collection[1], 1) + 1 + > Ox.remove(Ox.test.collection[1], 3) + void 0 + > Ox.test.collection + [['a', 'c'], {a: 0, c: 2}] +@*/ +Ox.remove = function(collection, element) { + var ret, key; + if (Ox.isArray(collection)) { + key = collection.indexOf(element); + if (key > -1) { + ret = collection.splice(key, 1)[0]; + } + } else { + key = Ox.keyOf(collection, element); + if (key) { + ret = collection[key]; + delete collection[key]; + } + } + return ret; +}; + /*@ Ox.reverse Reverses an array or string > Ox.reverse([1, 2, 3])