-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedObject.js
More file actions
93 lines (86 loc) · 1.75 KB
/
LinkedObject.js
File metadata and controls
93 lines (86 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* LinkedObject
*
* @version 1.0.0
*
* @author m13p4
* @copyright Meliantchenkov Pavel
*/
function LinkedObject()
{
var obj = {}, linkedObject, first = {p: false, n: false}, last = first, c;
function _set(key, value)
{
var toSet = obj[key];
if("undefined" === typeof obj[key])
{
_push(key, value);
}
else toSet[2] = value;
}
function _push(key, value)
{
var toPush = {
p: last, //prev
n: false, //next
v: value, //value
k: key //key
};
last.n = toPush;
last = toPush;
obj[key] = toPush;
}
function _get(key)
{
var toGet = obj[key];
if("undefined" !== typeof toGet)
return toGet.v;
}
function _current()
{
return c.v;
}
function _start()
{
c = first;
}
function _end()
{
c = {p: last, n: false};
}
function _next()
{
c = c.n;
return c !== false;
}
function _prev()
{
c = c.p;
return c !== false && c !== first;
}
function _for(callback)
{
if("function" === typeof callback)
{
var current = first.n;
while(current)
{
callback(current.k, current.v);
current = current.n;
}
}
}
linkedObject = {
set: _set,
push: _push,
get: _get,
start: _start,
end: _end,
next: _next,
prev: _prev,
current: _current,
for: _for
};
return linkedObject;
}
if("undefined" !== typeof module) module.exports = LinkedObject;