collect-your-stuff 2.1.2 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -3926
- package/browser/collect-your-stuff.js +912 -549
- package/browser/collect-your-stuff.min.js +1 -1
- package/dist/collections/arrayable/ArrayElement.d.ts +6 -11
- package/dist/collections/arrayable/ArrayElement.js +5 -7
- package/dist/collections/arrayable/Arrayable.d.ts +22 -37
- package/dist/collections/arrayable/Arrayable.js +208 -223
- package/dist/collections/doubly-linked-list/DoubleLinker.d.ts +9 -14
- package/dist/collections/doubly-linked-list/DoubleLinker.js +71 -73
- package/dist/collections/doubly-linked-list/DoublyLinkedList.d.ts +19 -36
- package/dist/collections/doubly-linked-list/DoublyLinkedList.js +310 -327
- package/dist/collections/linked-list/LinkedList.d.ts +20 -36
- package/dist/collections/linked-list/LinkedList.js +283 -296
- package/dist/collections/linked-list/Linker.d.ts +8 -13
- package/dist/collections/linked-list/Linker.js +82 -84
- package/dist/collections/linked-tree-list/LinkedTreeList.d.ts +24 -43
- package/dist/collections/linked-tree-list/LinkedTreeList.js +274 -294
- package/dist/collections/linked-tree-list/TreeLinker.d.ts +14 -20
- package/dist/collections/linked-tree-list/TreeLinker.js +91 -94
- package/dist/collections/queue/Queue.d.ts +10 -14
- package/dist/collections/queue/Queue.js +114 -121
- package/dist/collections/queue/Queueable.d.ts +13 -22
- package/dist/collections/queue/Queueable.js +142 -148
- package/dist/collections/queue/TaskQueue.d.ts +8 -17
- package/dist/collections/queue/TaskQueue.js +128 -137
- package/dist/collections/scoped-collection/ScopedCollection.d.ts +42 -0
- package/dist/collections/scoped-collection/ScopedCollection.js +81 -0
- package/dist/collections/scoped-collection/ScopedCollection.min.js +1 -0
- package/dist/collections/stack/Stack.d.ts +11 -15
- package/dist/collections/stack/Stack.js +122 -129
- package/dist/collections/stack/Stackable.d.ts +9 -16
- package/dist/collections/stack/Stackable.js +81 -85
- package/dist/collections/stack/TaskStack.d.ts +8 -17
- package/dist/collections/stack/TaskStack.js +98 -107
- package/dist/main.d.ts +67 -68
- package/dist/main.js +8 -4
- package/dist/main.min.js +1 -1
- package/dist/recipes/ArrayIterator.d.ts +4 -4
- package/dist/recipes/ArrayIterator.js +4 -4
- package/dist/recipes/DoubleLinkerIterator.d.ts +3 -3
- package/dist/recipes/DoubleLinkerIterator.js +3 -3
- package/dist/recipes/IsArrayable.d.ts +13 -33
- package/dist/recipes/IsQueue.d.ts +4 -9
- package/dist/recipes/IsScopedCollection.d.ts +27 -0
- package/dist/recipes/IsScopedCollection.js +5 -0
- package/dist/recipes/IsScopedCollection.min.js +1 -0
- package/dist/recipes/IsStack.d.ts +4 -9
- package/dist/recipes/IsTreeNode.d.ts +1 -1
- package/dist/recipes/LinkerIterator.d.ts +3 -3
- package/dist/recipes/LinkerIterator.js +3 -3
- package/dist/recipes/Runnable.d.ts +3 -16
- package/dist/recipes/Runnable.js +3 -9
- package/dist/recipes/TreeLinkerIterator.d.ts +4 -4
- package/dist/recipes/TreeLinkerIterator.js +36 -36
- package/dist/recipes/recipes.d.ts +1 -4
- package/dist/recipes/recipes.js +19 -22
- package/dist/services/parseTree.d.ts +2 -3
- package/dist/services/parseTree.js +23 -24
- package/dist/services/parseTreeNext.d.ts +2 -3
- package/dist/services/parseTreeNext.js +2 -3
- package/dist/services/services.d.ts +1 -4
- package/dist/services/services.js +19 -22
- package/package.json +4 -4
|
@@ -1,296 +1,283 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', {
|
|
4
|
-
value: true
|
|
5
|
-
})
|
|
6
|
-
exports.LinkedList = void 0
|
|
7
|
-
const _Linker = require('./Linker')
|
|
8
|
-
const _LinkerIterator = require('../../recipes/LinkerIterator')
|
|
9
|
-
const _Arrayable = require('../arrayable/Arrayable')
|
|
10
|
-
/**
|
|
11
|
-
* LinkedList represents a collection stored as a LinkedList with next references.
|
|
12
|
-
* @extends Arrayable
|
|
13
|
-
*/
|
|
14
|
-
class LinkedList {
|
|
15
|
-
/**
|
|
16
|
-
* Create the new LinkedList instance.
|
|
17
|
-
* @param
|
|
18
|
-
*/
|
|
19
|
-
constructor (linkerClass = _Linker.Linker) {
|
|
20
|
-
/** The class used to create this instance, so that it can be recognized as valid without an instanceof check. */
|
|
21
|
-
this.classType = LinkedList
|
|
22
|
-
/** The first linker of the list (null when the list is empty), from which the whole list is reached. */
|
|
23
|
-
this.innerList = null
|
|
24
|
-
/** Whether the inner list has been initialized (it can only be initialized once). */
|
|
25
|
-
this.initialized = false
|
|
26
|
-
/** The last linker, remembered so that adding to the end does not need to walk the whole list (null when not known yet). */
|
|
27
|
-
this.tailCache = null
|
|
28
|
-
/** The number of linkers, kept up to date by the list's own methods so that the length does not need to walk the whole list (null when not known yet). */
|
|
29
|
-
this.countCache = null
|
|
30
|
-
this.linkerClass = linkerClass
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Initialize the inner list, should only run once.
|
|
35
|
-
* @param
|
|
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
|
-
newNode.next =
|
|
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
|
-
* @param
|
|
172
|
-
* @returns
|
|
173
|
-
*/
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
if (
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
this.
|
|
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
|
-
* Convert an array to a LinkedList.
|
|
287
|
-
* @param {Array} values An array of values which will be converted to linkers in this linked-list
|
|
288
|
-
* @param {IsLinker} linkerClass The class to use for each linker
|
|
289
|
-
* @param {IsArrayable<Linker>} [classType=LinkedList] Provide the type of IsArrayable to use.
|
|
290
|
-
* @returns {LinkedList}
|
|
291
|
-
*/
|
|
292
|
-
exports.LinkedList = LinkedList
|
|
293
|
-
LinkedList.fromArray = (values = [], linkerClass = _Linker.Linker, classType = LinkedList) => {
|
|
294
|
-
const list = new classType(linkerClass)
|
|
295
|
-
return list.initialize(linkerClass.fromArray(values).head)
|
|
296
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true
|
|
5
|
+
})
|
|
6
|
+
exports.LinkedList = void 0
|
|
7
|
+
const _Linker = require('./Linker')
|
|
8
|
+
const _LinkerIterator = require('../../recipes/LinkerIterator')
|
|
9
|
+
const _Arrayable = require('../arrayable/Arrayable')
|
|
10
|
+
/**
|
|
11
|
+
* LinkedList represents a collection stored as a LinkedList with next references.
|
|
12
|
+
* @extends Arrayable
|
|
13
|
+
*/
|
|
14
|
+
class LinkedList {
|
|
15
|
+
/**
|
|
16
|
+
* Create the new LinkedList instance.
|
|
17
|
+
* @param linkerClass The class used to wrap given data as linkers.
|
|
18
|
+
*/
|
|
19
|
+
constructor (linkerClass = _Linker.Linker) {
|
|
20
|
+
/** The class used to create this instance, so that it can be recognized as valid without an instanceof check. */
|
|
21
|
+
this.classType = LinkedList
|
|
22
|
+
/** The first linker of the list (null when the list is empty), from which the whole list is reached. */
|
|
23
|
+
this.innerList = null
|
|
24
|
+
/** Whether the inner list has been initialized (it can only be initialized once). */
|
|
25
|
+
this.initialized = false
|
|
26
|
+
/** The last linker, remembered so that adding to the end does not need to walk the whole list (null when not known yet). */
|
|
27
|
+
this.tailCache = null
|
|
28
|
+
/** The number of linkers, kept up to date by the list's own methods so that the length does not need to walk the whole list (null when not known yet). */
|
|
29
|
+
this.countCache = null
|
|
30
|
+
this.linkerClass = linkerClass
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Initialize the inner list, should only run once.
|
|
35
|
+
* @param initialList Give the list of linkers to start in this linked-list.
|
|
36
|
+
*/
|
|
37
|
+
initialize (initialList) {
|
|
38
|
+
// Borrowed from Arrayable, which types its return as an Arrayable although it returns whatever list called it
|
|
39
|
+
return _Arrayable.Arrayable.prototype.initialize.call(this, initialList)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Retrieve the innerList used (the list itself, not a copy).
|
|
44
|
+
*/
|
|
45
|
+
get list () {
|
|
46
|
+
return this.innerList
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Retrieve the first Linker in the list.
|
|
51
|
+
*/
|
|
52
|
+
get first () {
|
|
53
|
+
return this.innerList
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Retrieve the last Linker in the list. The end is remembered, so this does not walk the list.
|
|
58
|
+
*/
|
|
59
|
+
get last () {
|
|
60
|
+
if (this.innerList === null) {
|
|
61
|
+
return null
|
|
62
|
+
}
|
|
63
|
+
let tail = this.tailCache !== null ? this.tailCache : this.innerList
|
|
64
|
+
// The remembered tail is normally the end already, walking on from it also finds anything linked on outside of this list
|
|
65
|
+
while (tail.next !== null) {
|
|
66
|
+
tail = tail.next
|
|
67
|
+
}
|
|
68
|
+
this.tailCache = tail
|
|
69
|
+
return tail
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Return the length of the list. It is kept up to date by the list's own methods, so this does not walk the list
|
|
74
|
+
* (call reset() after linkers were changed directly).
|
|
75
|
+
*/
|
|
76
|
+
get length () {
|
|
77
|
+
if (this.countCache === null) {
|
|
78
|
+
this.reset()
|
|
79
|
+
}
|
|
80
|
+
return this.countCache
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Insert a new node (or data) after a node.
|
|
85
|
+
* @param node The existing node as reference, or null to insert at the start of the list
|
|
86
|
+
* @param newNode The new node to go after the existing node
|
|
87
|
+
*/
|
|
88
|
+
insertAfter (node, newNode) {
|
|
89
|
+
newNode = this.linkerClass.make(newNode, this.linkerClass)
|
|
90
|
+
if (node === null || typeof node === 'undefined') {
|
|
91
|
+
// After nothing means at the start of the list
|
|
92
|
+
newNode.next = this.innerList
|
|
93
|
+
if (this.innerList === null) {
|
|
94
|
+
this.tailCache = newNode
|
|
95
|
+
}
|
|
96
|
+
this.innerList = newNode
|
|
97
|
+
} else {
|
|
98
|
+
newNode.next = node.next
|
|
99
|
+
node.next = newNode
|
|
100
|
+
if (newNode.next === null) {
|
|
101
|
+
this.tailCache = newNode
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (this.countCache !== null) {
|
|
105
|
+
++this.countCache
|
|
106
|
+
}
|
|
107
|
+
return this
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Insert a new node (or data) before a node.
|
|
112
|
+
* @param node The existing node as reference, or null to insert at the end of the list
|
|
113
|
+
* @param newNode The new node to go before the existing node
|
|
114
|
+
* @throws {Error} When the reference node is not in this list
|
|
115
|
+
*/
|
|
116
|
+
insertBefore (node, newNode) {
|
|
117
|
+
newNode = this.linkerClass.make(newNode, this.linkerClass)
|
|
118
|
+
if (node === null || typeof node === 'undefined') {
|
|
119
|
+
// Before nothing means at the end of the list
|
|
120
|
+
const tail = this.last
|
|
121
|
+
newNode.next = null
|
|
122
|
+
if (tail === null) {
|
|
123
|
+
this.innerList = newNode
|
|
124
|
+
} else {
|
|
125
|
+
tail.next = newNode
|
|
126
|
+
}
|
|
127
|
+
this.tailCache = newNode
|
|
128
|
+
} else {
|
|
129
|
+
let prevNode = null
|
|
130
|
+
let currentNode = this.first
|
|
131
|
+
while (currentNode !== null && currentNode !== node) {
|
|
132
|
+
prevNode = currentNode
|
|
133
|
+
currentNode = currentNode.next
|
|
134
|
+
}
|
|
135
|
+
if (currentNode === null) {
|
|
136
|
+
throw new Error('The reference node is not in this list.')
|
|
137
|
+
}
|
|
138
|
+
newNode.next = node
|
|
139
|
+
if (prevNode) {
|
|
140
|
+
prevNode.next = newNode
|
|
141
|
+
} else {
|
|
142
|
+
this.innerList = newNode
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (this.countCache !== null) {
|
|
146
|
+
++this.countCache
|
|
147
|
+
}
|
|
148
|
+
return this
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Add a node (or data) after the given (or last) node in the list.
|
|
153
|
+
* @param node The new node to add to the end of the list
|
|
154
|
+
* @param after The existing last node
|
|
155
|
+
*/
|
|
156
|
+
append (node, after = this.last) {
|
|
157
|
+
return this.insertAfter(after, node)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Add a node (or data) before the given (or first) node in the list.
|
|
162
|
+
* @param node The new node to add to the start of the list
|
|
163
|
+
* @param before The existing first node
|
|
164
|
+
*/
|
|
165
|
+
prepend (node, before = this.first) {
|
|
166
|
+
return this.insertBefore(before, node)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Remove a linker from this linked list.
|
|
171
|
+
* @param node The node we wish to remove (and it will be returned after removal)
|
|
172
|
+
* @returns The removed node, or null when it was not in this list (nothing is removed)
|
|
173
|
+
*/
|
|
174
|
+
remove (node) {
|
|
175
|
+
if (node === null || typeof node === 'undefined') {
|
|
176
|
+
return null
|
|
177
|
+
}
|
|
178
|
+
let prevNode = null
|
|
179
|
+
let currentNode = this.first
|
|
180
|
+
while (currentNode !== null && currentNode !== node) {
|
|
181
|
+
prevNode = currentNode
|
|
182
|
+
currentNode = currentNode.next
|
|
183
|
+
}
|
|
184
|
+
if (currentNode === null) {
|
|
185
|
+
// The node is not in this list, so there is nothing to remove
|
|
186
|
+
return null
|
|
187
|
+
}
|
|
188
|
+
if (prevNode) {
|
|
189
|
+
prevNode.next = node.next
|
|
190
|
+
} else {
|
|
191
|
+
this.innerList = node.next
|
|
192
|
+
}
|
|
193
|
+
if (this.tailCache === node) {
|
|
194
|
+
this.tailCache = prevNode
|
|
195
|
+
}
|
|
196
|
+
if (this.innerList === null) {
|
|
197
|
+
this.tailCache = null
|
|
198
|
+
}
|
|
199
|
+
if (this.countCache !== null) {
|
|
200
|
+
--this.countCache
|
|
201
|
+
}
|
|
202
|
+
return node
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Refresh the remembered end and length of the list by walking it once. The list's own methods keep these up to date,
|
|
207
|
+
* so this is only needed after linkers were changed directly (for example by setting next on a linker).
|
|
208
|
+
* @returns The first linker of the list
|
|
209
|
+
*/
|
|
210
|
+
reset () {
|
|
211
|
+
let count = 0
|
|
212
|
+
let tail = null
|
|
213
|
+
let current = this.innerList
|
|
214
|
+
while (current !== null) {
|
|
215
|
+
++count
|
|
216
|
+
tail = current
|
|
217
|
+
current = current.next
|
|
218
|
+
}
|
|
219
|
+
this.countCache = count
|
|
220
|
+
this.tailCache = tail
|
|
221
|
+
return this.innerList
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Retrieve a Linker item from this list by numeric index, otherwise return null.
|
|
226
|
+
* @param index The integer number for retrieving a node by position.
|
|
227
|
+
*/
|
|
228
|
+
item (index) {
|
|
229
|
+
if (index >= 0) {
|
|
230
|
+
let current = this.first
|
|
231
|
+
let currentIndex = -1
|
|
232
|
+
while (++currentIndex < index && current !== null) {
|
|
233
|
+
current = current.next
|
|
234
|
+
}
|
|
235
|
+
return currentIndex === index ? current : null
|
|
236
|
+
}
|
|
237
|
+
let current = this.first
|
|
238
|
+
let currentIndex = 0
|
|
239
|
+
const calculatedIndex = this.length + index
|
|
240
|
+
if (calculatedIndex < 0) {
|
|
241
|
+
return null
|
|
242
|
+
}
|
|
243
|
+
while (currentIndex < calculatedIndex && current !== null) {
|
|
244
|
+
current = current.next
|
|
245
|
+
++currentIndex
|
|
246
|
+
}
|
|
247
|
+
return currentIndex === calculatedIndex ? current : null
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Be able to run forEach on this LinkedList to iterate over the linkers.
|
|
252
|
+
* @param callback The function to call for-each linker
|
|
253
|
+
* @param thisArg Optional, 'this' reference
|
|
254
|
+
*/
|
|
255
|
+
forEach (callback, thisArg = this) {
|
|
256
|
+
let index = 0
|
|
257
|
+
let current = thisArg.first
|
|
258
|
+
while (current !== null) {
|
|
259
|
+
callback(current, index, thisArg)
|
|
260
|
+
current = current.next
|
|
261
|
+
++index
|
|
262
|
+
}
|
|
263
|
+
return thisArg
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Be able to iterate over this class.
|
|
268
|
+
*/
|
|
269
|
+
[Symbol.iterator] () {
|
|
270
|
+
return new _LinkerIterator.LinkerIterator(this.first)
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Convert an array to a LinkedList.
|
|
275
|
+
* @param values An array of values which will be converted to linkers in this linked-list
|
|
276
|
+
* @param linkerClass The class to use for each linker
|
|
277
|
+
* @param classType Provide the type of IsArrayable to use.
|
|
278
|
+
*/
|
|
279
|
+
exports.LinkedList = LinkedList
|
|
280
|
+
LinkedList.fromArray = (values = [], linkerClass = _Linker.Linker, classType = LinkedList) => {
|
|
281
|
+
const list = new classType(linkerClass)
|
|
282
|
+
return list.initialize(linkerClass.fromArray(values).head)
|
|
283
|
+
}
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @author Joshua Heagle <joshuaheagle@gmail.com>
|
|
4
|
-
* @version 1.1.0
|
|
5
|
-
* @memberOf module:collect-your-stuff
|
|
2
|
+
* linked list item.
|
|
6
3
|
*/
|
|
7
4
|
import { IsLinker } from '../../recipes/IsLinker';
|
|
8
5
|
/**
|
|
@@ -18,9 +15,9 @@ export declare class Linker implements IsLinker {
|
|
|
18
15
|
next: Linker | null;
|
|
19
16
|
/**
|
|
20
17
|
* Create the new Linker instance, provide the data and optionally give the next Linker.
|
|
21
|
-
* @param
|
|
22
|
-
* @param
|
|
23
|
-
* @param
|
|
18
|
+
* @param nodeData The settings for the new linker.
|
|
19
|
+
* @param nodeData.data The data to be stored in this linker
|
|
20
|
+
* @param nodeData.next The reference to the next linker if any
|
|
24
21
|
*/
|
|
25
22
|
constructor({ data, next }?: {
|
|
26
23
|
data?: any;
|
|
@@ -28,16 +25,14 @@ export declare class Linker implements IsLinker {
|
|
|
28
25
|
});
|
|
29
26
|
/**
|
|
30
27
|
* Make a new Linker from the data given if it is not already a valid Linker.
|
|
31
|
-
* @param
|
|
32
|
-
* @param
|
|
33
|
-
* @return {Linker}
|
|
28
|
+
* @param linker Return a valid Linker instance from given data, or even an already valid one.
|
|
29
|
+
* @param classType Provide the type of IsLinker to use.
|
|
34
30
|
*/
|
|
35
31
|
static make: (linker: Linker | any, classType?: any) => IsLinker | any;
|
|
36
32
|
/**
|
|
37
33
|
* Convert an array into Linker instances, return the head and tail Linkers.
|
|
38
|
-
* @param
|
|
39
|
-
* @param
|
|
40
|
-
* @returns {{head: Linker, tail: Linker}}
|
|
34
|
+
* @param values Provide an array of data that will be converted to a chain of linkers.
|
|
35
|
+
* @param classType Provide the type of IsLinker to use.
|
|
41
36
|
*/
|
|
42
37
|
static fromArray: (values?: Array<any>, classType?: any) => {
|
|
43
38
|
head: IsLinker;
|