-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
162 lines (160 loc) · 3.48 KB
/
index.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
function ListItem(content){
this.link = null;
this.content = content;
}
ListItem.prototype.destroy = function(){
this.content = null;
this.link = null;
};
ListItem.prototype.linkTo = function(item){
if(item){
if(item.link){
this.link = item.link;
}
item.link = this;
}
};
ListItem.prototype.empty = function(){
return 'undefined' === typeof this.content;
};
ListItem.prototype.apply = function(func){
if('undefined' !== typeof this.content){
return func(this.content,this);
}else{
return false;
}
};
function defaultSorter(){
return false;
}
function List(sortfunction){
this.head = new ListItem();
this.sorter = sortfunction || defaultSorter;
this.length = 0;
}
List.prototype.destroy = function(){
this.purge();
this.length = null;
this.sorter = null;
this.head = null;
};
List.prototype.purge = function(){
var item = this.head, ditem;
while(item){
ditem = item;
item = item.link;
ditem.destroy();
}
this.head.content = void 0;
this.length=0;
};
List.prototype.empty = function(){
return !this.head.content;
};
List.prototype.add = function(content,afteritem){
if(this.head.empty()){
this.head.content = content;
this.length = 1;
return this.head;
}
var newitem = new ListItem(content);
var item = afteritem || this.lastItemToSatisfy(this.sorter.bind(null,content));
if(!item){
this.head.linkTo(newitem);
this.head = newitem;
}else{
newitem.linkTo(item);
}
this.length++;
return newitem;
};
List.prototype.removeOne = function(listitem){
var item = this.head, previtem;
while(item){
if(item===listitem){
if(item===this.head){
if(item.link){
this.head = item.link;
item.destroy();
}else{
this.head.content = void 0;
}
}else{
previtem.link = item.link;
item.destroy();
}
this.length--;
return;
}
previtem = item;
item = item.link;
}
};
List.prototype.findOne = function(criterionfunction){
var item = this.firstItemToSatisfy(criterionfunction);
if(item){
return item.content;
}
};
List.prototype.firstItemToSatisfy = function(func){
var check=false, item = this.head;
while(!check&&item){
check = item.apply(func);
if('boolean' !== typeof check){
throw 'func needs to return a boolean value';
}
if(check){
return item;
}else{
item = item.link;
}
}
return item;
};
List.prototype.lastItemToSatisfy = function(func){
var check, item = this.head, ret;
while(item){
check = item.apply(func);
if('boolean' !== typeof check){
throw 'func needs to return a boolean value';
}
if(!check){
return ret;
}else{
ret = item;
item = item.link;
}
}
return ret;
};
List.prototype.traverse = function(func){
var item = this.head;
while(item){
item.apply(func);
item = item.link;
}
};
List.prototype.traverseConditionally = function(func){
var result, item = this.head;
while(item){
result = item.apply(func);
if('undefined' !== typeof result){
return result;
}
item = item.link;
}
};
List.prototype.dumpToConsole = function(){
this.traverse(console.log.bind(console));
};
function drainer(arry,countobj,content){
arry[countobj.count] = content;
countobj.count++;
}
List.prototype.drain = function(){
var ret = new Array(this.length),countobj={count:0};
this.traverse(drainer.bind(null,ret,countobj));
this.purge();
return ret;
};
module.exports = List;