Skip to content

Commit

Permalink
Add except function
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys-Bushulyak committed Feb 8, 2015
1 parent 59b7a8d commit f07a4c9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 29 deletions.
7 changes: 0 additions & 7 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
Expand Down
78 changes: 57 additions & 21 deletions chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
15 changes: 14 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down

0 comments on commit f07a4c9

Please sign in to comment.