collect-your-stuff 1.2.6 → 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 (74) hide show
  1. package/README.md +2525 -2525
  2. package/browser/collect-your-stuff.js +22766 -22859
  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 -70
  6. package/dist/collections/arrayable/ArrayElement.min.js +1 -1
  7. package/dist/collections/arrayable/Arrayable.d.ts +108 -108
  8. package/dist/collections/arrayable/Arrayable.js +188 -196
  9. package/dist/collections/arrayable/Arrayable.min.js +1 -1
  10. package/dist/collections/doubly-linked-list/DoubleLinker.d.ts +46 -46
  11. package/dist/collections/doubly-linked-list/DoubleLinker.js +69 -77
  12. package/dist/collections/doubly-linked-list/DoubleLinker.min.js +1 -1
  13. package/dist/collections/doubly-linked-list/DoublyLinkedList.d.ts +113 -113
  14. package/dist/collections/doubly-linked-list/DoublyLinkedList.js +274 -277
  15. package/dist/collections/doubly-linked-list/DoublyLinkedList.min.js +1 -1
  16. package/dist/collections/linked-list/LinkedList.d.ts +109 -109
  17. package/dist/collections/linked-list/LinkedList.js +233 -241
  18. package/dist/collections/linked-list/LinkedList.min.js +1 -1
  19. package/dist/collections/linked-list/Linker.d.ts +43 -43
  20. package/dist/collections/linked-list/Linker.js +80 -86
  21. package/dist/collections/linked-list/Linker.min.js +1 -1
  22. package/dist/collections/linked-tree-list/LinkedTreeList.d.ts +135 -135
  23. package/dist/collections/linked-tree-list/LinkedTreeList.js +252 -261
  24. package/dist/collections/linked-tree-list/LinkedTreeList.min.js +1 -1
  25. package/dist/collections/linked-tree-list/TreeLinker.d.ts +63 -63
  26. package/dist/collections/linked-tree-list/TreeLinker.js +79 -92
  27. package/dist/collections/linked-tree-list/TreeLinker.min.js +1 -1
  28. package/dist/collections/queue/Queue.d.ts +63 -63
  29. package/dist/collections/queue/Queue.js +130 -136
  30. package/dist/collections/queue/Queue.min.js +1 -1
  31. package/dist/collections/queue/Queueable.d.ts +77 -77
  32. package/dist/collections/queue/Queueable.js +141 -151
  33. package/dist/collections/queue/Queueable.min.js +1 -1
  34. package/dist/collections/stack/Stack.d.ts +63 -63
  35. package/dist/collections/stack/Stack.js +106 -112
  36. package/dist/collections/stack/Stack.min.js +1 -1
  37. package/dist/collections/stack/Stackable.d.ts +56 -56
  38. package/dist/collections/stack/Stackable.js +83 -91
  39. package/dist/collections/stack/Stackable.min.js +1 -1
  40. package/dist/main.d.ts +40 -40
  41. package/dist/main.js +50 -50
  42. package/dist/recipes/ArrayIterator.d.ts +10 -10
  43. package/dist/recipes/ArrayIterator.js +29 -30
  44. package/dist/recipes/ArrayIterator.min.js +1 -1
  45. package/dist/recipes/DoubleLinkerIterator.d.ts +9 -9
  46. package/dist/recipes/DoubleLinkerIterator.js +24 -24
  47. package/dist/recipes/IsArrayable.d.ts +105 -105
  48. package/dist/recipes/IsArrayable.js +5 -5
  49. package/dist/recipes/IsDoubleLinker.d.ts +14 -14
  50. package/dist/recipes/IsDoubleLinker.js +5 -5
  51. package/dist/recipes/IsElement.d.ts +14 -14
  52. package/dist/recipes/IsElement.js +5 -5
  53. package/dist/recipes/IsLinker.d.ts +10 -10
  54. package/dist/recipes/IsLinker.js +5 -5
  55. package/dist/recipes/IsTree.d.ts +11 -11
  56. package/dist/recipes/IsTree.js +5 -5
  57. package/dist/recipes/IsTreeNode.d.ts +29 -29
  58. package/dist/recipes/IsTreeNode.js +5 -5
  59. package/dist/recipes/LinkerIterator.d.ts +9 -9
  60. package/dist/recipes/LinkerIterator.js +24 -24
  61. package/dist/recipes/Runnable.d.ts +54 -54
  62. package/dist/recipes/Runnable.js +68 -69
  63. package/dist/recipes/Runnable.min.js +1 -1
  64. package/dist/recipes/TreeLinkerIterator.d.ts +9 -9
  65. package/dist/recipes/TreeLinkerIterator.js +25 -25
  66. package/dist/recipes/recipes.d.ts +15 -15
  67. package/dist/recipes/recipes.js +22 -22
  68. package/dist/services/parseTree.d.ts +9 -9
  69. package/dist/services/parseTree.js +24 -24
  70. package/dist/services/parseTreeNext.d.ts +14 -14
  71. package/dist/services/parseTreeNext.js +47 -47
  72. package/dist/services/services.d.ts +13 -13
  73. package/dist/services/services.js +22 -22
  74. package/package.json +2 -2
