From 4b01e4984b2a812e5c149f7952c2134050bf24b4 Mon Sep 17 00:00:00 2001 From: rolux Date: Sat, 25 May 2013 14:24:39 +0200 Subject: [PATCH] add Ox.remove: sugar for array.splice(array.indexOf(element), 1) --- source/Ox/js/Collection.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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])