collect-your-stuff 1.2.7 → 1.2.8

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.
Files changed (61) hide show
  1. package/README.md +2525 -2525
  2. package/browser/collect-your-stuff.js +22766 -22761
  3. package/browser/collect-your-stuff.min.js +1 -1
  4. package/dist/collections/arrayable/ArrayElement.d.ts +36 -36
  5. package/dist/collections/arrayable/ArrayElement.js +64 -64
  6. package/dist/collections/arrayable/Arrayable.d.ts +108 -108
  7. package/dist/collections/arrayable/Arrayable.js +188 -188
  8. package/dist/collections/doubly-linked-list/DoubleLinker.d.ts +46 -46
  9. package/dist/collections/doubly-linked-list/DoubleLinker.js +69 -69
  10. package/dist/collections/doubly-linked-list/DoublyLinkedList.d.ts +113 -113
  11. package/dist/collections/doubly-linked-list/DoublyLinkedList.js +274 -269
  12. package/dist/collections/doubly-linked-list/DoublyLinkedList.min.js +1 -1
  13. package/dist/collections/linked-list/LinkedList.d.ts +109 -109
  14. package/dist/collections/linked-list/LinkedList.js +233 -233
  15. package/dist/collections/linked-list/Linker.d.ts +43 -43
  16. package/dist/collections/linked-list/Linker.js +80 -80
  17. package/dist/collections/linked-tree-list/LinkedTreeList.d.ts +135 -135
  18. package/dist/collections/linked-tree-list/LinkedTreeList.js +252 -252
  19. package/dist/collections/linked-tree-list/TreeLinker.d.ts +63 -63
  20. package/dist/collections/linked-tree-list/TreeLinker.js +79 -79
  21. package/dist/collections/queue/Queue.d.ts +63 -63
  22. package/dist/collections/queue/Queue.js +130 -130
  23. package/dist/collections/queue/Queueable.d.ts +77 -77
  24. package/dist/collections/queue/Queueable.js +141 -141
  25. package/dist/collections/stack/Stack.d.ts +63 -63
  26. package/dist/collections/stack/Stack.js +106 -106
  27. package/dist/collections/stack/Stackable.d.ts +56 -56
  28. package/dist/collections/stack/Stackable.js +83 -83
  29. package/dist/main.d.ts +40 -40
  30. package/dist/main.js +50 -50
  31. package/dist/recipes/ArrayIterator.d.ts +10 -10
  32. package/dist/recipes/ArrayIterator.js +29 -29
  33. package/dist/recipes/DoubleLinkerIterator.d.ts +9 -9
  34. package/dist/recipes/DoubleLinkerIterator.js +24 -24
  35. package/dist/recipes/IsArrayable.d.ts +105 -105
  36. package/dist/recipes/IsArrayable.js +5 -5
  37. package/dist/recipes/IsDoubleLinker.d.ts +14 -14
  38. package/dist/recipes/IsDoubleLinker.js +5 -5
  39. package/dist/recipes/IsElement.d.ts +14 -14
  40. package/dist/recipes/IsElement.js +5 -5
  41. package/dist/recipes/IsLinker.d.ts +10 -10
  42. package/dist/recipes/IsLinker.js +5 -5
  43. package/dist/recipes/IsTree.d.ts +11 -11
  44. package/dist/recipes/IsTree.js +5 -5
  45. package/dist/recipes/IsTreeNode.d.ts +29 -29
  46. package/dist/recipes/IsTreeNode.js +5 -5
  47. package/dist/recipes/LinkerIterator.d.ts +9 -9
  48. package/dist/recipes/LinkerIterator.js +24 -24
  49. package/dist/recipes/Runnable.d.ts +54 -54
  50. package/dist/recipes/Runnable.js +68 -68
  51. package/dist/recipes/TreeLinkerIterator.d.ts +9 -9
  52. package/dist/recipes/TreeLinkerIterator.js +25 -25
  53. package/dist/recipes/recipes.d.ts +15 -15
  54. package/dist/recipes/recipes.js +22 -22
  55. package/dist/services/parseTree.d.ts +9 -9
  56. package/dist/services/parseTree.js +24 -24
  57. package/dist/services/parseTreeNext.d.ts +14 -14
  58. package/dist/services/parseTreeNext.js +47 -47
  59. package/dist/services/services.d.ts +13 -13
  60. package/dist/services/services.js +22 -22
  61. package/package.json +1 -1
