collect-your-stuff 1.4.0 → 2.1.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.
Files changed (35) hide show
  1. package/dist/collections/linked-tree-list/LinkedTreeList.d.ts +31 -19
  2. package/dist/collections/linked-tree-list/LinkedTreeList.js +63 -24
  3. package/dist/collections/linked-tree-list/LinkedTreeList.min.js +1 -1
  4. package/dist/collections/linked-tree-list/TreeLinker.d.ts +3 -1
  5. package/dist/collections/linked-tree-list/TreeLinker.js +14 -5
  6. package/dist/collections/linked-tree-list/TreeLinker.min.js +1 -1
  7. package/dist/collections/queue/Queue.d.ts +35 -42
  8. package/dist/collections/queue/Queue.js +64 -80
  9. package/dist/collections/queue/Queue.min.js +1 -1
  10. package/dist/collections/queue/TaskQueue.d.ts +68 -0
  11. package/dist/collections/queue/TaskQueue.js +137 -0
  12. package/dist/collections/queue/TaskQueue.min.js +1 -0
  13. package/dist/collections/stack/Stack.d.ts +40 -40
  14. package/dist/collections/stack/Stack.js +73 -52
  15. package/dist/collections/stack/Stack.min.js +1 -1
  16. package/dist/collections/stack/TaskStack.d.ts +65 -0
  17. package/dist/collections/stack/TaskStack.js +107 -0
  18. package/dist/collections/stack/TaskStack.min.js +1 -0
  19. package/dist/main.d.ts +8 -2
  20. package/dist/main.js +16 -0
  21. package/dist/main.min.js +1 -1
  22. package/dist/recipes/IsQueue.d.ts +37 -0
  23. package/dist/recipes/IsQueue.js +5 -0
  24. package/dist/recipes/IsQueue.min.js +1 -0
  25. package/dist/recipes/IsStack.d.ts +36 -0
  26. package/dist/recipes/IsStack.js +5 -0
  27. package/dist/recipes/IsStack.min.js +1 -0
  28. package/dist/recipes/TreeLinkerIterator.d.ts +3 -1
  29. package/dist/recipes/TreeLinkerIterator.js +4 -2
  30. package/dist/recipes/TreeLinkerIterator.min.js +1 -1
  31. package/dist/services/parseTreeNext.d.ts +4 -1
  32. package/dist/services/parseTreeNext.js +14 -19
  33. package/dist/services/parseTreeNext.min.js +1 -1
  34. package/dist/services/services.d.ts +1 -1
  35. package/package.json +1 -1
