From f07a4c9e21ab3e09807f2954898e2e6187f46885 Mon Sep 17 00:00:00 2001 From: Denys-Bushulyak Date: Sun, 8 Feb 2015 20:57:40 +0200 Subject: [PATCH] Add except function --- bower.json | 7 ----- chain.js | 78 ++++++++++++++++++++++++++++++++++++++-------------- test/test.js | 15 +++++++++- 3 files changed, 71 insertions(+), 29 deletions(-) diff --git a/bower.json b/bower.json index a159fd7..4edb8dc 100644 --- a/bower.json +++ b/bower.json @@ -2,13 +2,6 @@ "name" : "chain.js", "main" : "chain.js", "description": "Function wrap a array into a chain.", - "moduleType" : [ - "amd", - "es6", - "globals", - "node", - "yui" - ], "keywords" : [ "[\"chain\"", "\"circle\"", diff --git a/chain.js b/chain.js index 85a5d5e..415fa9a 100644 --- a/chain.js +++ b/chain.js @@ -15,6 +15,12 @@ function Chain(items) { } } + function log(message, arg_2, arg_3, arg_n) { + if (Chain.debug) { + console && console.debug(message, arguments); + } + } + function _next() { _index++; @@ -83,9 +89,9 @@ function Chain(items) { return _items; } - function _count(){ + function _count() { var length = 0; - for(var i in _items){ + for (var i in _items) { length++; } return length; @@ -102,32 +108,62 @@ function Chain(items) { return _current(); } - function _slice(start, length){ + function _slice(start, length) { var out = []; length = length || Number.POSITIVE_INFINITY; - for(var i in _items){ - if(i >= start && i <= length){ + for (var i in _items) { + if (i >= start && i <= length) { out.push(_items[i]); } } return new Chain(out); } - return { - next : _next, - prev : _prev, - current : _current, - first : _first, - last : _last, - reset : _reset, - isEnd : _isEnd, - isBegin : _isBegin, - goTo : _goTo, - goToEnd : _goToEnd, - getItems : _getItems, - beginFrom : _beginFrom, - getIndex : _getIndex, - slice : _slice, - count : _count + function _except(itemIndex) { + var out = []; + + itemIndex = Array.isArray(itemIndex) ? itemIndex : [itemIndex]; + + for (var i in itemIndex) { + + log("Excepting index: ", i); + + for (var j in _items) { + + log("Item index: ", j); + + if (j != itemIndex[i]) { + + log("Compare: ", j != itemIndex[i]); + + out.push(_items[j]); + } + } + } + + log("Result", out); + + return new Chain(out); + } + + var instance = { + next : _next, + prev : _prev, + current : _current, + first : _first, + last : _last, + reset : _reset, + isEnd : _isEnd, + isBegin : _isBegin, + goTo : _goTo, + goToEnd : _goToEnd, + getItems : _getItems, + beginFrom: _beginFrom, + getIndex : _getIndex, + slice : _slice, + count : _count, + except : _except }; + + return instance; } \ No newline at end of file diff --git a/test/test.js b/test/test.js index 04a58ca..0d8714d 100644 --- a/test/test.js +++ b/test/test.js @@ -20,7 +20,20 @@ describe("Testing Chain", function () { it("expecting than last item would be null", function(){ expect(chain.last()).toBe(null); - }) + }); + }); + + describe("Testing exception function",function(){ + + Chain.debug = true; + var chain = new Chain(arr); + chain.debug = true; + + it("expecting than new chain will not contain '3'", function(){ + expect(chain.except(2).count()).toBe(4); + expect(chain.except(2).goTo(2)).toBe(-2); + }); + }); describe("Testing slice function",function(){