|
1 | | -primitives.common.LinkedHashItems = function () { |
| 1 | +/** |
| 2 | + * Creates linked hash list collection. |
| 3 | + * @returns {LinkedHashItems} Returns linked hash list structure |
| 4 | + */ |
| 5 | +primitives.common.LinkedHashItems = function () { |
2 | 6 | var segmentsHash = {}, |
3 | 7 | nextKeys = {}, |
4 | 8 | prevKeys = {}, |
5 | 9 | startSegmentKey = null, |
6 | 10 | endSegmentKey = null; |
7 | 11 |
|
| 12 | + /** |
| 13 | + * Adds new item to collection |
| 14 | + * @param {string} key The new item key |
| 15 | + * @param {object} item The new item context object value |
| 16 | + */ |
8 | 17 | function add(key, item) { |
9 | 18 | if (segmentsHash.hasOwnProperty(key)) { |
10 | 19 | throw "Duplicate segments are not supported!"; |
|
21 | 30 | endSegmentKey = key; |
22 | 31 | } |
23 | 32 |
|
| 33 | + /** |
| 34 | + * Checks if collection is empty |
| 35 | + * |
| 36 | + * @returns {boolean} Returns true if collection is empty |
| 37 | + */ |
24 | 38 | function isEmpty() { |
25 | 39 | return startSegmentKey == null; |
26 | 40 | } |
27 | 41 |
|
| 42 | + /** |
| 43 | + * Item context object |
| 44 | + * |
| 45 | + * @param {string} key The item's key |
| 46 | + * @returns {object} Returns context object |
| 47 | + */ |
28 | 48 | function item(key) { |
29 | 49 | return segmentsHash[key]; |
30 | 50 | } |
31 | 51 |
|
| 52 | + /** |
| 53 | + * Gets next key |
| 54 | + * |
| 55 | + * @param {string} key The item key |
| 56 | + * @returns {string} Returns key of the next collection item |
| 57 | + */ |
32 | 58 | function nextKey(key) { |
33 | 59 | return nextKeys[key]; |
34 | 60 | } |
35 | 61 |
|
| 62 | + /** |
| 63 | + * Gets previous key |
| 64 | + * |
| 65 | + * @param {string} key The item key |
| 66 | + * @returns {string} Returns key of the previous collection item |
| 67 | + */ |
36 | 68 | function prevKey(key) { |
37 | 69 | return prevKeys[key]; |
38 | 70 | } |
39 | 71 |
|
| 72 | + /** |
| 73 | + * First collection item key |
| 74 | + * |
| 75 | + * @returns {string} Returns the key of the first item in the collection |
| 76 | + */ |
40 | 77 | function startKey() { |
41 | 78 | return startSegmentKey; |
42 | 79 | } |
43 | 80 |
|
| 81 | + /** |
| 82 | + * Last collection item key |
| 83 | + * |
| 84 | + * @returns {string} Returns key of the last item in the collection |
| 85 | + */ |
44 | 86 | function endKey() { |
45 | 87 | return endSegmentKey; |
46 | 88 | } |
47 | 89 |
|
| 90 | + /** |
| 91 | + * Adds new item to the head of the list |
| 92 | + * |
| 93 | + * @param {string} key The new item key |
| 94 | + * @param {object} item The new item context object value |
| 95 | + * @returns {string} Returns key of the last item in the collection |
| 96 | + */ |
48 | 97 | function unshift(key, item) { |
49 | 98 | if (segmentsHash.hasOwnProperty(key)) { |
50 | 99 | throw "Duplicate segments are not supported!"; |
|
61 | 110 | startSegmentKey = key; |
62 | 111 | } |
63 | 112 |
|
| 113 | + /** |
| 114 | + * Inserts new item into the list after the given key |
| 115 | + * |
| 116 | + * @param {string} afterKey The key that the new element is placed after |
| 117 | + * @param {string} key The new item key |
| 118 | + * @param {object} item The new item context object value |
| 119 | + */ |
64 | 120 | function insertAfter(afterKey, key, item) { |
65 | 121 | if (segmentsHash.hasOwnProperty(key)) { |
66 | 122 | throw "Duplicate segments are not supported!"; |
|
82 | 138 | } |
83 | 139 | } |
84 | 140 |
|
| 141 | + /** |
| 142 | + * Inserts new item into the list before the given key |
| 143 | + * |
| 144 | + * @param {string} beforeKey The key that the new element is placed before |
| 145 | + * @param {string} key The new item key |
| 146 | + * @param {object} item The new item context object value |
| 147 | + */ |
85 | 148 | function insertBefore(beforeKey, key, item) { |
86 | 149 | if (segmentsHash.hasOwnProperty(key)) { |
87 | 150 | throw "Duplicate segments are not supported!"; |
|
97 | 160 | } |
98 | 161 | } |
99 | 162 |
|
| 163 | + /** |
| 164 | + * Removes item |
| 165 | + * @param {string} key The key of the item |
| 166 | + */ |
100 | 167 | function remove(key) { |
101 | 168 | var prevKey = prevKeys[key], |
102 | 169 | nextKey = nextKeys[key]; |
|
118 | 185 | delete prevKeys[key]; |
119 | 186 | } |
120 | 187 |
|
| 188 | + /** |
| 189 | + * Empties collection |
| 190 | + */ |
121 | 191 | function empty() { |
122 | 192 | segmentsHash = {}; |
123 | 193 | nextKeys = {}; |
|
152 | 222 | } |
153 | 223 | } |
154 | 224 |
|
| 225 | + /** |
| 226 | + * Appends one list to another |
| 227 | + * |
| 228 | + * @param {LinkedHashItems} list A list to append to the end of the current list |
| 229 | + */ |
155 | 230 | function attach(list) { |
156 | 231 | list.iterate(function (segment, key) { |
157 | 232 | add(key, segment); |
158 | 233 | }); |
159 | 234 | } |
160 | 235 |
|
| 236 | + /** |
| 237 | + * Callback function for iterating list items |
| 238 | + * |
| 239 | + * @callback onLinkedHashItemsCallback |
| 240 | + * @param {object} item The item context object |
| 241 | + * @param {string} key The item key |
| 242 | + * @returns {boolean} Returns true to break the iteration process |
| 243 | + */ |
| 244 | + |
| 245 | + /** |
| 246 | + * Loops items of the collection |
| 247 | + * @param {onLinkedHashItemsCallback} onItem Callback function for iterating collection items |
| 248 | + * @param {string} startKey The key to start iteration from |
| 249 | + * @param {string} endKey The key to end iteration at |
| 250 | + */ |
161 | 251 | function iterate(onItem, startKey, endKey) { |
162 | 252 | _iterate(true, onItem, startKey, endKey); |
163 | 253 | } |
164 | 254 |
|
| 255 | + /** |
| 256 | + * Loops items of the collection backward |
| 257 | + * @param {onLinkedHashItemsCallback} onItem Callback function for iterating collection items |
| 258 | + * @param {string} startKey The key to start iteration from |
| 259 | + * @param {string} endKey The key to end iteration at |
| 260 | + */ |
165 | 261 | function iterateBack(onItem, startKey, endKey) { |
166 | 262 | _iterate(false, onItem, startKey, endKey); |
167 | 263 | } |
168 | 264 |
|
| 265 | + /** |
| 266 | + * Validates internal data consistensy of the structure |
| 267 | + * @returns {boolean} Returns true if it pass validation |
| 268 | + */ |
169 | 269 | function validate(info) { |
170 | 270 | var key, prevKey, nextKey; |
171 | 271 | for (key in segmentsHash) { |
|
221 | 321 | return true; |
222 | 322 | } |
223 | 323 |
|
| 324 | + /** |
| 325 | + * Returns a regular javascript array of collection items |
| 326 | + * |
| 327 | + * @returns{object[]} Returns array containing items of the collection |
| 328 | + */ |
224 | 329 | function toArray() { |
225 | 330 | var result = []; |
226 | 331 |
|
|
0 commit comments