Skip to content

Commit

Permalink
Add Hash Table for future implementation "find by".
Browse files Browse the repository at this point in the history
Add Chaining children arrays;
  • Loading branch information
Denys-Bushulyak committed Feb 3, 2015
1 parent 3b1a6d1 commit 58140d0
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 165 deletions.
176 changes: 103 additions & 73 deletions chain.js
Original file line number Diff line number Diff line change
@@ -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
};
}
Loading

0 comments on commit 58140d0

Please sign in to comment.