@@ -1,151 +1,141 @@
1
- 'use strict'
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- })
6
- exports.Queueable = void 0
7
- var _Linker = require('../linked-list/Linker')
8
- /**
9
- * Queueable represents a runnable entry in a queue.
10
- * @extends Linker
11
- */
12
- class Queueable {
13
- /**
14
- * Create a queueable item that can be used in a queue.
15
- * @param {Object} [queueableData={}]
16
- * @param {*} [queueableData.task=null] The data to be stored in this queueable
17
- * @param {Queueable|null} [queueableData.next=null] The reference to the next queueable if any
18
- * @param {boolean|Function} [queueableData.ready=false] Indicate if the queueable is ready to run
19
- */
20
- constructor () {
21
- const _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}
22
- const _ref$task = _ref.task
23
- const task = _ref$task === void 0 ? null : _ref$task
24
- const _ref$next = _ref.next
25
- const next = _ref$next === void 0 ? null : _ref$next
26
- const _ref$ready = _ref.ready
27
- const ready = _ref$ready === void 0 ? false : _ref$ready
28
- this.data = null
29
- this.next = null
30
- this.complete = false
31
- this.ready = false
32
- this.running = false
33
- this.classType = Queueable
34
- this.data = task
35
- this.next = next
36
- this.complete = false
37
- this.ready = ready
38
- this.running = false
39
- }
40
-
41
- /**
42
- * Check ready state.
43
- * @return {boolean}
44
- */
45
- get isReady () {
46
- return typeof this.ready === 'function' ? this.ready() : this.ready
47
- }
48
-
49
- /**
50
- * Retrieve the data which should be formed as a task.
51
- * @return {*}
52
- */
53
- get task () {
54
- if (typeof this.data === 'function') {
55
- return this.data
56
- }
57
- return complete => typeof complete === 'function' ? complete({
58
- context: this.data
59
- }).context : this.data
60
- }
61
-
62
- /**
63
- * Set this queueable as completed.
64
- * @param {Object} completeResponse
65
- * @param {*} [completeResponse.success=true] Indicate when the task failed (use false) or give a success message
66
- * @param {*} [completeResponse.error=false] Indicate a task was error-free (use false) or give an error message
67
- * @param {*} [completeResponse.context=null] Provide additional data in the response
68
- * @return {completeResponse}
69
- */
70
- markCompleted () {
71
- const _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}
72
- const _ref2$success = _ref2.success
73
- const success = _ref2$success === void 0 ? true : _ref2$success
74
- const _ref2$error = _ref2.error
75
- const error = _ref2$error === void 0 ? false : _ref2$error
76
- const _ref2$context = _ref2.context
77
- const context = _ref2$context === void 0 ? null : _ref2$context
78
- this.complete = true
79
- this.running = false
80
- return {
81
- success: success,
82
- error: error,
83
- context: context
84
- }
85
- }
86
-
87
- /**
88
- * Intend to run the queued task when it is ready. If ready, mark this task as running and run the task.
89
- * @return {completeResponse}
90
- */
91
- run () {
92
- if (!this.isReady) {
93
- // Not yet ready, return with errors
94
- return {
95
- success: false,
96
- error: 'Task is not ready',
97
- context: this.data
98
- }
99
- }
100
- if (this.running) {
101
- // Already running, return error since we cannot run again
102
- return {
103
- success: false,
104
- error: 'Queued task is already running, possible missing \'complete\' callback',
105
- context: this.data
106
- }
107
- }
108
- this.running = true
109
- // Wrap the task in the markCompleted function, so we can set flags and format the response
110
- return this.task(this.markCompleted.bind(this))
111
- }
112
- }
113
- /**
114
- * Make a new Queueable from the data given if it is not already a valid Queueable.
115
- * @param {Queueable|*} queueable Return a valid Queueable instance from given data, or even an already valid one.
116
- * @param {IsLinker} [classType=Queueable] Provide the type of IsLinker to use.
117
- * @return {Queueable}
118
- */
119
- exports.Queueable = Queueable
120
- Queueable.make = function (queueable) {
121
- const classType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Queueable
122
- if (typeof queueable !== 'object') {
123
- // It is not an object, so instantiate the Queueable with an element as the data
124
- return new classType({
125
- task: queueable,
126
- ready: true
127
- })
128
- }
129
- if (queueable.classType) {
130
- // Already valid Queueable, return as-is
131
- return queueable
132
- }
133
- if (!queueable.task) {
134
- queueable = {
135
- task: queueable,
136
- ready: true
137
- }
138
- }
139
- // Create the new node as the configured #classType
140
- return new classType(queueable)
141
- }
142
- /**
143
- * Convert an array into Queueable instances, return the head and tail Queueables.
144
- * @param {Array} values Provide an array of data that will be converted to a chain of queueable linkers.
145
- * @param {IsLinker} [classType=Queueable] Provide the type of IsLinker to use.
146
- * @returns {{head: Queueable, tail: Queueable}}
147
- */
148
- Queueable.fromArray = function (values) {
149
- const classType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Queueable
150
- return _Linker.Linker.fromArray(values, classType)
151
- }
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ exports.Queueable = void 0
7
+ var _Linker = require('../linked-list/Linker')
8
+ /**
9
+ * Queueable represents a runnable entry in a queue.
10
+ * @extends Linker
11
+ */
12
+ class Queueable {
13
+ /**
14
+ * Create a queueable item that can be used in a queue.
15
+ * @param {Object} [queueableData={}]
16
+ * @param {*} [queueableData.task=null] The data to be stored in this queueable
17
+ * @param {Queueable|null} [queueableData.next=null] The reference to the next queueable if any
18
+ * @param {boolean|Function} [queueableData.ready=false] Indicate if the queueable is ready to run
19
+ */
20
+ constructor ({
21
+ task = null,
22
+ next = null,
23
+ ready = false
24
+ } = {}) {
25
+ this.data = null
26
+ this.next = null
27
+ this.complete = false
28
+ this.ready = false
29
+ this.running = false
30
+ this.classType = Queueable
31
+ this.data = task
32
+ this.next = next
33
+ this.complete = false
34
+ this.ready = ready
35
+ this.running = false
36
+ }
37
+
38
+ /**
39
+ * Check ready state.
40
+ * @return {boolean}
41
+ */
42
+ get isReady () {
43
+ return typeof this.ready === 'function' ? this.ready() : this.ready
44
+ }
45
+
46
+ /**
47
+ * Retrieve the data which should be formed as a task.
48
+ * @return {*}
49
+ */
50
+ get task () {
51
+ if (typeof this.data === 'function') {
52
+ return this.data
53
+ }
54
+ return complete => typeof complete === 'function' ? complete({
55
+ context: this.data
56
+ }).context : this.data
57
+ }
58
+
59
+ /**
60
+ * Set this queueable as completed.
61
+ * @param {Object} completeResponse
62
+ * @param {*} [completeResponse.success=true] Indicate when the task failed (use false) or give a success message
63
+ * @param {*} [completeResponse.error=false] Indicate a task was error-free (use false) or give an error message
64
+ * @param {*} [completeResponse.context=null] Provide additional data in the response
65
+ * @return {completeResponse}
66
+ */
67
+ markCompleted ({
68
+ success = true,
69
+ error = false,
70
+ context = null
71
+ } = {}) {
72
+ this.complete = true
73
+ this.running = false
74
+ return {
75
+ success: success,
76
+ error: error,
77
+ context: context
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Intend to run the queued task when it is ready. If ready, mark this task as running and run the task.
83
+ * @return {completeResponse}
84
+ */
85
+ run () {
86
+ if (!this.isReady) {
87
+ // Not yet ready, return with errors
88
+ return {
89
+ success: false,
90
+ error: 'Task is not ready',
91
+ context: this.data
92
+ }
93
+ }
94
+ if (this.running) {
95
+ // Already running, return error since we cannot run again
96
+ return {
97
+ success: false,
98
+ error: 'Queued task is already running, possible missing \'complete\' callback',
99
+ context: this.data
100
+ }
101
+ }
102
+ this.running = true
103
+ // Wrap the task in the markCompleted function, so we can set flags and format the response
104
+ return this.task(this.markCompleted.bind(this))
105
+ }
106
+ }
107
+ /**
108
+ * Make a new Queueable from the data given if it is not already a valid Queueable.
109
+ * @param {Queueable|*} queueable Return a valid Queueable instance from given data, or even an already valid one.
110
+ * @param {IsLinker} [classType=Queueable] Provide the type of IsLinker to use.
111
+ * @return {Queueable}
112
+ */
113
+ exports.Queueable = Queueable
114
+ Queueable.make = (queueable, classType = Queueable) => {
115
+ if (typeof queueable !== 'object') {
116
+ // It is not an object, so instantiate the Queueable with an element as the data
117
+ return new classType({
118
+ task: queueable,
119
+ ready: true
120
+ })
121
+ }
122
+ if (queueable.classType) {
123
+ // Already valid Queueable, return as-is
124
+ return queueable
125
+ }
126
+ if (!queueable.task) {
127
+ queueable = {
128
+ task: queueable,
129
+ ready: true
130
+ }
131
+ }
132
+ // Create the new node as the configured #classType
133
+ return new classType(queueable)
134
+ }
135
+ /**
136
+ * Convert an array into Queueable instances, return the head and tail Queueables.
137
+ * @param {Array} values Provide an array of data that will be converted to a chain of queueable linkers.
138
+ * @param {IsLinker} [classType=Queueable] Provide the type of IsLinker to use.
139
+ * @returns {{head: Queueable, tail: Queueable}}
140
+ */
141
+ Queueable.fromArray = (values, classType = Queueable) => _Linker.Linker.fromArray(values, classType)
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Queueable=void 0;var _Linker=require("../linked-list/Linker");class Queueable{constructor(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.task,s=void 0===t?null:t,i=e.next,n=void 0===i?null:i,r=e.ready,a=void 0!==r&&r;this.data=null,this.next=null,this.complete=!1,this.ready=!1,this.running=!1,this.classType=Queueable,this.data=s,this.next=n,this.complete=!1,this.ready=a,this.running=!1}get isReady(){return"function"==typeof this.ready?this.ready():this.ready}get task(){return"function"==typeof this.data?this.data:e=>"function"==typeof e?e({context:this.data}).context:this.data}markCompleted(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.success,s=void 0===t||t,i=e.error,n=void 0!==i&&i,r=e.context,a=void 0===r?null:r;return this.complete=!0,this.running=!1,{success:s,error:n,context:a}}run(){return this.isReady?this.running?{success:!1,error:"Queued task is already running, possible missing 'complete' callback",context:this.data}:(this.running=!0,this.task(this.markCompleted.bind(this))):{success:!1,error:"Task is not ready",context:this.data}}}exports.Queueable=Queueable,Queueable.make=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Queueable;return"object"!=typeof e?new t({task:e,ready:!0}):e.classType?e:(e.task||(e={task:e,ready:!0}),new t(e))},Queueable.fromArray=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Queueable;return _Linker.Linker.fromArray(e,t)};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Queueable=void 0;var _Linker=require("../linked-list/Linker");class Queueable{constructor({task:e=null,next:t=null,ready:s=!1}={}){this.data=null,this.next=null,this.complete=!1,this.ready=!1,this.running=!1,this.classType=Queueable,this.data=e,this.next=t,this.complete=!1,this.ready=s,this.running=!1}get isReady(){return"function"==typeof this.ready?this.ready():this.ready}get task(){return"function"==typeof this.data?this.data:e=>"function"==typeof e?e({context:this.data}).context:this.data}markCompleted({success:e=!0,error:t=!1,context:s=null}={}){return this.complete=!0,this.running=!1,{success:e,error:t,context:s}}run(){return this.isReady?this.running?{success:!1,error:"Queued task is already running, possible missing 'complete' callback",context:this.data}:(this.running=!0,this.task(this.markCompleted.bind(this))):{success:!1,error:"Task is not ready",context:this.data}}}exports.Queueable=Queueable,Queueable.make=(e,t=Queueable)=>"object"!=typeof e?new t({task:e,ready:!0}):e.classType?e:(e.task||(e={task:e,ready:!0}),new t(e)),Queueable.fromArray=(e,t=Queueable)=>_Linker.Linker.fromArray(e,t);
@@ -1,63 +1,63 @@
1
- /**
2
- * @file stack.
3
- * @author Joshua Heagle <joshuaheagle@gmail.com>
4
- * @version 1.0.0
5
- * @memberOf module:collect-your-stuff
6
- */
7
- import { Stackable } from './Stackable';
8
- import { IsArrayable } from '../../recipes/IsArrayable';
9
- import { IsLinker } from '../../recipes/IsLinker';
10
- import { completeResponse } from '../../recipes/Runnable';
11
- /**
12
- * Store a collection of items which can only be inserted and removed from the top.
13
- */
14
- export declare class Stack {
15
- stackedList: IsArrayable<any>;
16
- private listClass;
17
- private stackableClass;
18
- /**
19
- * Instantiate the state with the starter stacked list.
20
- * @param {Iterable|LinkedList} stackedList
21
- * @param {IsArrayable} listClass
22
- * @param {Stackable} stackableClass
23
- */
24
- constructor(stackedList?: IsArrayable<any>, listClass?: any, stackableClass?: typeof Stackable);
25
- /**
26
- * Return true if the stack is empty (there are no tasks in the stacked list)
27
- * @return {boolean}
28
- */
29
- empty(): boolean;
30
- /**
31
- * Take a look at the next stacked task
32
- * @return {Stackable}
33
- */
34
- top(): IsLinker;
35
- /**
36
- * Remove the next stacked task and return it.
37
- * @return {Stackable|null}
38
- */
39
- pop(): Stackable | completeResponse | null;
40
- /**
41
- * Push a stackable task to the top of the stack.
42
- * @param {Stackable|*} stackable Add a new stackable to the top of the stack
43
- */
44
- push(stackable: any): void;
45
- /**
46
- * Remove the next stacked task and return it.
47
- * @return {Stackable|null}
48
- */
49
- remove(): Stackable | null;
50
- /**
51
- * Get the size of the current stack.
52
- * @return {number}
53
- */
54
- size(): number;
55
- /**
56
- * Convert an array to a Stack.
57
- * @param {Array} values An array of values which will be converted to stackables in this queue
58
- * @param {Stackable} stackableClass The class to use for each stackable
59
- * @param {Stack|Iterable} listClass The class to use to manage the stackables
60
- * @returns {Stack}
61
- */
62
- static fromArray: (values?: Array<any>, stackableClass?: typeof Stackable, listClass?: any) => Stack;
63
- }
1
+ /**
2
+ * @file stack.
3
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
4
+ * @version 1.0.0
5
+ * @memberOf module:collect-your-stuff
6
+ */
7
+ import { Stackable } from './Stackable';
8
+ import { IsArrayable } from '../../recipes/IsArrayable';
9
+ import { IsLinker } from '../../recipes/IsLinker';
10
+ import { completeResponse } from '../../recipes/Runnable';
11
+ /**
12
+ * Store a collection of items which can only be inserted and removed from the top.
13
+ */
14
+ export declare class Stack {
15
+ stackedList: IsArrayable<any>;
16
+ private listClass;
17
+ private stackableClass;
18
+ /**
19
+ * Instantiate the state with the starter stacked list.
20
+ * @param {Iterable|LinkedList} stackedList
21
+ * @param {IsArrayable} listClass
22
+ * @param {Stackable} stackableClass
23
+ */
24
+ constructor(stackedList?: IsArrayable<any>, listClass?: any, stackableClass?: typeof Stackable);
25
+ /**
26
+ * Return true if the stack is empty (there are no tasks in the stacked list)
27
+ * @return {boolean}
28
+ */
29
+ empty(): boolean;
30
+ /**
31
+ * Take a look at the next stacked task
32
+ * @return {Stackable}
33
+ */
34
+ top(): IsLinker;
35
+ /**
36
+ * Remove the next stacked task and return it.
37
+ * @return {Stackable|null}
38
+ */
39
+ pop(): Stackable | completeResponse | null;
40
+ /**
41
+ * Push a stackable task to the top of the stack.
42
+ * @param {Stackable|*} stackable Add a new stackable to the top of the stack
43
+ */
44
+ push(stackable: any): void;
45
+ /**
46
+ * Remove the next stacked task and return it.
47
+ * @return {Stackable|null}
48
+ */
49
+ remove(): Stackable | null;
50
+ /**
51
+ * Get the size of the current stack.
52
+ * @return {number}
53
+ */
54
+ size(): number;
55
+ /**
56
+ * Convert an array to a Stack.
57
+ * @param {Array} values An array of values which will be converted to stackables in this queue
58
+ * @param {Stackable} stackableClass The class to use for each stackable
59
+ * @param {Stack|Iterable} listClass The class to use to manage the stackables
60
+ * @returns {Stack}
61
+ */
62
+ static fromArray: (values?: Array<any>, stackableClass?: typeof Stackable, listClass?: any) => Stack;
63
+ }
@@ -1,112 +1,106 @@
1
- 'use strict'
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- })
6
- exports.Stack = void 0
7
- var _Stackable = require('./Stackable')
8
- var _LinkedList = require('../linked-list/LinkedList')
9
- /**
10
- * @file stack.
11
- * @author Joshua Heagle <joshuaheagle@gmail.com>
12
- * @version 1.0.0
13
- * @memberOf module:collect-your-stuff
14
- */
15
-
16
- /**
17
- * Store a collection of items which can only be inserted and removed from the top.
18
- */
19
- class Stack {
20
- /**
21
- * Instantiate the state with the starter stacked list.
22
- * @param {Iterable|LinkedList} stackedList
23
- * @param {IsArrayable} listClass
24
- * @param {Stackable} stackableClass
25
- */
26
- constructor () {
27
- let stackedList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null
28
- const listClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _LinkedList.LinkedList
29
- const stackableClass = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _Stackable.Stackable
30
- this.listClass = listClass
31
- this.stackableClass = stackableClass
32
- if (stackedList === null) {
33
- stackedList = new listClass(stackableClass)
34
- }
35
- this.stackedList = stackedList
36
- }
37
-
38
- /**
39
- * Return true if the stack is empty (there are no tasks in the stacked list)
40
- * @return {boolean}
41
- */
42
- empty () {
43
- return this.size() <= 0
44
- }
45
-
46
- /**
47
- * Take a look at the next stacked task
48
- * @return {Stackable}
49
- */
50
- top () {
51
- return this.stackedList.first
52
- }
53
-
54
- /**
55
- * Remove the next stacked task and return it.
56
- * @return {Stackable|null}
57
- */
58
- pop () {
59
- const next = this.remove()
60
- if (!next) {
61
- return {
62
- success: 'No more stackable tasks in the stack',
63
- error: false,
64
- context: this.stackedList
65
- }
66
- }
67
- return next.run()
68
- }
69
-
70
- /**
71
- * Push a stackable task to the top of the stack.
72
- * @param {Stackable|*} stackable Add a new stackable to the top of the stack
73
- */
74
- push (stackable) {
75
- this.stackedList.prepend(stackable)
76
- }
77
-
78
- /**
79
- * Remove the next stacked task and return it.
80
- * @return {Stackable|null}
81
- */
82
- remove () {
83
- if (this.empty()) {
84
- return null
85
- }
86
- return this.stackedList.remove(this.stackedList.first)
87
- }
88
-
89
- /**
90
- * Get the size of the current stack.
91
- * @return {number}
92
- */
93
- size () {
94
- return this.stackedList.length
95
- }
96
- }
97
- /**
98
- * Convert an array to a Stack.
99
- * @param {Array} values An array of values which will be converted to stackables in this queue
100
- * @param {Stackable} stackableClass The class to use for each stackable
101
- * @param {Stack|Iterable} listClass The class to use to manage the stackables
102
- * @returns {Stack}
103
- */
104
- exports.Stack = Stack
105
- Stack.fromArray = function () {
106
- const values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []
107
- const stackableClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _Stackable.Stackable
108
- const listClass = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _LinkedList.LinkedList
109
- const list = new listClass(stackableClass)
110
- list.initialize(stackableClass.fromArray(values, stackableClass).head)
111
- return new Stack(list)
112
- }
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ exports.Stack = void 0
7
+ var _Stackable = require('./Stackable')
8
+ var _LinkedList = require('../linked-list/LinkedList')
9
+ /**
10
+ * @file stack.
11
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
12
+ * @version 1.0.0
13
+ * @memberOf module:collect-your-stuff
14
+ */
15
+
16
+ /**
17
+ * Store a collection of items which can only be inserted and removed from the top.
18
+ */
19
+ class Stack {
20
+ /**
21
+ * Instantiate the state with the starter stacked list.
22
+ * @param {Iterable|LinkedList} stackedList
23
+ * @param {IsArrayable} listClass
24
+ * @param {Stackable} stackableClass
25
+ */
26
+ constructor (stackedList = null, listClass = _LinkedList.LinkedList, stackableClass = _Stackable.Stackable) {
27
+ this.listClass = listClass
28
+ this.stackableClass = stackableClass
29
+ if (stackedList === null) {
30
+ stackedList = new listClass(stackableClass)
31
+ }
32
+ this.stackedList = stackedList
33
+ }
34
+
35
+ /**
36
+ * Return true if the stack is empty (there are no tasks in the stacked list)
37
+ * @return {boolean}
38
+ */
39
+ empty () {
40
+ return this.size() <= 0
41
+ }
42
+
43
+ /**
44
+ * Take a look at the next stacked task
45
+ * @return {Stackable}
46
+ */
47
+ top () {
48
+ return this.stackedList.first
49
+ }
50
+
51
+ /**
52
+ * Remove the next stacked task and return it.
53
+ * @return {Stackable|null}
54
+ */
55
+ pop () {
56
+ const next = this.remove()
57
+ if (!next) {
58
+ return {
59
+ success: 'No more stackable tasks in the stack',
60
+ error: false,
61
+ context: this.stackedList
62
+ }
63
+ }
64
+ return next.run()
65
+ }
66
+
67
+ /**
68
+ * Push a stackable task to the top of the stack.
69
+ * @param {Stackable|*} stackable Add a new stackable to the top of the stack
70
+ */
71
+ push (stackable) {
72
+ this.stackedList.prepend(stackable)
73
+ }
74
+
75
+ /**
76
+ * Remove the next stacked task and return it.
77
+ * @return {Stackable|null}
78
+ */
79
+ remove () {
80
+ if (this.empty()) {
81
+ return null
82
+ }
83
+ return this.stackedList.remove(this.stackedList.first)
84
+ }
85
+
86
+ /**
87
+ * Get the size of the current stack.
88
+ * @return {number}
89
+ */
90
+ size () {
91
+ return this.stackedList.length
92
+ }
93
+ }
94
+ /**
95
+ * Convert an array to a Stack.
96
+ * @param {Array} values An array of values which will be converted to stackables in this queue
97
+ * @param {Stackable} stackableClass The class to use for each stackable
98
+ * @param {Stack|Iterable} listClass The class to use to manage the stackables
99
+ * @returns {Stack}
100
+ */
101
+ exports.Stack = Stack
102
+ Stack.fromArray = (values = [], stackableClass = _Stackable.Stackable, listClass = _LinkedList.LinkedList) => {
103
+ const list = new listClass(stackableClass)
104
+ list.initialize(stackableClass.fromArray(values, stackableClass).head)
105
+ return new Stack(list)
106
+ }