-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLinkedHashItems.js
More file actions
361 lines (331 loc) · 8.72 KB
/
LinkedHashItems.js
File metadata and controls
361 lines (331 loc) · 8.72 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* Creates linked hash list collection.
* @class LinkedHashItems
*
* @returns {LinkedHashItems} Returns linked hash list structure
*/
export default function LinkedHashItems() {
var segmentsHash = {},
nextKeys = {},
prevKeys = {},
startSegmentKey = null,
endSegmentKey = null;
/**
* Adds new item to collection
* @param {string} key The new item key
* @param {object} item The new item context object value
*/
function add(key, item) {
if (segmentsHash.hasOwnProperty(key)) {
throw "Duplicate segments are not supported!";
}
segmentsHash[key] = item;
nextKeys[key] = null;
if (endSegmentKey == null) {
startSegmentKey = key;
prevKeys[key] = null;
} else {
nextKeys[endSegmentKey] = key;
prevKeys[key] = endSegmentKey;
}
endSegmentKey = key;
}
/**
* Checks if collection is empty
*
* @returns {boolean} Returns true if collection is empty
*/
function isEmpty() {
return startSegmentKey == null;
}
/**
* Item context object
*
* @param {string} key The item's key
* @returns {object} Returns context object
*/
function item(key) {
return segmentsHash[key];
}
/**
* Gets next key
*
* @param {string} key The item key
* @returns {string} Returns key of the next collection item
*/
function nextKey(key) {
return nextKeys[key];
}
/**
* Gets previous key
*
* @param {string} key The item key
* @returns {string} Returns key of the previous collection item
*/
function prevKey(key) {
return prevKeys[key];
}
/**
* First collection item key
*
* @returns {string} Returns the key of the first item in the collection
*/
function startKey() {
return startSegmentKey;
}
/**
* Last collection item key
*
* @returns {string} Returns key of the last item in the collection
*/
function endKey() {
return endSegmentKey;
}
/**
* Adds new item to the head of the list
*
* @param {string} key The new item key
* @param {object} item The new item context object value
* @returns {string} Returns key of the last item in the collection
*/
function unshift(key, item) {
if (segmentsHash.hasOwnProperty(key)) {
throw "Duplicate segments are not supported!";
}
segmentsHash[key] = item;
prevKeys[key] = null;
if (startSegmentKey == null) {
endSegmentKey = key;
nextKeys[key] = null;
} else {
prevKeys[startSegmentKey] = key;
nextKeys[key] = startSegmentKey;
}
startSegmentKey = key;
}
/**
* Inserts new item into the list after the given key
*
* @param {string} afterKey The key that the new element is placed after
* @param {string} key The new item key
* @param {object} item The new item context object value
*/
function insertAfter(afterKey, key, item) {
if (segmentsHash.hasOwnProperty(key)) {
throw "Duplicate segments are not supported!";
}
if (afterKey == null) {
unshift(key, item);
} else {
var nextKey = nextKeys[afterKey];
if (nextKey == null) {
add(key, item);
} else {
segmentsHash[key] = item;
nextKeys[afterKey] = key;
nextKeys[key] = nextKey;
prevKeys[nextKey] = key;
prevKeys[key] = afterKey;
}
}
}
/**
* Inserts new item into the list before the given key
*
* @param {string} beforeKey The key that the new element is placed before
* @param {string} key The new item key
* @param {object} item The new item context object value
*/
function insertBefore(beforeKey, key, item) {
if (segmentsHash.hasOwnProperty(key)) {
throw "Duplicate segments are not supported!";
}
if (beforeKey == null || !segmentsHash.hasOwnProperty(beforeKey)) {
throw "Before key should be defined!";
}
var prevKey = prevKeys[beforeKey];
if (prevKey == null) {
unshift(key, item);
} else {
insertAfter(prevKey, key, item)
}
}
/**
* Removes item
* @param {string} key The key of the item
*/
function remove(key) {
var prevKey = prevKeys[key],
nextKey = nextKeys[key];
if (prevKey != null) {
nextKeys[prevKey] = nextKey;
} else {
startSegmentKey = nextKey;
}
if (nextKey != null) {
prevKeys[nextKey] = prevKey;
} else {
endSegmentKey = prevKey;
}
delete segmentsHash[key];
delete nextKeys[key];
delete prevKeys[key];
}
/**
* Empties collection
*/
function empty() {
segmentsHash = {};
nextKeys = {};
prevKeys = {};
startSegmentKey = null;
endSegmentKey = null;
}
function _iterate(forward, onItem, startKey, endKey) {
var key = startKey,
segment;
if (key == null) {
key = forward ? startSegmentKey : endSegmentKey;
}
if (onItem != null) {
while (key != null) {
segment = segmentsHash[key];
if (segment != null) {
if (onItem(segment, key)) {
return;
}
}
if (key == endKey) {
key = null;
} else {
key = forward ? nextKeys[key] : prevKeys[key];
}
}
}
}
/**
* Appends one list to another
*
* @param {LinkedHashItems} list A list to append to the end of the current list
*/
function attach(list) {
list.iterate(function (segment, key) {
add(key, segment);
});
}
/**
* Callback function for iterating list items
*
* @callback onLinkedHashItemsCallback
* @param {object} item The item context object
* @param {string} key The item key
* @returns {boolean} Returns true to break the iteration process
*/
/**
* Loops items of the collection
* @param {onLinkedHashItemsCallback} onItem Callback function for iterating collection items
* @param {string} startKey The key to start iteration from
* @param {string} endKey The key to end iteration at
*/
function iterate(onItem, startKey, endKey) {
_iterate(true, onItem, startKey, endKey);
}
/**
* Loops items of the collection backward
* @param {onLinkedHashItemsCallback} onItem Callback function for iterating collection items
* @param {string} startKey The key to start iteration from
* @param {string} endKey The key to end iteration at
*/
function iterateBack(onItem, startKey, endKey) {
_iterate(false, onItem, startKey, endKey);
}
/**
* Validates internal data consistency of the structure
* @returns {boolean} Returns true if it pass validation
*/
function validate(info) {
var key, prevKey, nextKey;
for (key in segmentsHash) {
if (segmentsHash.hasOwnProperty(key)) {
if (!nextKeys.hasOwnProperty(key) || !prevKeys.hasOwnProperty(key)) {
if (info != null) {
info.message = "Orphant key found!";
}
return false;
}
}
}
if (!segmentsHash.hasOwnProperty(startSegmentKey) || !segmentsHash.hasOwnProperty(endSegmentKey)) {
if (info != null) {
info.message = "Start or end values are missing!";
}
return false;
}
for (key in nextKeys) {
if (nextKeys.hasOwnProperty(key)) {
if (!segmentsHash.hasOwnProperty(key) || !prevKeys.hasOwnProperty(key)) {
if (info != null) {
info.message = "Orphant key found!";
}
return false;
}
nextKey = nextKeys[key];
if (nextKey && !nextKeys.hasOwnProperty(nextKey)) {
if (info != null) {
info.message = "Next key not found!";
}
return false;
}
}
}
for (key in prevKeys) {
if (prevKeys.hasOwnProperty(key)) {
if (!segmentsHash.hasOwnProperty(key) || !nextKeys.hasOwnProperty(key)) {
if (info != null) {
info.message = "Orphant key found!";
}
return false;
}
prevKey = prevKeys[key];
if (prevKey && !prevKeys.hasOwnProperty(prevKey)) {
if (info != null) {
info.message = "Prev key not found!";
}
return false;
}
}
}
return true;
}
/**
* Returns a regular javascript array of collection items
*
* @returns {object[]} Returns array containing items of the collection
*/
function toArray() {
var result = [];
iterate(function (item) {
result.push(item);
});
return result;
}
return {
add: add,
item: item,
nextKey: nextKey,
prevKey: prevKey,
startKey: startKey,
endKey: endKey,
unshift: unshift,
insertAfter: insertAfter,
insertBefore: insertBefore,
remove: remove,
isEmpty: isEmpty,
attach: attach,
iterate: iterate,
iterateBack: iterateBack,
empty: empty,
toArray: toArray,
validate: validate
};
};