@@ -1,188 +1,188 @@
1
- 'use strict'
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- })
6
- exports.Arrayable = void 0
7
- var _ArrayElement = require('./ArrayElement')
8
- var _ArrayIterator = require('../../recipes/ArrayIterator')
9
- /**
10
- * @file arrayable list.
11
- * @author Joshua Heagle <joshuaheagle@gmail.com>
12
- * @version 1.1.0
13
- * @memberOf module:collect-your-stuff
14
- */
15
-
16
- /**
17
- * Arrayable represents a collection stored as an array.
18
- */
19
- class Arrayable {
20
- /**
21
- * Create the new Arrayable instance, configure the Arrayable class.
22
- */
23
- constructor (elementClass = _ArrayElement.ArrayElement) {
24
- this.classType = Arrayable
25
- this.innerList = []
26
- this.initialized = false
27
- this.elementClass = elementClass
28
- }
29
-
30
- /**
31
- * Initialize the inner list, should only run once.
32
- * @param {Array<ArrayElement>} initialList Give the array of elements to start in this Arrayable.
33
- * @return {Arrayable}
34
- */
35
- initialize (initialList) {
36
- if (this.initialized) {
37
- console.warn('Attempt to initialize non-empty list.')
38
- return this
39
- }
40
- this.initialized = true
41
- this.innerList = initialList
42
- return this
43
- }
44
-
45
- /**
46
- * Retrieve a copy of the innerList used.
47
- * @returns {Array<ArrayElement>}
48
- */
49
- get list () {
50
- return this.innerList
51
- }
52
-
53
- /**
54
- * Retrieve the first Element from the Arrayable
55
- * @returns {ArrayElement}
56
- */
57
- get first () {
58
- return this.innerList[0]
59
- }
60
-
61
- /**
62
- * Retrieve the last Element from the Arrayable
63
- * @returns {ArrayElement}
64
- */
65
- get last () {
66
- return this.innerList[this.length - 1]
67
- }
68
-
69
- /**
70
- * Return the length of the list.
71
- * @returns {number}
72
- */
73
- get length () {
74
- return this.innerList.length
75
- }
76
-
77
- /**
78
- * Insert a new node (or data) after a node.
79
- * @param {ArrayElement|*} node The existing node as reference
80
- * @param {ArrayElement|*} newNode The new node to go after the existing node
81
- * @returns {Arrayable}
82
- */
83
- insertAfter (node, newNode) {
84
- const insertAt = this.innerList.indexOf(node)
85
- this.innerList.splice(insertAt + 1, 0, this.elementClass.make(newNode))
86
- return this
87
- }
88
-
89
- /**
90
- * Insert a new node (or data) before a node.
91
- * @param {ArrayElement|*} node The existing node as reference
92
- * @param {ArrayElement|*} newNode The new node to go before the existing node
93
- * @returns {Arrayable}
94
- */
95
- insertBefore (node, newNode) {
96
- const insertAt = this.innerList.indexOf(node)
97
- this.innerList.splice(insertAt, 0, this.elementClass.make(newNode))
98
- return this
99
- }
100
-
101
- /**
102
- * Add a node (or data) after the given (or last) node in the list.
103
- * @param {ArrayElement|*} node The new node to add to the end of the list
104
- * @param {ArrayElement} after The existing last node
105
- * @returns {Arrayable}
106
- */
107
- append (node, after = this.last) {
108
- return this.insertAfter(after, node)
109
- }
110
-
111
- /**
112
- * Add a node (or data) before the given (or first) node in the list.
113
- * @param {ArrayElement|*} node The new node to add to the start of the list
114
- * @param {ArrayElement} before The existing first node
115
- * @returns {Arrayable}
116
- */
117
- prepend (node, before = this.first) {
118
- return this.insertBefore(before, node)
119
- }
120
-
121
- /**
122
- * Remove an element from this arrayable.
123
- * @param {ArrayElement} node The node we wish to remove (and it will be returned after removal)
124
- * @return {ArrayElement}
125
- */
126
- remove (node) {
127
- const deleteAt = this.innerList.indexOf(node)
128
- this.innerList.splice(deleteAt, 1)
129
- return node
130
- }
131
-
132
- /**
133
- * Retrieve an ArrayElement item from this list by numeric index, otherwise return null.
134
- * @param {number} index The integer number for retrieving a node by position.
135
- * @return {ArrayElement|null}
136
- */
137
- item (index) {
138
- if (index >= this.length) {
139
- // index is beyond array limit
140
- return null
141
- }
142
- if (index >= 0) {
143
- // use the positive index at nth position from the beginning of the array
144
- return this.innerList[index]
145
- }
146
- const calculatedIndex = this.length + index
147
- if (calculatedIndex < 0) {
148
- // negative index is beyond array limit (minus direction)
149
- return null
150
- }
151
- // Return the item at nth position from the end of the array
152
- return this.innerList[calculatedIndex]
153
- }
154
-
155
- /**
156
- * Be able to run forEach on this Arrayable to iterate over the elements.
157
- * @param {forEachCallback} callback The function to call for-each element
158
- * @param {Arrayable} thisArg Optional, 'this' reference
159
- * @returns {Arrayable}
160
- */
161
- forEach (callback, thisArg = this) {
162
- for (let i = 0; i < thisArg.length; ++i) {
163
- callback(thisArg.item(i), i, thisArg)
164
- }
165
- return thisArg
166
- }
167
-
168
- /**
169
- * Be able to iterate over this class.
170
- * @returns {Iterator}
171
- */
172
- [Symbol.iterator] () {
173
- const index = 0
174
- return new _ArrayIterator.ArrayIterator(this.innerList, index)
175
- }
176
- }
177
- /**
178
- * Convert an array to an Arrayable.
179
- * @param {Array} values An array of values which will be converted to elements in this arrayable
180
- * @param {IsElement} [elementClass=ArrayElement] The class to use for each element
181
- * @param {IsArrayable<ArrayElement>} [classType=Arrayable] Provide the type of IsArrayable to use.
182
- * @returns {Arrayable}
183
- */
184
- exports.Arrayable = Arrayable
185
- Arrayable.fromArray = (values = [], elementClass = _ArrayElement.ArrayElement, classType = Arrayable) => {
186
- const list = new classType(elementClass)
187
- return list.initialize(elementClass.fromArray(values).head)
188
- }
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ exports.Arrayable = void 0
7
+ var _ArrayElement = require('./ArrayElement')
8
+ var _ArrayIterator = require('../../recipes/ArrayIterator')
9
+ /**
10
+ * @file arrayable list.
11
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
12
+ * @version 1.1.0
13
+ * @memberOf module:collect-your-stuff
14
+ */
15
+
16
+ /**
17
+ * Arrayable represents a collection stored as an array.
18
+ */
19
+ class Arrayable {
20
+ /**
21
+ * Create the new Arrayable instance, configure the Arrayable class.
22
+ */
23
+ constructor (elementClass = _ArrayElement.ArrayElement) {
24
+ this.classType = Arrayable
25
+ this.innerList = []
26
+ this.initialized = false
27
+ this.elementClass = elementClass
28
+ }
29
+
30
+ /**
31
+ * Initialize the inner list, should only run once.
32
+ * @param {Array<ArrayElement>} initialList Give the array of elements to start in this Arrayable.
33
+ * @return {Arrayable}
34
+ */
35
+ initialize (initialList) {
36
+ if (this.initialized) {
37
+ console.warn('Attempt to initialize non-empty list.')
38
+ return this
39
+ }
40
+ this.initialized = true
41
+ this.innerList = initialList
42
+ return this
43
+ }
44
+
45
+ /**
46
+ * Retrieve a copy of the innerList used.
47
+ * @returns {Array<ArrayElement>}
48
+ */
49
+ get list () {
50
+ return this.innerList
51
+ }
52
+
53
+ /**
54
+ * Retrieve the first Element from the Arrayable
55
+ * @returns {ArrayElement}
56
+ */
57
+ get first () {
58
+ return this.innerList[0]
59
+ }
60
+
61
+ /**
62
+ * Retrieve the last Element from the Arrayable
63
+ * @returns {ArrayElement}
64
+ */
65
+ get last () {
66
+ return this.innerList[this.length - 1]
67
+ }
68
+
69
+ /**
70
+ * Return the length of the list.
71
+ * @returns {number}
72
+ */
73
+ get length () {
74
+ return this.innerList.length
75
+ }
76
+
77
+ /**
78
+ * Insert a new node (or data) after a node.
79
+ * @param {ArrayElement|*} node The existing node as reference
80
+ * @param {ArrayElement|*} newNode The new node to go after the existing node
81
+ * @returns {Arrayable}
82
+ */
83
+ insertAfter (node, newNode) {
84
+ const insertAt = this.innerList.indexOf(node)
85
+ this.innerList.splice(insertAt + 1, 0, this.elementClass.make(newNode))
86
+ return this
87
+ }
88
+
89
+ /**
90
+ * Insert a new node (or data) before a node.
91
+ * @param {ArrayElement|*} node The existing node as reference
92
+ * @param {ArrayElement|*} newNode The new node to go before the existing node
93
+ * @returns {Arrayable}
94
+ */
95
+ insertBefore (node, newNode) {
96
+ const insertAt = this.innerList.indexOf(node)
97
+ this.innerList.splice(insertAt, 0, this.elementClass.make(newNode))
98
+ return this
99
+ }
100
+
101
+ /**
102
+ * Add a node (or data) after the given (or last) node in the list.
103
+ * @param {ArrayElement|*} node The new node to add to the end of the list
104
+ * @param {ArrayElement} after The existing last node
105
+ * @returns {Arrayable}
106
+ */
107
+ append (node, after = this.last) {
108
+ return this.insertAfter(after, node)
109
+ }
110
+
111
+ /**
112
+ * Add a node (or data) before the given (or first) node in the list.
113
+ * @param {ArrayElement|*} node The new node to add to the start of the list
114
+ * @param {ArrayElement} before The existing first node
115
+ * @returns {Arrayable}
116
+ */
117
+ prepend (node, before = this.first) {
118
+ return this.insertBefore(before, node)
119
+ }
120
+
121
+ /**
122
+ * Remove an element from this arrayable.
123
+ * @param {ArrayElement} node The node we wish to remove (and it will be returned after removal)
124
+ * @return {ArrayElement}
125
+ */
126
+ remove (node) {
127
+ const deleteAt = this.innerList.indexOf(node)
128
+ this.innerList.splice(deleteAt, 1)
129
+ return node
130
+ }
131
+
132
+ /**
133
+ * Retrieve an ArrayElement item from this list by numeric index, otherwise return null.
134
+ * @param {number} index The integer number for retrieving a node by position.
135
+ * @return {ArrayElement|null}
136
+ */
137
+ item (index) {
138
+ if (index >= this.length) {
139
+ // index is beyond array limit
140
+ return null
141
+ }
142
+ if (index >= 0) {
143
+ // use the positive index at nth position from the beginning of the array
144
+ return this.innerList[index]
145
+ }
146
+ const calculatedIndex = this.length + index
147
+ if (calculatedIndex < 0) {
148
+ // negative index is beyond array limit (minus direction)
149
+ return null
150
+ }
151
+ // Return the item at nth position from the end of the array
152
+ return this.innerList[calculatedIndex]
153
+ }
154
+
155
+ /**
156
+ * Be able to run forEach on this Arrayable to iterate over the elements.
157
+ * @param {forEachCallback} callback The function to call for-each element
158
+ * @param {Arrayable} thisArg Optional, 'this' reference
159
+ * @returns {Arrayable}
160
+ */
161
+ forEach (callback, thisArg = this) {
162
+ for (let i = 0; i < thisArg.length; ++i) {
163
+ callback(thisArg.item(i), i, thisArg)
164
+ }
165
+ return thisArg
166
+ }
167
+
168
+ /**
169
+ * Be able to iterate over this class.
170
+ * @returns {Iterator}
171
+ */
172
+ [Symbol.iterator] () {
173
+ const index = 0
174
+ return new _ArrayIterator.ArrayIterator(this.innerList, index)
175
+ }
176
+ }
177
+ /**
178
+ * Convert an array to an Arrayable.
179
+ * @param {Array} values An array of values which will be converted to elements in this arrayable
180
+ * @param {IsElement} [elementClass=ArrayElement] The class to use for each element
181
+ * @param {IsArrayable<ArrayElement>} [classType=Arrayable] Provide the type of IsArrayable to use.
182
+ * @returns {Arrayable}
183
+ */
184
+ exports.Arrayable = Arrayable
185
+ Arrayable.fromArray = (values = [], elementClass = _ArrayElement.ArrayElement, classType = Arrayable) => {
186
+ const list = new classType(elementClass)
187
+ return list.initialize(elementClass.fromArray(values).head)
188
+ }
@@ -1,46 +1,46 @@
1
- /**
2
- * @file doubly linked list item.
3
- * @author Joshua Heagle <joshuaheagle@gmail.com>
4
- * @version 1.1.0
5
- * @memberOf module:collect-your-stuff
6
- */
7
- import { IsDoubleLinker } from '../../recipes/IsDoubleLinker';
8
- /**
9
- * DoubleLinker represents a node in a DoublyLinkedList which is chained by next and prev.
10
- * @extends Linker
11
- */
12
- export declare class DoubleLinker implements IsDoubleLinker {
13
- readonly classType: typeof DoubleLinker;
14
- data: any;
15
- next: DoubleLinker | null;
16
- prev: DoubleLinker | null;
17
- /**
18
- * Create the new DoubleLinker instance, provide the data and optionally the next and prev references.
19
- * @param {Object} [nodeData={}]
20
- * @param {*} [nodeData.data=null] The data to be stored in this linker
21
- * @param {DoubleLinker|null} [nodeData.next=null] The reference to the next linker if any
22
- * @param {DoubleLinker|null} [nodeData.prev=null] The reference to the previous linker if any
23
- */
24
- constructor({ data, next, prev }?: {
25
- data?: any;
26
- next?: DoubleLinker | null;
27
- prev?: DoubleLinker | null;
28
- });
29
- /**
30
- * Make a new DoubleLinker from the data given if it is not already a valid Linker.
31
- * @param {DoubleLinker|*} linker Return a valid Linker instance from given data, or even an already valid one.
32
- * @param {IsDoubleLinker} [classType=DoubleLinker] Provide the type of IsDoubleLinker to use.
33
- * @return {DoubleLinker}
34
- */
35
- static make: (linker: DoubleLinker | any, classType?: any) => IsDoubleLinker | any;
36
- /**
37
- * Convert an array into DoubleLinker instances, return the head and tail DoubleLinkers.
38
- * @param {Array} [values=[]] Provide an array of data that will be converted to a chain of linkers.
39
- * @param {IsDoubleLinker} [classType=DoubleLinker] Provide the type of IsDoubleLinker to use.
40
- * @returns {{head: DoubleLinker, tail: DoubleLinker}}
41
- */
42
- static fromArray: (values?: Array<any>, classType?: any) => {
43
- head: DoubleLinker | any;
44
- tail: DoubleLinker | any;
45
- };
46
- }
1
+ /**
2
+ * @file doubly linked list item.
3
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
4
+ * @version 1.1.0
5
+ * @memberOf module:collect-your-stuff
6
+ */
7
+ import { IsDoubleLinker } from '../../recipes/IsDoubleLinker';
8
+ /**
9
+ * DoubleLinker represents a node in a DoublyLinkedList which is chained by next and prev.
10
+ * @extends Linker
11
+ */
12
+ export declare class DoubleLinker implements IsDoubleLinker {
13
+ readonly classType: typeof DoubleLinker;
14
+ data: any;
15
+ next: DoubleLinker | null;
16
+ prev: DoubleLinker | null;
17
+ /**
18
+ * Create the new DoubleLinker instance, provide the data and optionally the next and prev references.
19
+ * @param {Object} [nodeData={}]
20
+ * @param {*} [nodeData.data=null] The data to be stored in this linker
21
+ * @param {DoubleLinker|null} [nodeData.next=null] The reference to the next linker if any
22
+ * @param {DoubleLinker|null} [nodeData.prev=null] The reference to the previous linker if any
23
+ */
24
+ constructor({ data, next, prev }?: {
25
+ data?: any;
26
+ next?: DoubleLinker | null;
27
+ prev?: DoubleLinker | null;
28
+ });
29
+ /**
30
+ * Make a new DoubleLinker from the data given if it is not already a valid Linker.
31
+ * @param {DoubleLinker|*} linker Return a valid Linker instance from given data, or even an already valid one.
32
+ * @param {IsDoubleLinker} [classType=DoubleLinker] Provide the type of IsDoubleLinker to use.
33
+ * @return {DoubleLinker}
34
+ */
35
+ static make: (linker: DoubleLinker | any, classType?: any) => IsDoubleLinker | any;
36
+ /**
37
+ * Convert an array into DoubleLinker instances, return the head and tail DoubleLinkers.
38
+ * @param {Array} [values=[]] Provide an array of data that will be converted to a chain of linkers.
39
+ * @param {IsDoubleLinker} [classType=DoubleLinker] Provide the type of IsDoubleLinker to use.
40
+ * @returns {{head: DoubleLinker, tail: DoubleLinker}}
41
+ */
42
+ static fromArray: (values?: Array<any>, classType?: any) => {
43
+ head: DoubleLinker | any;
44
+ tail: DoubleLinker | any;
45
+ };
46
+ }
@@ -1,69 +1,69 @@
1
- 'use strict'
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- })
6
- exports.DoubleLinker = void 0
7
- require('core-js/modules/esnext.iterator.constructor.js')
8
- require('core-js/modules/esnext.iterator.reduce.js')
9
- var _Linker = require('../linked-list/Linker')
10
- /**
11
- * DoubleLinker represents a node in a DoublyLinkedList which is chained by next and prev.
12
- * @extends Linker
13
- */
14
- class DoubleLinker {
15
- /**
16
- * Create the new DoubleLinker instance, provide the data and optionally the next and prev references.
17
- * @param {Object} [nodeData={}]
18
- * @param {*} [nodeData.data=null] The data to be stored in this linker
19
- * @param {DoubleLinker|null} [nodeData.next=null] The reference to the next linker if any
20
- * @param {DoubleLinker|null} [nodeData.prev=null] The reference to the previous linker if any
21
- */
22
- constructor ({
23
- data = null,
24
- next = null,
25
- prev = null
26
- } = {}) {
27
- this.classType = DoubleLinker
28
- this.data = null
29
- this.next = null
30
- this.prev = null
31
- this.data = data
32
- this.next = next
33
- this.prev = prev
34
- }
35
- }
36
- /**
37
- * Make a new DoubleLinker from the data given if it is not already a valid Linker.
38
- * @param {DoubleLinker|*} linker Return a valid Linker instance from given data, or even an already valid one.
39
- * @param {IsDoubleLinker} [classType=DoubleLinker] Provide the type of IsDoubleLinker to use.
40
- * @return {DoubleLinker}
41
- */
42
- exports.DoubleLinker = DoubleLinker
43
- DoubleLinker.make = (linker, classType = DoubleLinker) => {
44
- return _Linker.Linker.make(linker, classType)
45
- }
46
- /**
47
- * Convert an array into DoubleLinker instances, return the head and tail DoubleLinkers.
48
- * @param {Array} [values=[]] Provide an array of data that will be converted to a chain of linkers.
49
- * @param {IsDoubleLinker} [classType=DoubleLinker] Provide the type of IsDoubleLinker to use.
50
- * @returns {{head: DoubleLinker, tail: DoubleLinker}}
51
- */
52
- DoubleLinker.fromArray = (values = [], classType = DoubleLinker) => values.reduce((references, linker) => {
53
- const newLinker = classType.make(linker, classType)
54
- if (references.head === null) {
55
- // Initialize the head and tail with the new node
56
- return {
57
- head: newLinker,
58
- tail: newLinker
59
- }
60
- }
61
- newLinker.prev = references.tail
62
- // Only update the tail once head has been set, tail is always the most recent node
63
- references.tail.next = newLinker
64
- references.tail = newLinker
65
- return references
66
- }, {
67
- head: null,
68
- tail: null
69
- })
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ exports.DoubleLinker = void 0
7
+ require('core-js/modules/esnext.iterator.constructor.js')
8
+ require('core-js/modules/esnext.iterator.reduce.js')
9
+ var _Linker = require('../linked-list/Linker')
10
+ /**
11
+ * DoubleLinker represents a node in a DoublyLinkedList which is chained by next and prev.
12
+ * @extends Linker
13
+ */
14
+ class DoubleLinker {
15
+ /**
16
+ * Create the new DoubleLinker instance, provide the data and optionally the next and prev references.
17
+ * @param {Object} [nodeData={}]
18
+ * @param {*} [nodeData.data=null] The data to be stored in this linker
19
+ * @param {DoubleLinker|null} [nodeData.next=null] The reference to the next linker if any
20
+ * @param {DoubleLinker|null} [nodeData.prev=null] The reference to the previous linker if any
21
+ */
22
+ constructor ({
23
+ data = null,
24
+ next = null,
25
+ prev = null
26
+ } = {}) {
27
+ this.classType = DoubleLinker
28
+ this.data = null
29
+ this.next = null
30
+ this.prev = null
31
+ this.data = data
32
+ this.next = next
33
+ this.prev = prev
34
+ }
35
+ }
36
+ /**
37
+ * Make a new DoubleLinker from the data given if it is not already a valid Linker.
38
+ * @param {DoubleLinker|*} linker Return a valid Linker instance from given data, or even an already valid one.
39
+ * @param {IsDoubleLinker} [classType=DoubleLinker] Provide the type of IsDoubleLinker to use.
40
+ * @return {DoubleLinker}
41
+ */
42
+ exports.DoubleLinker = DoubleLinker
43
+ DoubleLinker.make = (linker, classType = DoubleLinker) => {
44
+ return _Linker.Linker.make(linker, classType)
45
+ }
46
+ /**
47
+ * Convert an array into DoubleLinker instances, return the head and tail DoubleLinkers.
48
+ * @param {Array} [values=[]] Provide an array of data that will be converted to a chain of linkers.
49
+ * @param {IsDoubleLinker} [classType=DoubleLinker] Provide the type of IsDoubleLinker to use.
50
+ * @returns {{head: DoubleLinker, tail: DoubleLinker}}
51
+ */
52
+ DoubleLinker.fromArray = (values = [], classType = DoubleLinker) => values.reduce((references, linker) => {
53
+ const newLinker = classType.make(linker, classType)
54
+ if (references.head === null) {
55
+ // Initialize the head and tail with the new node
56
+ return {
57
+ head: newLinker,
58
+ tail: newLinker
59
+ }
60
+ }
61
+ newLinker.prev = references.tail
62
+ // Only update the tail once head has been set, tail is always the most recent node
63
+ references.tail.next = newLinker
64
+ references.tail = newLinker
65
+ return references
66
+ }, {
67
+ head: null,
68
+ tail: null
69
+ })