Skip to content

Commit

Permalink
add count method
Browse files Browse the repository at this point in the history
add count method tests
  • Loading branch information
Denys-Bushulyak committed Feb 5, 2015
1 parent 2d9b751 commit 36de0bc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
22 changes: 21 additions & 1 deletion chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ function Chain(items) {
return _items;
}

function _count(){
var length = 0;
for(var i in _items){
length++;
}
return length;
}

function _goTo(index) {
index = parseInt(index);
_index = index < 0 ? _items.length - 1 : index;
Expand All @@ -94,6 +102,16 @@ function Chain(items) {
return _current();
}

function _slice(start, length){
var out = [];
for(var i in _items){
if(i >= start && i <= length){
out.push(_items[i]);
}
}
return new Chain(out);
}

return {
next : _next,
prev : _prev,
Expand All @@ -107,6 +125,8 @@ function Chain(items) {
goToEnd : _goToEnd,
getItems : _getItems,
beginFrom : _beginFrom,
getIndex : _getIndex
getIndex : _getIndex,
slice : _slice,
count : _count
};
}
26 changes: 24 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,40 @@ describe("Testing Chain", function () {

it("expecting shift to -2", function () {
expect(chain.current()).toBe(-2);
expect(chain.current().getIndex()).toBe(3);
});

it("expecting index would be 0", function () {
expect(chain.getIndex()).toBe(0);
});

it("expecting than array length would be 2", function(){
expect(chain.getItems().length).toBe(2);
expect(chain.getItems().length).toBe(4);
});

it("expecting than last item would be null", function(){
expect(chain.last()).toBe(null);
})
});

describe("Begin sub chain", function(){
var chain = new Chain(arr);

var sub_chain = chain.slice(2,4);

it("subchain must have 4 items ", function(){
expect(sub_chain.count()).toBe(3);
});

it("first item will be 1", function(){
expect(sub_chain.current()).toBe(3);
expect(sub_chain.first()).toBe(3);
});

it("subchain must have last items equal as -2", function(){
expect(sub_chain.last()).toBeNull();
});
});

describe("Items equality", function () {

var chain = new Chain(arr);
Expand Down

0 comments on commit 36de0bc

Please sign in to comment.