From 58140d07b11895baff1d2882ac0dd0d739627f25 Mon Sep 17 00:00:00 2001 From: Denys-Bushulyak Date: Tue, 3 Feb 2015 14:05:44 +0200 Subject: [PATCH] Add Hash Table for future implementation "find by". Add Chaining children arrays; --- chain.js | 176 ++++++++++++++++++++++----------------- test/test.js | 227 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 238 insertions(+), 165 deletions(-) diff --git a/chain.js b/chain.js index b67bddc..d305953 100644 --- a/chain.js +++ b/chain.js @@ -1,100 +1,130 @@ function Chain(items) { + "use strict"; - var _index = 0; + var _index = 0; - var _items = items.slice(0) || []; + var _items = items.slice(0) || []; + var _hashTable = []; - function _next() { - _index++; + //Wraping all arrays at items into chain + for (var index in _items) { - _index = _index > _items.length - 1 ? 0 : _index; + var hash = getHash(_items[index]); + _hashTable[hash] = _items[index]; - return _items[_index]; - } + for (var i in _items[index]) { + if (Array.isArray(_items[index][i])) { + _items[index][i] = Chain(_items[index][i]); + } + } + } - function _prev() { + function getHash(obj) { + var hash = btoa(JSON.stringify(obj)); - _index--; + return hash.substr(Math.random() * 100, 5); + } - _index = _index < 0 ? _items.length - 1 : _index; + function _next() { + _index++; - return _items[_index]; - } + _index = _index > _items.length - 1 ? 0 : _index; - function _first(item) { + return _items[_index]; + } - if (item) { - _items[0] = item; - } + function _prev() { - return _items[0] || undefined; - } + _index--; - function _last(item) { - if (item) { - _items[_items.length - 1] = item; - } + _index = _index < 0 ? _items.length - 1 : _index; - return _items[_items.length - 1]; - } + return _items[_index]; + } - function _reset() { - _index = 0; - return _current(); - } + function _first(item) { - function _isBegin() { - return _index === 0; - } + if (item) { + _items[0] = item; + } - function _isEnd() { - return _index == (_items.length - 1); - } + return _items[0] || undefined; + } - function _current() { - return _items[_index]; - } + function _last(item) { + if (item) { + _items[_items.length - 1] = item; + } - function _beginFrom(index) { - var newArray = _items.slice(index); + return _items[_items.length - 1]; + } - for (var i = index; i < _items.length; i++) { - newArray.push(_items[i]); - } - return Chain(newArray); - } + function _reset() { + _index = 0; + return _current(); + } - function _getIndex() { - return _index; - } + function _isBegin() { + return _index === 0; + } - function _getItems() { - return _items; - } + function _isEnd() { + return _index == (_items.length - 1); + } - function _goTo(index) { - _index = index; - return _current(); - } + function _current() { + return _items[_index]; + } - function _goToEnd() { - _index = _items.length - 1; - return _current(); - } + function _beginFrom(index) { + var newArray = _items.slice(index); - 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 - }; + for (var i = index; i < _items.length; i++) { + newArray.push(_items[i]); + } + return Chain(newArray); + } + + function _getIndex(item) { + if (item) { + var hash = generateHash(item); + + } + return _index; + } + + function _getItems() { + return _items; + } + + function _goTo(index) { + _index = index; + return _current(); + } + + function _goToEnd() { + _index = _items.length - 1; + return _current(); + } + + function _getHashTable() { + return _hashTable; + } + + 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, + getHashTable: _getHashTable + }; } \ No newline at end of file diff --git a/test/test.js b/test/test.js index 0e15323..f3d46ba 100644 --- a/test/test.js +++ b/test/test.js @@ -1,127 +1,170 @@ describe("Testing Chain", function () { - var arr = ['a', 1, 3, -2, null]; + var arr = ['a', 1, 3, -2, null]; - describe("Shift of begin", function () { + describe("Shift of begin", function () { - var chain = Chain(arr).beginFrom(3); + var chain = (new Chain(arr)).beginFrom(3); - it("expecting shift to -2", function () { - expect(-2).toBe(chain.current()); - expect(0).toBe(chain.getIndex()); - }); - }); + it("expecting shift to -2", function () { + expect(-2).toBe(chain.current()); + expect(0).toBe(chain.getIndex()); + }); + }); - describe("Items equality", function () { + describe("Items equality", function () { - var chain = Chain(arr); + var chain = new Chain(arr); - it("contains items as expected", function () { - expect(arr.length).toBe(chain.getItems().length); - }); + it("contains items as expected", function () { + expect(arr.length).toBe(chain.getItems().length); + }); - it("item returned from object Chain equal the item from array with the same index", function () { - var looped_count = 1; + it("item returned from object Chain equal the item from array with the same index", function () { + var looped_count = 1; - do { - var item = chain.current(); + do { + var item = chain.current(); - expect(arr[chain.getIndex()]).toBe(item); + expect(arr[chain.getIndex()]).toBe(item); - chain.next(); + chain.next(); - looped_count++; + looped_count++; - } while (!chain.isBegin()); - }); - }); + } while (!chain.isBegin()); + }); + }); - describe("moving by chain", function () { + describe("moving by chain", function () { - var chain = null; + var chain = null; - beforeEach(function () { - chain = Chain(arr); - }); + beforeEach(function () { + chain = new Chain(arr); + }); - it("expecting return the first element of array", function () { - expect(chain.first()).toBe(arr[0]) - }); + it("expecting return the first element of array", function () { + expect(chain.first()).toBe(arr[0]) + }); - it("expecting return the last element of array", function () { - expect(chain.last()).toBe(arr[arr.length - 1]) - }); + it("expecting return the last element of array", function () { + expect(chain.last()).toBe(arr[arr.length - 1]) + }); - it("expecting no changing index on tacking first element of chain", function () { - chain.last(); - expect(chain.getIndex()).toBe(0); - chain.next(); - expect(chain.getIndex()).not.toBe(0); - expect(1).toBe(chain.getIndex()); - chain.last(); - expect(chain.getIndex()).not.toBe(arr.length - 1); - }); + it("expecting no changing index on tacking first element of chain", function () { + chain.last(); + expect(chain.getIndex()).toBe(0); + chain.next(); + expect(chain.getIndex()).not.toBe(0); + expect(1).toBe(chain.getIndex()); + chain.last(); + expect(chain.getIndex()).not.toBe(arr.length - 1); + }); + + it("expecting moving down index after prev()", function () { + chain.next(); + + expect(1).toBe(chain.getIndex()); + chain.next(); - it("expecting moving down index after prev()", function () { - chain.next(); + expect(2).toBe(chain.getIndex()); + chain.prev(); - expect(1).toBe(chain.getIndex()); - chain.next(); + expect(1).toBe(chain.getIndex()); + }); - expect(2).toBe(chain.getIndex()); - chain.prev(); + it("expecting to take last index of chain after prev after init", function () { + var item = chain.prev(); + var lastIndexOfArray = arr.length - 1; - expect(1).toBe(chain.getIndex()); - }); + expect(chain.last()).toBe(item) + expect(chain.getIndex()).toBe(lastIndexOfArray); + }); - it("expecting to take last index of chain after prev after init", function () { - var item = chain.prev(); - var lastIndexOfArray = arr.length - 1; + it("expecting to take first index of chain after next after last() after init", function () { + var lastIndexOfArray = arr.length - 1; - expect(chain.last()).toBe(item) - expect(chain.getIndex()).toBe(lastIndexOfArray); - }); + for (var i = 1; i <= lastIndexOfArray; i++) { + chain.next(); + } - it("expecting to take first index of chain after next after last() after init", function () { - var lastIndexOfArray = arr.length - 1; + var item = chain.next(); - for (var i = 1; i <= lastIndexOfArray; i++) { - chain.next(); - } + expect(chain.first()).toBe(item) + expect(chain.getIndex()).toBe(0); + }); - var item = chain.next(); + it("expecting going to set index", function () { + chain.goTo(2); + expect(chain.current()).toBe(3); + }) + }); - expect(chain.first()).toBe(item) - expect(chain.getIndex()).toBe(0); - }); + describe("scope of chain", function () { + var chain = new Chain(arr); - it("expecting going to set index", function () { - chain.goTo(2); - expect(chain.current()).toBe(3); - }) - }); + it("expecting True from isBegin()", function () { + expect(chain.isBegin()).toBe(true); + expect(chain.isEnd()).not.toBe(true); + }); - describe("scope of chain", function () { - var chain = Chain(arr); + it("expecting False from isBegin() after next() ", function () { + chain.next(); + expect(chain.isBegin()).toBe(false); + expect(chain.isEnd()).not.toBe(true); + }); + + it("expecting True from isEnd after next() ", function () { + + var item = chain.goTo(chain.getItems().length - 1); + + expect(item).toBe(null); + expect(chain.isBegin()).toBe(false); + expect(chain.isEnd()).toBe(true); + }); + }) +}); + +describe("Testing hash table", function () { + var arr = [ + { + name: "Jack", age: 20, portfolio: [ + {year: 2000, skills: ['php', 'js', 'html']}, + {year: 2010, skills: ['java', 'c++', 'iOS']} + ] + }, + { + name: "Sally", age: 24, portfolio: [ + {years: 2003, skills: ['iOS', 'Android', 'yaml']}, + {years: 2004, skills: ['WP', 'Windows', 'MacOS']}, + {years: 2005, skills: ['python', 'Bash', 'Linux']} + ] + } + ]; + + describe("Wraping all arrays", function () { + + it('expecting than first brand has 2 collections', function () { + var chain = new Chain(arr); + expect(chain.getItems().length).toBe(2); + expect(chain.first().name).toBe(arr[0].name); + expect(chain.last().name).toBe(arr[1].name); + expect(chain.next().name).toBe(arr[1].name); + expect(chain.reset().name).toBe(arr[0].name); + expect(chain.first().portfolio.first().year).toBe(2000); + expect(chain.first().portfolio.next().year).toBe(2010); + expect(chain.first().portfolio.next().year).toBe(2000); + expect(chain.first().portfolio.first().skills.goTo(1)).toBe('js'); + expect(chain.first().portfolio.first().skills.goTo(0)).toBe('php'); + expect(chain.next().name).toBe('Sally'); + expect(chain.next().name).toBe('Jack'); + expect(chain.next().age).toBe(24); + expect(chain.current().portfolio.getItems().length).toBe(3); + }); + + + }); - it("expecting True from isBegin()", function () { - expect(chain.isBegin()).toBe(true); - expect(chain.isEnd()).not.toBe(true); - }); - it("expecting False from isBegin() after next() ", function () { - chain.next(); - expect(chain.isBegin()).toBe(false); - expect(chain.isEnd()).not.toBe(true); - }); - - it("expecting True from isEnd after next() ", function () { - - var item = chain.goTo(chain.getItems().length - 1); - - expect(item).toBe(null); - expect(chain.isBegin()).toBe(false); - expect(chain.isEnd()).toBe(true); - }); - }) }); \ No newline at end of file