@@ -0,0 +1,68 @@
1
+ /**
2
+ * @file task queue
3
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
4
+ * @version 1.1.0
5
+ * @memberOf module:collect-your-stuff
6
+ */
7
+ import { Queueable } from './Queueable';
8
+ import { IsArrayable } from '../../recipes/IsArrayable';
9
+ import { IsLinker } from '../../recipes/IsLinker';
10
+ import { completeResponse } from '../../recipes/Runnable';
11
+ /**
12
+ * Maintain a series of queued tasks (Queueables): dequeue() takes the next task from the front and RUNS it, giving each
13
+ * task in turn a chance to run (a task which is not ready, or which has not finished, is placed at the back again).
14
+ * This is a task scheduler, for a plain first-in-first-out collection of items use Queue.
15
+ */
16
+ export declare class TaskQueue {
17
+ /** The list which stores the queueables, the first is next to be dequeued. */
18
+ queuedList: IsArrayable<any>;
19
+ private listClass;
20
+ private queueableClass;
21
+ /**
22
+ * Instantiate the queue with the given queue list.
23
+ * @param {Iterable|LinkedList} queuedList Give the list of queueables to start in this queue.
24
+ * @param {IsArrayable} [listClass=LinkedList] The type of list to create when no queued list is given.
25
+ * @param {Queueable} [queueableClass=Queueable] The class used to wrap queued items.
26
+ */
27
+ constructor(queuedList?: IsArrayable<any>, listClass?: any, queueableClass?: typeof Queueable);
28
+ /**
29
+ * Take a queued task from the front of the queue and run it if ready. A task which is not ready yet is kept in the
30
+ * queue (never dropped), a task which is still running is reported as blocking and left to finish on its own, and
31
+ * completed tasks are discarded.
32
+ * @return {completeResponse|*}
33
+ */
34
+ dequeue(): completeResponse | any;
35
+ /**
36
+ * Return true if the queue is empty (there are no tasks in the queue list)
37
+ * @return {boolean}
38
+ */
39
+ empty(): boolean;
40
+ /**
41
+ * Add a queued task to the end of the queue
42
+ * @param {Queueable} queueable Add a new queueable to the end of the queue
43
+ */
44
+ enqueue(queueable: Queueable): void;
45
+ /**
46
+ * Take a look at the next queued task
47
+ * @return {Queueable}
48
+ */
49
+ peek(): IsLinker;
50
+ /**
51
+ * Remove the next queued item and return it.
52
+ * @return {Queueable|null}
53
+ */
54
+ remove(): Queueable | null;
55
+ /**
56
+ * Get the length of the current queue.
57
+ * @return {number}
58
+ */
59
+ size(): number;
60
+ /**
61
+ * Convert an array to a TaskQueue.
62
+ * @param {Array} values An array of values which will be converted to queueables in this queue
63
+ * @param {Queueable} queueableClass The class to use for each queueable
64
+ * @param {TaskQueue|Iterable} listClass The class to use to manage the queueables
65
+ * @returns {TaskQueue}
66
+ */
67
+ static fromArray: (values?: Array<any>, queueableClass?: typeof Queueable, listClass?: any) => TaskQueue;
68
+ }
@@ -0,0 +1,137 @@
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ exports.TaskQueue = void 0
7
+ var _Queueable = require('./Queueable')
8
+ var _LinkedList = require('../linked-list/LinkedList')
9
+ /**
10
+ * @file task queue
11
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
12
+ * @version 1.1.0
13
+ * @memberOf module:collect-your-stuff
14
+ */
15
+
16
+ /**
17
+ * Maintain a series of queued tasks (Queueables): dequeue() takes the next task from the front and RUNS it, giving each
18
+ * task in turn a chance to run (a task which is not ready, or which has not finished, is placed at the back again).
19
+ * This is a task scheduler, for a plain first-in-first-out collection of items use Queue.
20
+ */
21
+ class TaskQueue {
22
+ /**
23
+ * Instantiate the queue with the given queue list.
24
+ * @param {Iterable|LinkedList} queuedList Give the list of queueables to start in this queue.
25
+ * @param {IsArrayable} [listClass=LinkedList] The type of list to create when no queued list is given.
26
+ * @param {Queueable} [queueableClass=Queueable] The class used to wrap queued items.
27
+ */
28
+ constructor (queuedList = null, listClass = _LinkedList.LinkedList, queueableClass = _Queueable.Queueable) {
29
+ this.listClass = listClass
30
+ this.queueableClass = queueableClass
31
+ if (queuedList === null) {
32
+ queuedList = new listClass(queueableClass)
33
+ }
34
+ this.queuedList = queuedList
35
+ }
36
+
37
+ /**
38
+ * Take a queued task from the front of the queue and run it if ready. A task which is not ready yet is kept in the
39
+ * queue (never dropped), a task which is still running is reported as blocking and left to finish on its own, and
40
+ * completed tasks are discarded.
41
+ * @return {completeResponse|*}
42
+ */
43
+ dequeue () {
44
+ let next = this.remove()
45
+ // Tasks which already completed are discarded when they reach the front of the queue
46
+ while (next && next.complete) {
47
+ next = this.remove()
48
+ }
49
+ if (!next) {
50
+ return {
51
+ success: 'No more queueable tasks in the queue',
52
+ error: false,
53
+ context: this.queuedList
54
+ }
55
+ }
56
+ if (next.running) {
57
+ // The unfinished task reports back through its own complete callback, so it is not kept in the queue
58
+ return {
59
+ success: false,
60
+ error: 'The queue has been blocked by an unfinished task.',
61
+ context: next
62
+ }
63
+ }
64
+ if (!next.isReady) {
65
+ // Keep the task (at the back, so the next dequeue can try the other tasks) rather than losing it
66
+ this.enqueue(next)
67
+ // We could go check the next in queue here but if we end up in a state where nothing is ready it would infinite loop
68
+ // Also, we want the loop handled externally
69
+ return {
70
+ success: false,
71
+ error: 'Unable to find ready task.',
72
+ context: next
73
+ }
74
+ }
75
+ if (!this.empty()) {
76
+ // Place back in queue to be checked once again next time, only if the queue will not be empty
77
+ this.enqueue(next)
78
+ }
79
+ return next.run.call(next)
80
+ }
81
+
82
+ /**
83
+ * Return true if the queue is empty (there are no tasks in the queue list)
84
+ * @return {boolean}
85
+ */
86
+ empty () {
87
+ return this.size() <= 0
88
+ }
89
+
90
+ /**
91
+ * Add a queued task to the end of the queue
92
+ * @param {Queueable} queueable Add a new queueable to the end of the queue
93
+ */
94
+ enqueue (queueable) {
95
+ this.queuedList.append(queueable)
96
+ }
97
+
98
+ /**
99
+ * Take a look at the next queued task
100
+ * @return {Queueable}
101
+ */
102
+ peek () {
103
+ return this.queuedList.first
104
+ }
105
+
106
+ /**
107
+ * Remove the next queued item and return it.
108
+ * @return {Queueable|null}
109
+ */
110
+ remove () {
111
+ if (this.empty()) {
112
+ return null
113
+ }
114
+ return this.queuedList.remove(this.queuedList.first)
115
+ }
116
+
117
+ /**
118
+ * Get the length of the current queue.
119
+ * @return {number}
120
+ */
121
+ size () {
122
+ return this.queuedList.length
123
+ }
124
+ }
125
+ /**
126
+ * Convert an array to a TaskQueue.
127
+ * @param {Array} values An array of values which will be converted to queueables in this queue
128
+ * @param {Queueable} queueableClass The class to use for each queueable
129
+ * @param {TaskQueue|Iterable} listClass The class to use to manage the queueables
130
+ * @returns {TaskQueue}
131
+ */
132
+ exports.TaskQueue = TaskQueue
133
+ TaskQueue.fromArray = (values = [], queueableClass = _Queueable.Queueable, listClass = _LinkedList.LinkedList) => {
134
+ const list = new listClass(queueableClass)
135
+ list.initialize(queueableClass.fromArray(values, queueableClass).head)
136
+ return new TaskQueue(list, listClass, queueableClass)
137
+ }
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.TaskQueue=void 0;var _Queueable=require("./Queueable"),_LinkedList=require("../linked-list/LinkedList");class TaskQueue{constructor(e=null,u=_LinkedList.LinkedList,s=_Queueable.Queueable){this.listClass=u,this.queueableClass=s,null===e&&(e=new u(s)),this.queuedList=e}dequeue(){let e=this.remove();for(;e&&e.complete;)e=this.remove();return e?e.running?{success:!1,error:"The queue has been blocked by an unfinished task.",context:e}:e.isReady?(this.empty()||this.enqueue(e),e.run.call(e)):(this.enqueue(e),{success:!1,error:"Unable to find ready task.",context:e}):{success:"No more queueable tasks in the queue",error:!1,context:this.queuedList}}empty(){return this.size()<=0}enqueue(e){this.queuedList.append(e)}peek(){return this.queuedList.first}remove(){return this.empty()?null:this.queuedList.remove(this.queuedList.first)}size(){return this.queuedList.length}}exports.TaskQueue=TaskQueue,TaskQueue.fromArray=(e=[],u=_Queueable.Queueable,s=_LinkedList.LinkedList)=>{const t=new s(u);return t.initialize(u.fromArray(e,u).head),new TaskQueue(t,s,u)};
@@ -1,64 +1,64 @@
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';
1
+ import { Linker } from '../linked-list/Linker';
8
2
  import { IsArrayable } from '../../recipes/IsArrayable';
