collect-your-stuff 1.0.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/LICENSE +674 -0
- package/README.md +1676 -0
- package/browser/collect-your-stuff.js +18987 -0
- package/browser/collect-your-stuff.min.js +1 -0
- package/dist/collections/arrayable/ArrayElement.js +95 -0
- package/dist/collections/arrayable/ArrayElement.min.js +1 -0
- package/dist/collections/arrayable/Arrayable.js +253 -0
- package/dist/collections/arrayable/Arrayable.min.js +1 -0
- package/dist/collections/doubly-linked-llist/DoubleLinker.js +90 -0
- package/dist/collections/doubly-linked-llist/DoubleLinker.min.js +1 -0
- package/dist/collections/doubly-linked-llist/DoublyLinkedList.js +234 -0
- package/dist/collections/doubly-linked-llist/DoublyLinkedList.min.js +1 -0
- package/dist/collections/linked-list/LinkedList.js +256 -0
- package/dist/collections/linked-list/LinkedList.min.js +1 -0
- package/dist/collections/linked-list/Linker.js +100 -0
- package/dist/collections/linked-list/Linker.min.js +1 -0
- package/dist/collections/linked-tree-list/LinkedTreeList.js +96 -0
- package/dist/collections/linked-tree-list/LinkedTreeList.min.js +1 -0
- package/dist/collections/linked-tree-list/TreeLinker.js +112 -0
- package/dist/collections/linked-tree-list/TreeLinker.min.js +1 -0
- package/dist/collections/queue/Queue.js +163 -0
- package/dist/collections/queue/Queue.min.js +1 -0
- package/dist/collections/queue/Queueable.js +177 -0
- package/dist/collections/queue/Queueable.min.js +1 -0
- package/dist/collections/stack/Stack.js +141 -0
- package/dist/collections/stack/Stack.min.js +1 -0
- package/dist/collections/stack/Stackable.js +85 -0
- package/dist/collections/stack/Stackable.min.js +1 -0
- package/dist/main.js +44 -0
- package/dist/main.min.js +1 -0
- package/dist/recipes/Runnable.js +67 -0
- package/dist/recipes/Runnable.min.js +1 -0
- package/dist/recipes/recipes.js +23 -0
- package/dist/recipes/recipes.min.js +1 -0
- package/package.json +44 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true
|
|
5
|
+
})
|
|
6
|
+
exports.default = void 0
|
|
7
|
+
var _Linker = _interopRequireDefault(require('./Linker'))
|
|
8
|
+
var _Arrayable = _interopRequireDefault(require('../arrayable/Arrayable'))
|
|
9
|
+
function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { default: obj } }
|
|
10
|
+
/**
|
|
11
|
+
* @file doubly linked list.
|
|
12
|
+
* @author Joshua Heagle <joshuaheagle@gmail.com>
|
|
13
|
+
* @version 1.0.0
|
|
14
|
+
* @memberOf module:collect-your-stuff
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* LinkedList represents a collection stored as a LinkedList with next references.
|
|
19
|
+
* @extends Arrayable
|
|
20
|
+
*/
|
|
21
|
+
class LinkedList extends _Arrayable.default {
|
|
22
|
+
/**
|
|
23
|
+
* Create the new LinkedList instance, configure the Linker and List classes.
|
|
24
|
+
* @param {LinkedList|Iterable} listClass
|
|
25
|
+
*/
|
|
26
|
+
constructor () {
|
|
27
|
+
const listClass = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : LinkedList
|
|
28
|
+
super(listClass)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Initialize the inner list, should only run once.
|
|
33
|
+
* @method
|
|
34
|
+
* @param {Linker|Array} initialList
|
|
35
|
+
* @return {LinkedList}
|
|
36
|
+
*/
|
|
37
|
+
initialize (initialList) {
|
|
38
|
+
if (this.initialized) {
|
|
39
|
+
console.warn('Attempt to initialize LinkedList which is not empty.')
|
|
40
|
+
return this
|
|
41
|
+
}
|
|
42
|
+
this.initialized = true
|
|
43
|
+
this.innerList = initialList
|
|
44
|
+
return this
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Retrieve the first Linker in the list.
|
|
49
|
+
* @returns {Linker}
|
|
50
|
+
*/
|
|
51
|
+
get first () {
|
|
52
|
+
return this.innerList
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Retrieve the last Linker in the list.
|
|
57
|
+
* @returns {Linker}
|
|
58
|
+
*/
|
|
59
|
+
get last () {
|
|
60
|
+
let tail = this.innerList
|
|
61
|
+
let next = tail.next
|
|
62
|
+
while (next !== null) {
|
|
63
|
+
tail = next
|
|
64
|
+
next = tail.next
|
|
65
|
+
}
|
|
66
|
+
return tail
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Return the length of the list.
|
|
71
|
+
* @returns {number}
|
|
72
|
+
*/
|
|
73
|
+
get length () {
|
|
74
|
+
let current = this.first
|
|
75
|
+
let length = 0
|
|
76
|
+
while (current !== null) {
|
|
77
|
+
++length
|
|
78
|
+
current = current.next
|
|
79
|
+
}
|
|
80
|
+
return length
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Insert a new node (or data) after a node.
|
|
85
|
+
* @method
|
|
86
|
+
* @param {Linker|*} node
|
|
87
|
+
* @param {Linker|*} newNode
|
|
88
|
+
* @returns {LinkedList}
|
|
89
|
+
*/
|
|
90
|
+
insertAfter (node, newNode) {
|
|
91
|
+
newNode = node.classType.make(newNode)
|
|
92
|
+
// Ensure the next reference of this node is assigned to the new node
|
|
93
|
+
newNode.next = node.next
|
|
94
|
+
// Then set this node's next reference to the new node
|
|
95
|
+
node.next = newNode
|
|
96
|
+
return this
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Insert a new node (or data) before a node.
|
|
101
|
+
* @method
|
|
102
|
+
* @param {Linker|*} node
|
|
103
|
+
* @param {Linker|*} newNode
|
|
104
|
+
* @returns {LinkedList}
|
|
105
|
+
*/
|
|
106
|
+
insertBefore (node, newNode) {
|
|
107
|
+
newNode = node.classType.make(newNode)
|
|
108
|
+
let prevNode = null
|
|
109
|
+
let currentNode = this.first
|
|
110
|
+
while (currentNode !== node) {
|
|
111
|
+
prevNode = currentNode
|
|
112
|
+
currentNode = currentNode.next
|
|
113
|
+
}
|
|
114
|
+
// The new node will reference this node as next
|
|
115
|
+
newNode.next = node
|
|
116
|
+
if (prevNode) {
|
|
117
|
+
// Ensure the next reference of the previous node is assigned to the new node
|
|
118
|
+
prevNode.next = newNode
|
|
119
|
+
}
|
|
120
|
+
if (node === this.first) {
|
|
121
|
+
this.innerList = newNode
|
|
122
|
+
}
|
|
123
|
+
return this
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Add a node (or data) after the given (or last) node in the list.
|
|
128
|
+
* @method
|
|
129
|
+
* @param {Linker|*} node
|
|
130
|
+
* @param {Linker} after
|
|
131
|
+
* @returns {Linker}
|
|
132
|
+
*/
|
|
133
|
+
append (node) {
|
|
134
|
+
const after = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.last
|
|
135
|
+
this.insertAfter(after, node)
|
|
136
|
+
return this
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Add a node (or data) before the given (or first) node in the list.
|
|
141
|
+
* @method
|
|
142
|
+
* @param {Linker|*} node
|
|
143
|
+
* @param {Linker} before
|
|
144
|
+
* @returns {Linker}
|
|
145
|
+
*/
|
|
146
|
+
prepend (node) {
|
|
147
|
+
const before = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.first
|
|
148
|
+
this.insertBefore(before, node)
|
|
149
|
+
return this
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Remove a linker from this linked list.
|
|
154
|
+
* @method
|
|
155
|
+
* @param {Linker} node
|
|
156
|
+
* @return {Linker}
|
|
157
|
+
*/
|
|
158
|
+
remove (node) {
|
|
159
|
+
let prevNode = null
|
|
160
|
+
let currentNode = this.first
|
|
161
|
+
while (currentNode !== node) {
|
|
162
|
+
prevNode = currentNode
|
|
163
|
+
currentNode = currentNode.next
|
|
164
|
+
}
|
|
165
|
+
if (prevNode) {
|
|
166
|
+
// Ensure the next reference of the previous node skips over the removed node
|
|
167
|
+
prevNode.next = node.next
|
|
168
|
+
}
|
|
169
|
+
if (node === this.first) {
|
|
170
|
+
// Update list head to point to next if it was this node
|
|
171
|
+
this.innerList = node.next
|
|
172
|
+
}
|
|
173
|
+
return node
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Retrieve a Linker item from this list by numeric index, otherwise return null.
|
|
178
|
+
* @method
|
|
179
|
+
* @param {number} index
|
|
180
|
+
* @returns {Linker|null}
|
|
181
|
+
*/
|
|
182
|
+
item (index) {
|
|
183
|
+
if (index >= 0) {
|
|
184
|
+
let current = this.first
|
|
185
|
+
let currentIndex = -1
|
|
186
|
+
while (++currentIndex < index && current !== null) {
|
|
187
|
+
current = current.next
|
|
188
|
+
}
|
|
189
|
+
return currentIndex === index ? current : null
|
|
190
|
+
}
|
|
191
|
+
let current = this.first
|
|
192
|
+
let currentIndex = 0
|
|
193
|
+
const calculatedIndex = this.length + index
|
|
194
|
+
if (calculatedIndex < 0) {
|
|
195
|
+
return null
|
|
196
|
+
}
|
|
197
|
+
while (currentIndex < calculatedIndex && current !== null) {
|
|
198
|
+
current = current.next
|
|
199
|
+
++currentIndex
|
|
200
|
+
}
|
|
201
|
+
return currentIndex === calculatedIndex ? current : null
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Be able to run forEach on this LinkedList to iterate over the linkers.
|
|
206
|
+
* @param {forEachCallback} callback
|
|
207
|
+
* @param {LinkedList|Arrayable} thisArg
|
|
208
|
+
* @returns {LinkedList}
|
|
209
|
+
*/
|
|
210
|
+
forEach (callback) {
|
|
211
|
+
const thisArg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this
|
|
212
|
+
let index = 0
|
|
213
|
+
let current = this.first
|
|
214
|
+
while (current !== null) {
|
|
215
|
+
callback(current, index, thisArg)
|
|
216
|
+
current = current.next
|
|
217
|
+
++index
|
|
218
|
+
}
|
|
219
|
+
return thisArg
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Be able to iterate over this class.
|
|
224
|
+
* @method
|
|
225
|
+
* @returns {Iterator}
|
|
226
|
+
*/
|
|
227
|
+
[Symbol.iterator] () {
|
|
228
|
+
let current = this.first
|
|
229
|
+
return {
|
|
230
|
+
next: () => {
|
|
231
|
+
const result = {
|
|
232
|
+
value: current,
|
|
233
|
+
done: !current
|
|
234
|
+
}
|
|
235
|
+
current = current ? current.next : null
|
|
236
|
+
return result
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Convert an array to a LinkedList.
|
|
244
|
+
* @methodof LinkedList
|
|
245
|
+
* @param {Array} values
|
|
246
|
+
* @param {Linker} linkerClass
|
|
247
|
+
* @param {LinkedList|Iterable} listClass
|
|
248
|
+
* @returns {LinkedList}
|
|
249
|
+
*/
|
|
250
|
+
LinkedList.fromArray = function () {
|
|
251
|
+
const values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []
|
|
252
|
+
const linkerClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _Linker.default
|
|
253
|
+
const listClass = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : LinkedList
|
|
254
|
+
return _Arrayable.default.fromArray(values, linkerClass, listClass)
|
|
255
|
+
}
|
|
256
|
+
var _default = exports.default = LinkedList
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _Linker=_interopRequireDefault(require("./Linker")),_Arrayable=_interopRequireDefault(require("../arrayable/Arrayable"));function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}class LinkedList extends _Arrayable.default{constructor(){super(arguments.length>0&&void 0!==arguments[0]?arguments[0]:LinkedList)}initialize(t){return this.initialized?(console.warn("Attempt to initialize LinkedList which is not empty."),this):(this.initialized=!0,this.innerList=t,this)}get first(){return this.innerList}get last(){let t=this.innerList,e=t.next;for(;null!==e;)t=e,e=t.next;return t}get length(){let t=this.first,e=0;for(;null!==t;)++e,t=t.next;return e}insertAfter(t,e){return(e=t.classType.make(e)).next=t.next,t.next=e,this}insertBefore(t,e){e=t.classType.make(e);let r=null,i=this.first;for(;i!==t;)r=i,i=i.next;return e.next=t,r&&(r.next=e),t===this.first&&(this.innerList=e),this}append(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.last;return this.insertAfter(e,t),this}prepend(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.first;return this.insertBefore(e,t),this}remove(t){let e=null,r=this.first;for(;r!==t;)e=r,r=r.next;return e&&(e.next=t.next),t===this.first&&(this.innerList=t.next),t}item(t){if(t>=0){let e=this.first,r=-1;for(;++r<t&&null!==e;)e=e.next;return r===t?e:null}let e=this.first,r=0;const i=this.length+t;if(i<0)return null;for(;r<i&&null!==e;)e=e.next,++r;return r===i?e:null}forEach(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this,r=0,i=this.first;for(;null!==i;)t(i,r,e),i=i.next,++r;return e}[Symbol.iterator](){let t=this.first;return{next:()=>{const e={value:t,done:!t};return t=t?t.next:null,e}}}}LinkedList.fromArray=function(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:_Linker.default,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:LinkedList;return _Arrayable.default.fromArray(t,e,r)};var _default=exports.default=LinkedList;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
require("core-js/modules/esnext.async-iterator.reduce.js");
|
|
8
|
+
require("core-js/modules/esnext.iterator.constructor.js");
|
|
9
|
+
require("core-js/modules/esnext.iterator.reduce.js");
|
|
10
|
+
var _ArrayElement = _interopRequireDefault(require("../arrayable/ArrayElement"));
|
|
11
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
+
/**
|
|
13
|
+
* @file linked list item.
|
|
14
|
+
* @author Joshua Heagle <joshuaheagle@gmail.com>
|
|
15
|
+
* @version 1.0.0
|
|
16
|
+
* @memberOf module:collect-your-stuff
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Linker represents a node in a LinkedList.
|
|
21
|
+
* @extends ArrayElement
|
|
22
|
+
*/
|
|
23
|
+
class Linker extends _ArrayElement.default {
|
|
24
|
+
next = null;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Create the new Linker instance, provide the data and optionally configure the type of Linker.
|
|
28
|
+
* @param {Object} [nodeData={}]
|
|
29
|
+
* @param {*} [nodeData.data=null]
|
|
30
|
+
* @param {Linker|null} [nodeData.next=null]
|
|
31
|
+
* @param {Linker} [linkerClass=Linker]
|
|
32
|
+
*/
|
|
33
|
+
constructor() {
|
|
34
|
+
let {
|
|
35
|
+
data = null,
|
|
36
|
+
next = null
|
|
37
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
38
|
+
let linkerClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Linker;
|
|
39
|
+
super(data, linkerClass);
|
|
40
|
+
this.next = next;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Make a new Linker from the data given if it is not already a valid Linker.
|
|
46
|
+
* @methodof Linker
|
|
47
|
+
* @param {Linker|*} linker
|
|
48
|
+
* @param {Linker} [linkerClass=Linker]
|
|
49
|
+
* @return {Linker}
|
|
50
|
+
*/
|
|
51
|
+
Linker.make = function (linker) {
|
|
52
|
+
let linkerClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Linker;
|
|
53
|
+
if (typeof linker !== 'object') {
|
|
54
|
+
// It is not an object, so instantiate the Linker with linker as the data
|
|
55
|
+
return new linkerClass({
|
|
56
|
+
data: linker
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (linker.classType) {
|
|
60
|
+
// Already valid Linker, return as-is
|
|
61
|
+
return linker;
|
|
62
|
+
}
|
|
63
|
+
if (!linker.data) {
|
|
64
|
+
linker = {
|
|
65
|
+
data: linker
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
// Create the new node as the configured linkerClass
|
|
69
|
+
return new linkerClass(linker, linkerClass);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Convert an array into Linker instances, return the head and tail Linkers.
|
|
74
|
+
* @methodof Linker
|
|
75
|
+
* @param {Array} [values=[]]
|
|
76
|
+
* @param {Linker} [linkerClass=Linker]
|
|
77
|
+
* @returns {{head: Linker, tail: Linker}}
|
|
78
|
+
*/
|
|
79
|
+
Linker.fromArray = function () {
|
|
80
|
+
let values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
81
|
+
let linkerClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Linker;
|
|
82
|
+
return values.reduce((references, linker) => {
|
|
83
|
+
const newLinker = linkerClass.make(linker);
|
|
84
|
+
if (references.head === null) {
|
|
85
|
+
// Initialize the head and tail with the new node
|
|
86
|
+
return {
|
|
87
|
+
head: newLinker,
|
|
88
|
+
tail: newLinker
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
// Only update the tail once head has been set, tail is always the most recent node
|
|
92
|
+
references.tail.next = newLinker;
|
|
93
|
+
references.tail = newLinker;
|
|
94
|
+
return references;
|
|
95
|
+
}, {
|
|
96
|
+
head: null,
|
|
97
|
+
tail: null
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
var _default = exports.default = Linker;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0,require("core-js/modules/esnext.async-iterator.reduce.js"),require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.reduce.js");var _ArrayElement=_interopRequireDefault(require("../arrayable/ArrayElement"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}class Linker extends _ArrayElement.default{next=null;constructor(){let{data:e=null,next:t=null}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};super(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:Linker),this.next=t}}Linker.make=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Linker;return"object"!=typeof e?new t({data:e}):e.classType?e:(e.data||(e={data:e}),new t(e,t))},Linker.fromArray=function(){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Linker;return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:[]).reduce(((t,r)=>{const n=e.make(r);return null===t.head?{head:n,tail:n}:(t.tail.next=n,t.tail=n,t)}),{head:null,tail:null})};var _default=exports.default=Linker;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true
|
|
5
|
+
})
|
|
6
|
+
exports.default = void 0
|
|
7
|
+
var _DoublyLinkedList = _interopRequireDefault(require('../doubly-linked-llist/DoublyLinkedList'))
|
|
8
|
+
var _TreeLinker = _interopRequireDefault(require('./TreeLinker'))
|
|
9
|
+
function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { default: obj } }
|
|
10
|
+
/**
|
|
11
|
+
* @file doubly linked tree list.
|
|
12
|
+
* @author Joshua Heagle <joshuaheagle@gmail.com>
|
|
13
|
+
* @version 1.0.0
|
|
14
|
+
* @memberOf module:collect-your-stuff
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* LinkedTreeList represents a collection stored with a root and spreading in branching (tree) formation..
|
|
19
|
+
* @extends DoublyLinkedList
|
|
20
|
+
*/
|
|
21
|
+
class LinkedTreeList extends _DoublyLinkedList.default {
|
|
22
|
+
/**
|
|
23
|
+
* Create the new LinkedTreeList instance, configure the list class.
|
|
24
|
+
* @param {LinkedTreeList|Iterable} [listClass=LinkedTreeList]
|
|
25
|
+
*/
|
|
26
|
+
constructor () {
|
|
27
|
+
const listClass = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : LinkedTreeList
|
|
28
|
+
super(listClass)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Get the parent of this tree list.
|
|
33
|
+
* @return {TreeLinker}
|
|
34
|
+
*/
|
|
35
|
+
get parent () {
|
|
36
|
+
return this.first.parent
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Set the parent of this tree list
|
|
41
|
+
* @param {TreeLinker} parent
|
|
42
|
+
*/
|
|
43
|
+
set parent (parent) {
|
|
44
|
+
let current = this.first
|
|
45
|
+
while (current !== null) {
|
|
46
|
+
current.parent = parent
|
|
47
|
+
current = current.next
|
|
48
|
+
}
|
|
49
|
+
if (parent) {
|
|
50
|
+
parent.children = this
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Return the root parent of the entire tree.
|
|
56
|
+
* @return {TreeLinker}
|
|
57
|
+
*/
|
|
58
|
+
get rootParent () {
|
|
59
|
+
let current = this.first
|
|
60
|
+
let parent = this.first.parent
|
|
61
|
+
while (parent !== null) {
|
|
62
|
+
current = parent
|
|
63
|
+
parent = current.parent
|
|
64
|
+
}
|
|
65
|
+
return current
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Set the children on a parent item.
|
|
70
|
+
* @param {TreeLinker} item
|
|
71
|
+
* @param {LinkedTreeList} children
|
|
72
|
+
*/
|
|
73
|
+
setChildren (item) {
|
|
74
|
+
const children = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null
|
|
75
|
+
if (Array.from(this).indexOf(item) < 0) {
|
|
76
|
+
console.error('item is not a child of this')
|
|
77
|
+
}
|
|
78
|
+
children.parent = item
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Create an instance of LinkedTreeList from an array
|
|
84
|
+
* @memberof LinkedTreeList
|
|
85
|
+
* @param {Array} [values=[]]
|
|
86
|
+
* @param {TreeLinker} [linkerClass=TreeLinker]
|
|
87
|
+
* @param {LinkedTreeList} [listClass=LinkedTreeList]
|
|
88
|
+
* @returns {DoublyLinkedList}
|
|
89
|
+
*/
|
|
90
|
+
LinkedTreeList.fromArray = function () {
|
|
91
|
+
const values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []
|
|
92
|
+
const linkerClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _TreeLinker.default
|
|
93
|
+
const listClass = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : LinkedTreeList
|
|
94
|
+
return _DoublyLinkedList.default.fromArray(values, linkerClass, listClass)
|
|
95
|
+
}
|
|
96
|
+
var _default = exports.default = LinkedTreeList
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _DoublyLinkedList=_interopRequireDefault(require("../doubly-linked-llist/DoublyLinkedList")),_TreeLinker=_interopRequireDefault(require("./TreeLinker"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}class LinkedTreeList extends _DoublyLinkedList.default{constructor(){super(arguments.length>0&&void 0!==arguments[0]?arguments[0]:LinkedTreeList)}get parent(){return this.first.parent}set parent(e){let t=this.first;for(;null!==t;)t.parent=e,t=t.next;e&&(e.children=this)}get rootParent(){let e=this.first,t=this.first.parent;for(;null!==t;)e=t,t=e.parent;return e}setChildren(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;Array.from(this).indexOf(e)<0&&console.error("item is not a child of this"),t.parent=e}}LinkedTreeList.fromArray=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:_TreeLinker.default,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:LinkedTreeList;return _DoublyLinkedList.default.fromArray(e,t,r)};var _default=exports.default=LinkedTreeList;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
require("core-js/modules/esnext.async-iterator.map.js");
|
|
8
|
+
require("core-js/modules/esnext.iterator.map.js");
|
|
9
|
+
var _DoubleLinker = _interopRequireDefault(require("../doubly-linked-llist/DoubleLinker"));
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
/**
|
|
12
|
+
* @file doubly linked tree item.
|
|
13
|
+
* @author Joshua Heagle <joshuaheagle@gmail.com>
|
|
14
|
+
* @version 1.0.0
|
|
15
|
+
* @memberOf module:collect-your-stuff
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* TreeLinker represents a node in a LinkedTreeList.
|
|
20
|
+
* @extends DoubleLinker
|
|
21
|
+
*/
|
|
22
|
+
class TreeLinker extends _DoubleLinker.default {
|
|
23
|
+
parent = null;
|
|
24
|
+
children = null;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Create the new TreeLinker instance, provide the data and optionally configure the type of Linker.
|
|
28
|
+
* @param {Object} [settings={}]
|
|
29
|
+
* @param {*} [settings.data=null]
|
|
30
|
+
* @param {TreeLinker} [settings.prev=null]
|
|
31
|
+
* @param {TreeLinker} [settings.next=null]
|
|
32
|
+
* @param {LinkedTreeList} [settings.children=null]
|
|
33
|
+
* @param {TreeLinker} [settings.parent=null]
|
|
34
|
+
* @param {TreeLinker} [linkerClass=TreeLinker]
|
|
35
|
+
*/
|
|
36
|
+
constructor() {
|
|
37
|
+
let {
|
|
38
|
+
data = null,
|
|
39
|
+
prev = null,
|
|
40
|
+
next = null,
|
|
41
|
+
children = null,
|
|
42
|
+
parent = null
|
|
43
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
44
|
+
let linkerClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : TreeLinker;
|
|
45
|
+
super({
|
|
46
|
+
data,
|
|
47
|
+
prev,
|
|
48
|
+
next
|
|
49
|
+
}, linkerClass);
|
|
50
|
+
this.parent = parent;
|
|
51
|
+
this.children = this.childrenFromArray(children, linkerClass);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Return the parent of this Tree Linker.
|
|
56
|
+
* @return {TreeLinker|null}
|
|
57
|
+
*/
|
|
58
|
+
get parent() {
|
|
59
|
+
return this.parent;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Return the children of this Tree Linker.
|
|
64
|
+
* @return {LinkedTreeList|null}
|
|
65
|
+
*/
|
|
66
|
+
get children() {
|
|
67
|
+
return this.children;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Create the children for this tree from an array.
|
|
72
|
+
* @param {Array|null} children
|
|
73
|
+
* @param {TreeLinker} linkerClass
|
|
74
|
+
* @return {DoubleLinker|null}
|
|
75
|
+
*/
|
|
76
|
+
childrenFromArray() {
|
|
77
|
+
let children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
78
|
+
let linkerClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : TreeLinker;
|
|
79
|
+
if (children === null) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return _DoubleLinker.default.fromArray.apply(this, [children.map(child => Object.assign({}, child, {
|
|
83
|
+
parent: this
|
|
84
|
+
})), linkerClass]).tail;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Make a new DoubleLinker from the data given if it is not already a valid Linker.
|
|
90
|
+
* @methodof TreeLinker
|
|
91
|
+
* @param {TreeLinker|*} linker
|
|
92
|
+
* @param {TreeLinker} [linkerClass=TreeLinker]
|
|
93
|
+
* @return {Linker}
|
|
94
|
+
*/
|
|
95
|
+
TreeLinker.make = function (linker) {
|
|
96
|
+
let linkerClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : TreeLinker;
|
|
97
|
+
return _DoubleLinker.default.make(linker, linkerClass);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Convert an array into DoubleLinker instances, return the head and tail DoubleLinkers.
|
|
102
|
+
* @methodof TreeLinker
|
|
103
|
+
* @param {Array} [values=[]]
|
|
104
|
+
* @param {TreeLinker} [linkerClass=TreeLinker]
|
|
105
|
+
* @returns {{head: TreeLinker, tail: TreeLinker}}
|
|
106
|
+
*/
|
|
107
|
+
TreeLinker.fromArray = function () {
|
|
108
|
+
let values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
109
|
+
let linkerClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : TreeLinker;
|
|
110
|
+
return _DoubleLinker.default.fromArray(values, linkerClass);
|
|
111
|
+
};
|
|
112
|
+
var _default = exports.default = TreeLinker;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0,require("core-js/modules/esnext.async-iterator.map.js"),require("core-js/modules/esnext.iterator.map.js");var _DoubleLinker=_interopRequireDefault(require("../doubly-linked-llist/DoubleLinker"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}class TreeLinker extends _DoubleLinker.default{parent=null;children=null;constructor(){let{data:e=null,prev:r=null,next:t=null,children:n=null,parent:l=null}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:TreeLinker;super({data:e,prev:r,next:t},i),this.parent=l,this.children=this.childrenFromArray(n,i)}get parent(){return this.parent}get children(){return this.children}childrenFromArray(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:TreeLinker;return null===e?null:_DoubleLinker.default.fromArray.apply(this,[e.map((e=>Object.assign({},e,{parent:this}))),r]).tail}}TreeLinker.make=function(e){let r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:TreeLinker;return _DoubleLinker.default.make(e,r)},TreeLinker.fromArray=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:TreeLinker;return _DoubleLinker.default.fromArray(e,r)};var _default=exports.default=TreeLinker;
|