9
- import { IsLinker } from '../../recipes/IsLinker';
10
- import { completeResponse } from '../../recipes/Runnable';
3
+ import { IsStack } from '../../recipes/IsStack';
11
4
  /**
12
- * Store a collection of items which can only be inserted and removed from the top.
5
+ * A last-in-first-out collection: items are added to the top with push and taken from the top with pop. Any value can
6
+ * be stacked (it is stored as it is, whether it is a function, an object or null), and adding and taking are constant
7
+ * time. To stack tasks which are run as they are taken use TaskStack.
13
8
  */
14
- export declare class Stack {
15
- /** The list which stores the stackables, the first is the top of the stack. */
9
+ export declare class Stack<T = any> implements IsStack<T>, Iterable<T> {
10
+ /** The list which stores the stacked items, the first is the top of the stack. */
16
11
  stackedList: IsArrayable<any>;
17
- private listClass;
18
- private stackableClass;
12
+ private readonly linkerClass;
19
13
  /**
20
- * Instantiate the state with the starter stacked list.
21
- * @param {Iterable|LinkedList} [stackedList=null] The list of stackables to start in this stack.
22
- * @param {IsArrayable} [listClass=LinkedList] The type of list to create when no stacked list is given.
23
- * @param {Stackable} [stackableClass=Stackable] The class used to wrap stacked items.
14
+ * Instantiate the stack, optionally with a list of items to start from.
15
+ * @param {IsArrayable|null} [stackedList=null] The list of linkers to start in this stack (the first is the top)
16
+ * @param {IsArrayable} [listClass=LinkedList] The type of list to create when no stacked list is given
17
+ * @param {Linker} [linkerClass=Linker] The class used to hold each stacked item
24
18
  */
25
- constructor(stackedList?: IsArrayable<any>, listClass?: any, stackableClass?: typeof Stackable);
19
+ constructor(stackedList?: IsArrayable<any> | null, listClass?: any, linkerClass?: typeof Linker);
26
20
  /**
27
- * Return true if the stack is empty (there are no tasks in the stacked list)
21
+ * Check whether the stack has no items.
28
22
  * @return {boolean}
29
23
  */
30
24
  empty(): boolean;
31
25
  /**
32
- * Take a look at the next stacked task
33
- * @return {Stackable}
34
- */
35
- top(): IsLinker;
36
- /**
37
- * Remove the next stacked task and return it.
38
- * @return {Stackable|null}
26
+ * Look at the item on the top of the stack, without removing it.
27
+ * @return {*|null} The item, or null when the stack is empty
39
28
  */
40
- pop(): Stackable | completeResponse | null;
29
+ peek(): T | null;
41
30
  /**
42
- * Push a stackable task to the top of the stack.
43
- * @param {Stackable|*} stackable Add a new stackable to the top of the stack
31
+ * Take the item from the top of the stack.
32
+ * @return {*|null} The item, or null when the stack is empty
44
33
  */
45
- push(stackable: any): void;
34
+ pop(): T | null;
46
35
  /**
47
- * Remove the next stacked task and return it.
48
- * @return {Stackable|null}
36
+ * Add an item to the top of the stack.
37
+ * @param {*} data The item to add
38
+ * @return {Stack} This stack, so that adding can be chained
49
39
  */
50
- remove(): Stackable | null;
40
+ push(data: T): this;
51
41
  /**
52
- * Get the size of the current stack.
42
+ * Count the items in the stack.
53
43
  * @return {number}
54
44
  */
55
45
  size(): number;
56
46
  /**
57
- * Convert an array to a Stack.
58
- * @param {Array} values An array of values which will be converted to stackables in this queue
59
- * @param {Stackable} stackableClass The class to use for each stackable
60
- * @param {Stack|Iterable} listClass The class to use to manage the stackables
47
+ * The item on the top of the stack (the same as peek).
48
+ * @return {*|null} The item, or null when the stack is empty
49
+ */
50
+ top(): T | null;
51
+ /**
52
+ * Iterate over the items from the top of the stack to the bottom, without removing them.
53
+ * @return {Iterator}
54
+ */
55
+ [Symbol.iterator](): Iterator<T>;
56
+ /**
57
+ * Convert an array to a Stack by pushing each value in turn, so the last value is on the top.
58
+ * @param {Array} [values=[]] The items to stack
59
+ * @param {IsArrayable} [listClass=LinkedList] The type of list used to store the items
60
+ * @param {Linker} [linkerClass=Linker] The class used to hold each stacked item
61
61
  * @returns {Stack}
62
62
  */
63
- static fromArray: (values?: Array<any>, stackableClass?: typeof Stackable, listClass?: any) => Stack;
63
+ static fromArray: <T_1 = any>(values?: Array<T_1>, listClass?: any, linkerClass?: typeof Linker) => Stack<T_1>;
64
64
  }
@@ -4,36 +4,36 @@ Object.defineProperty(exports, '__esModule', {
4
4
  value: true
5
5
  })
6
6
  exports.Stack = void 0
7
- var _Stackable = require('./Stackable')
7
+ require('core-js/modules/esnext.iterator.constructor.js')
8
+ require('core-js/modules/esnext.iterator.for-each.js')
8
9
  var _LinkedList = require('../linked-list/LinkedList')
10
+ var _Linker = require('../linked-list/Linker')
9
11
  /**
10
12
  * @file stack.
11
13
  * @author Joshua Heagle <joshuaheagle@gmail.com>
12
- * @version 1.0.0
14
+ * @version 2.0.0
13
15
  * @memberOf module:collect-your-stuff
14
16
  */
15
17
 
16
18
  /**
17
- * Store a collection of items which can only be inserted and removed from the top.
19
+ * A last-in-first-out collection: items are added to the top with push and taken from the top with pop. Any value can
20
+ * be stacked (it is stored as it is, whether it is a function, an object or null), and adding and taking are constant
21
+ * time. To stack tasks which are run as they are taken use TaskStack.
18
22
  */
19
23
  class Stack {
20
24
  /**
21
- * Instantiate the state with the starter stacked list.
22
- * @param {Iterable|LinkedList} [stackedList=null] The list of stackables to start in this stack.
23
- * @param {IsArrayable} [listClass=LinkedList] The type of list to create when no stacked list is given.
24
- * @param {Stackable} [stackableClass=Stackable] The class used to wrap stacked items.
25
+ * Instantiate the stack, optionally with a list of items to start from.
26
+ * @param {IsArrayable|null} [stackedList=null] The list of linkers to start in this stack (the first is the top)
27
+ * @param {IsArrayable} [listClass=LinkedList] The type of list to create when no stacked list is given
28
+ * @param {Linker} [linkerClass=Linker] The class used to hold each stacked item
25
29
  */
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
30
+ constructor (stackedList = null, listClass = _LinkedList.LinkedList, linkerClass = _Linker.Linker) {
31
+ this.linkerClass = linkerClass
32
+ this.stackedList = stackedList === null ? new listClass(linkerClass) : stackedList
33
33
  }
34
34
 
35
35
  /**
36
- * Return true if the stack is empty (there are no tasks in the stacked list)
36
+ * Check whether the stack has no items.
37
37
  * @return {boolean}
38
38
  */
39
39
  empty () {
@@ -41,66 +41,87 @@ class Stack {
41
41
  }
42
42
 
43
43
  /**
44
- * Take a look at the next stacked task
45
- * @return {Stackable}
44
+ * Look at the item on the top of the stack, without removing it.
45
+ * @return {*|null} The item, or null when the stack is empty
46
46
  */
47
- top () {
48
- return this.stackedList.first
47
+ peek () {
48
+ const top = this.stackedList.first
49
+ return top === null || typeof top === 'undefined' ? null : top.data
49
50
  }
50
51
 
51
52
  /**
52
- * Remove the next stacked task and return it.
53
- * @return {Stackable|null}
53
+ * Take the item from the top of the stack.
54
+ * @return {*|null} The item, or null when the stack is empty
54
55
  */
55
56
  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
- }
57
+ const top = this.stackedList.first
58
+ if (top === null || typeof top === 'undefined') {
59
+ return null
63
60
  }
64
- return next.run()
61
+ this.stackedList.remove(top)
62
+ return top.data
65
63
  }
66
64
 
67
65
  /**
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
66
+ * Add an item to the top of the stack.
67
+ * @param {*} data The item to add
68
+ * @return {Stack} This stack, so that adding can be chained
70
69
  */
71
- push (stackable) {
72
- this.stackedList.prepend(stackable)
70
+ push (data) {
71
+ // The item is wrapped here rather than left to the list, since the list treats objects that look like a linker's
72
+ // settings (they have a data property) as such, and a stack must give back exactly what it was given
73
+ this.stackedList.prepend(new this.linkerClass({
74
+ data
75
+ }))
76
+ return this
73
77
  }
74
78
 
75
79
  /**
76
- * Remove the next stacked task and return it.
77
- * @return {Stackable|null}
80
+ * Count the items in the stack.
81
+ * @return {number}
78
82
  */
79
- remove () {
80
- if (this.empty()) {
81
- return null
82
- }
83
- return this.stackedList.remove(this.stackedList.first)
83
+ size () {
84
+ return this.stackedList.length
84
85
  }
85
86
 
86
87
  /**
87
- * Get the size of the current stack.
88
- * @return {number}
88
+ * The item on the top of the stack (the same as peek).
89
+ * @return {*|null} The item, or null when the stack is empty
89
90
  */
90
- size () {
91
- return this.stackedList.length
91
+ top () {
92
+ return this.peek()
93
+ }
94
+
95
+ /**
96
+ * Iterate over the items from the top of the stack to the bottom, without removing them.
97
+ * @return {Iterator}
98
+ */
99
+ [Symbol.iterator] () {
100
+ const linkers = this.stackedList[Symbol.iterator]()
101
+ return {
102
+ next: () => {
103
+ const result = linkers.next()
104
+ return result.done ? {
105
+ done: true,
106
+ value: undefined
107
+ } : {
108
+ done: false,
109
+ value: result.value.data
110
+ }
111
+ }
112
+ }
92
113
  }
93
114
  }
94
115
  /**
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
116
+ * Convert an array to a Stack by pushing each value in turn, so the last value is on the top.
117
+ * @param {Array} [values=[]] The items to stack
118
+ * @param {IsArrayable} [listClass=LinkedList] The type of list used to store the items
119
+ * @param {Linker} [linkerClass=Linker] The class used to hold each stacked item
99
120
  * @returns {Stack}
100
121
  */
101
122
  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, listClass, stackableClass)
123
+ Stack.fromArray = (values = [], listClass = _LinkedList.LinkedList, linkerClass = _Linker.Linker) => {
124
+ const stack = new Stack(null, listClass, linkerClass)
125
+ values.forEach(value => stack.push(value))
126
+ return stack
106
127
  }
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Stack=void 0;var _Stackable=require("./Stackable"),_LinkedList=require("../linked-list/LinkedList");class Stack{constructor(t=null,e=_LinkedList.LinkedList,s=_Stackable.Stackable){this.listClass=e,this.stackableClass=s,null===t&&(t=new e(s)),this.stackedList=t}empty(){return this.size()<=0}top(){return this.stackedList.first}pop(){const t=this.remove();return t?t.run():{success:"No more stackable tasks in the stack",error:!1,context:this.stackedList}}push(t){this.stackedList.prepend(t)}remove(){return this.empty()?null:this.stackedList.remove(this.stackedList.first)}size(){return this.stackedList.length}}exports.Stack=Stack,Stack.fromArray=(t=[],e=_Stackable.Stackable,s=_LinkedList.LinkedList)=>{const i=new s(e);return i.initialize(e.fromArray(t,e).head),new Stack(i,s,e)};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Stack=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.for-each.js");var _LinkedList=require("../linked-list/LinkedList"),_Linker=require("../linked-list/Linker");class Stack{constructor(e=null,t=_LinkedList.LinkedList,s=_Linker.Linker){this.linkerClass=s,this.stackedList=null===e?new t(s):e}empty(){return this.size()<=0}peek(){const e=this.stackedList.first;return null==e?null:e.data}pop(){const e=this.stackedList.first;return null==e?null:(this.stackedList.remove(e),e.data)}push(e){return this.stackedList.prepend(new this.linkerClass({data:e})),this}size(){return this.stackedList.length}top(){return this.peek()}[Symbol.iterator](){const e=this.stackedList[Symbol.iterator]();return{next:()=>{const t=e.next();return t.done?{done:!0,value:void 0}:{done:!1,value:t.value.data}}}}}exports.Stack=Stack,Stack.fromArray=(e=[],t=_LinkedList.LinkedList,s=_Linker.Linker)=>{const r=new Stack(null,t,s);return e.forEach((e=>r.push(e))),r};
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @file task 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 tasks (Stackables) which can only be inserted and removed from the top: pop() takes the task from
13
+ * the top and RUNS it. For a plain last-in-first-out collection of items use Stack.
14
+ */
15
+ export declare class TaskStack {
16
+ /** The list which stores the stackables, the first is the top of the stack. */
17
+ stackedList: IsArrayable<any>;
18
+ private listClass;
19
+ private stackableClass;
20
+ /**
21
+ * Instantiate the state with the starter stacked list.
22
+ * @param {Iterable|LinkedList} [stackedList=null] The list of stackables to start in this stack.
23
+ * @param {IsArrayable} [listClass=LinkedList] The type of list to create when no stacked list is given.
24
+ * @param {Stackable} [stackableClass=Stackable] The class used to wrap stacked items.
25
+ */
26
+ constructor(stackedList?: IsArrayable<any>, listClass?: any, stackableClass?: typeof Stackable);
27
+ /**
28
+ * Return true if the stack is empty (there are no tasks in the stacked list)
29
+ * @return {boolean}
30
+ */
31
+ empty(): boolean;
32
+ /**
33
+ * Take a look at the next stacked task
34
+ * @return {Stackable}
35
+ */
36
+ top(): IsLinker;
37
+ /**
38
+ * Remove the next stacked task and return it.
39
+ * @return {Stackable|null}
40
+ */
41
+ pop(): Stackable | completeResponse | null;
42
+ /**
43
+ * Push a stackable task to the top of the stack.
44
+ * @param {Stackable|*} stackable Add a new stackable to the top of the stack
45
+ */
46
+ push(stackable: any): void;
47
+ /**
48
+ * Remove the next stacked task and return it.
49
+ * @return {Stackable|null}
50
+ */
51
+ remove(): Stackable | null;
52
+ /**
53
+ * Get the size of the current stack.
54
+ * @return {number}
55
+ */
56
+ size(): number;
57
+ /**
58
+ * Convert an array to a TaskStack.
59
+ * @param {Array} values An array of values which will be converted to stackables in this queue
60
+ * @param {Stackable} stackableClass The class to use for each stackable
61
+ * @param {TaskStack|Iterable} listClass The class to use to manage the stackables
62
+ * @returns {TaskStack}
63
+ */
64
+ static fromArray: (values?: Array<any>, stackableClass?: typeof Stackable, listClass?: any) => TaskStack;
65
+ }