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,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _Queueable = _interopRequireDefault(require("./Queueable"));
|
|
8
|
+
var _LinkedList = _interopRequireDefault(require("../linked-list/LinkedList"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
/**
|
|
11
|
+
* @file queue
|
|
12
|
+
* @author Joshua Heagle <joshuaheagle@gmail.com>
|
|
13
|
+
* @version 1.0.0
|
|
14
|
+
* @memberOf module:collect-your-stuff
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Maintain a series of queued items.
|
|
19
|
+
* @see [Java Queue Interface]{@link http://www.cs.williams.edu/~freund/cs136-073/javadoc/structure5/structure5/Queue.html}
|
|
20
|
+
*/
|
|
21
|
+
class Queue {
|
|
22
|
+
queuedList = [];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Instantiate the queue with the given queue list.
|
|
26
|
+
* @param {Iterable|LinkedList} queuedList
|
|
27
|
+
*/
|
|
28
|
+
constructor() {
|
|
29
|
+
let queuedList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
30
|
+
this.queuedList = queuedList;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Return the internal queued list.
|
|
35
|
+
* @return {Iterable|LinkedList}
|
|
36
|
+
*/
|
|
37
|
+
get queuedList() {
|
|
38
|
+
return this.queuedList;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Add a queued task to the end of the queue
|
|
43
|
+
* @param {Queueable|*} queueable
|
|
44
|
+
*/
|
|
45
|
+
add(queueable) {
|
|
46
|
+
this.queuedList.append(queueable);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Take a queued task from the front of the queue and run it if ready.
|
|
51
|
+
* @return {completeResponse|*}
|
|
52
|
+
*/
|
|
53
|
+
dequeue() {
|
|
54
|
+
const next = this.remove();
|
|
55
|
+
if (!next) {
|
|
56
|
+
return {
|
|
57
|
+
success: 'No more queueable tasks in the queue',
|
|
58
|
+
error: false,
|
|
59
|
+
context: this.queuedList
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (next.complete) {
|
|
63
|
+
// Previously ran queued, run next
|
|
64
|
+
return this.dequeue();
|
|
65
|
+
}
|
|
66
|
+
if (next.running) {
|
|
67
|
+
return {
|
|
68
|
+
success: false,
|
|
69
|
+
error: 'The queue has been blocked by an unfinished task.',
|
|
70
|
+
context: next
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
// Place back in queue to be checked once again next time
|
|
74
|
+
this.enqueue(next);
|
|
75
|
+
if (next.isReady) {
|
|
76
|
+
return next.run.call(next);
|
|
77
|
+
}
|
|
78
|
+
// 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
|
|
79
|
+
// Also, we want the loop handled externally
|
|
80
|
+
return {
|
|
81
|
+
success: false,
|
|
82
|
+
error: 'Unable to find ready task.',
|
|
83
|
+
context: next
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Return true if the queue is empty (there are no tasks in the queue list)
|
|
89
|
+
* @return {boolean}
|
|
90
|
+
*/
|
|
91
|
+
empty() {
|
|
92
|
+
return this.size() <= 0;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Add a queued task to the end of the queue (alias for 'add()')
|
|
97
|
+
* @param {Queueable} queueable
|
|
98
|
+
*/
|
|
99
|
+
enqueue(queueable) {
|
|
100
|
+
this.add(queueable);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Return a reference to the next queued task.
|
|
105
|
+
* @return {Queueable}
|
|
106
|
+
*/
|
|
107
|
+
get() {
|
|
108
|
+
return this.queuedList.first;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Return a reference to the next queued / first queued task (alias for 'get()')
|
|
113
|
+
* @return {Queueable}
|
|
114
|
+
*/
|
|
115
|
+
getFirst() {
|
|
116
|
+
return this.get();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Take a look at the next queued task (alias for 'get()')
|
|
121
|
+
* @return {Queueable}
|
|
122
|
+
*/
|
|
123
|
+
peek() {
|
|
124
|
+
return this.get();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Remove the next queued item and return it.
|
|
129
|
+
* @return {Queueable|null}
|
|
130
|
+
*/
|
|
131
|
+
remove() {
|
|
132
|
+
if (this.empty()) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
return this.queuedList.remove(this.queuedList.first);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Get the length of the current queue.
|
|
140
|
+
* @return {number}
|
|
141
|
+
*/
|
|
142
|
+
size() {
|
|
143
|
+
return this.queuedList.length;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Convert an array to a Queue.
|
|
149
|
+
* @methodof Queue
|
|
150
|
+
* @param {Array} values
|
|
151
|
+
* @param {Queueable} queueableClass
|
|
152
|
+
* @param {Queue|Iterable} listClass
|
|
153
|
+
* @returns {Queue}
|
|
154
|
+
*/
|
|
155
|
+
Queue.fromArray = function () {
|
|
156
|
+
let values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
157
|
+
let queueableClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _Queueable.default;
|
|
158
|
+
let listClass = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _LinkedList.default;
|
|
159
|
+
const list = new listClass(queueableClass);
|
|
160
|
+
list.initialize(queueableClass.fromArray(values, queueableClass).head);
|
|
161
|
+
return new Queue(list);
|
|
162
|
+
};
|
|
163
|
+
var _default = exports.default = Queue;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _Queueable=_interopRequireDefault(require("./Queueable")),_LinkedList=_interopRequireDefault(require("../linked-list/LinkedList"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}class Queue{queuedList=[];constructor(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.queuedList=e}get queuedList(){return this.queuedList}add(e){this.queuedList.append(e)}dequeue(){const e=this.remove();return e?e.complete?this.dequeue():e.running?{success:!1,error:"The queue has been blocked by an unfinished task.",context:e}:(this.enqueue(e),e.isReady?e.run.call(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.add(e)}get(){return this.queuedList.first}getFirst(){return this.get()}peek(){return this.get()}remove(){return this.empty()?null:this.queuedList.remove(this.queuedList.first)}size(){return this.queuedList.length}}Queue.fromArray=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:_Queueable.default;const u=new(arguments.length>2&&void 0!==arguments[2]?arguments[2]:_LinkedList.default)(t);return u.initialize(t.fromArray(e,t).head),new Queue(u)};var _default=exports.default=Queue;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _Linker = _interopRequireDefault(require("../linked-list/Linker"));
|
|
8
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
|
+
/**
|
|
10
|
+
* @file queueable item.
|
|
11
|
+
* @author Joshua Heagle <joshuaheagle@gmail.com>
|
|
12
|
+
* @version 1.0.0
|
|
13
|
+
* @memberOf module:collect-your-stuff
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Return results of the task.
|
|
18
|
+
* @typedef {Object} completeResponse
|
|
19
|
+
* @property {*} success
|
|
20
|
+
* @property {*} error
|
|
21
|
+
* @property {*} context
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Queueable represents a runnable entry in a queue.
|
|
25
|
+
* @extends Linker
|
|
26
|
+
* @extends Runnable
|
|
27
|
+
*/
|
|
28
|
+
class Queueable extends _Linker.default {
|
|
29
|
+
complete = false;
|
|
30
|
+
ready = false;
|
|
31
|
+
running = false;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Create a queueable item that can be used in a queue.
|
|
35
|
+
* @param {Object} [queueableData={}]
|
|
36
|
+
* @param {*} [queueableData.task=null]
|
|
37
|
+
* @param {boolean|Function} [queueableData.ready=false]
|
|
38
|
+
* @param {Queueable} [queueableClass=Queueable]
|
|
39
|
+
*/
|
|
40
|
+
constructor() {
|
|
41
|
+
let {
|
|
42
|
+
task = null,
|
|
43
|
+
ready = false
|
|
44
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
45
|
+
let queueableClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Queueable;
|
|
46
|
+
super({
|
|
47
|
+
data: task
|
|
48
|
+
}, queueableClass);
|
|
49
|
+
this.complete = false;
|
|
50
|
+
this.ready = ready;
|
|
51
|
+
this.running = false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Check complete state.
|
|
56
|
+
* @return {boolean}
|
|
57
|
+
*/
|
|
58
|
+
get complete() {
|
|
59
|
+
return this.complete;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Check ready state.
|
|
64
|
+
* @return {boolean}
|
|
65
|
+
*/
|
|
66
|
+
get isReady() {
|
|
67
|
+
return typeof this.ready === 'function' ? this.ready() : this.ready;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Check running state.
|
|
72
|
+
* @return {boolean}
|
|
73
|
+
*/
|
|
74
|
+
get running() {
|
|
75
|
+
return this.running;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Retrieve the data which should be formed as a task.
|
|
80
|
+
* @return {*}
|
|
81
|
+
*/
|
|
82
|
+
get task() {
|
|
83
|
+
if (typeof this.data === 'function') {
|
|
84
|
+
return this.data;
|
|
85
|
+
}
|
|
86
|
+
return complete => typeof complete === 'function' ? complete({
|
|
87
|
+
context: this.data
|
|
88
|
+
}).context : this.data;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Set this queueable as completed.
|
|
93
|
+
* @param {Object} completeResponse
|
|
94
|
+
* @param {*} [completeResponse.success=true]
|
|
95
|
+
* @param {*} [completeResponse.error=false]
|
|
96
|
+
* @param {*} [completeResponse.context=null]
|
|
97
|
+
* @return {completeResponse}
|
|
98
|
+
*/
|
|
99
|
+
markCompleted() {
|
|
100
|
+
let {
|
|
101
|
+
success = true,
|
|
102
|
+
error = false,
|
|
103
|
+
context = null
|
|
104
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
105
|
+
this.complete = true;
|
|
106
|
+
this.running = false;
|
|
107
|
+
return {
|
|
108
|
+
success: success,
|
|
109
|
+
error: error,
|
|
110
|
+
context: context
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Intend to run the queued task when it is ready. If ready, mark this task as running and run the task.
|
|
116
|
+
* @return {completeResponse}
|
|
117
|
+
*/
|
|
118
|
+
run() {
|
|
119
|
+
if (!this.isReady) {
|
|
120
|
+
return {
|
|
121
|
+
success: false,
|
|
122
|
+
error: 'Task is not ready',
|
|
123
|
+
context: this.data
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
if (this.running) {
|
|
127
|
+
return {
|
|
128
|
+
success: false,
|
|
129
|
+
error: 'Queued task is already running, possible missing \'complete\' callback',
|
|
130
|
+
context: this.data
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
this.running = true;
|
|
134
|
+
return this.task(this.markCompleted.bind(this));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Make a new Queueable from the data given if it is not already a valid Queueable.
|
|
140
|
+
* @methodof Queueable
|
|
141
|
+
* @param {Queueable|*} queueable
|
|
142
|
+
* @param {Queueable} [queueableClass=Queueable]
|
|
143
|
+
* @return {Queueable}
|
|
144
|
+
*/
|
|
145
|
+
Queueable.make = function (queueable) {
|
|
146
|
+
let queueableClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Queueable;
|
|
147
|
+
if (typeof queueable !== 'object') {
|
|
148
|
+
// It is not an object, so instantiate the Queueable with queueable as the data
|
|
149
|
+
return new queueableClass({
|
|
150
|
+
task: queueable
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
if (queueable.classType) {
|
|
154
|
+
// Already valid Queueable, return as-is
|
|
155
|
+
return queueable;
|
|
156
|
+
}
|
|
157
|
+
if (!queueable.task) {
|
|
158
|
+
queueable = {
|
|
159
|
+
task: queueable
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
// Create the new node as the configured queueableClass
|
|
163
|
+
return new queueableClass(queueable, queueableClass);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Convert an array into Queueable instances, return the head and tail Queueables.
|
|
168
|
+
* @param {Array} values
|
|
169
|
+
* @param {Queueable} queueableClass
|
|
170
|
+
* @returns {{head: Queueable, tail: Queueable}}
|
|
171
|
+
*/
|
|
172
|
+
Queueable.fromArray = function () {
|
|
173
|
+
let values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
174
|
+
let queueableClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Queueable;
|
|
175
|
+
return _Linker.default.fromArray(values, queueableClass);
|
|
176
|
+
};
|
|
177
|
+
var _default = exports.default = Queueable;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _Linker=_interopRequireDefault(require("../linked-list/Linker"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}class Queueable extends _Linker.default{complete=!1;ready=!1;running=!1;constructor(){let{task:e=null,ready:t=!1}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};super({data:e},arguments.length>1&&void 0!==arguments[1]?arguments[1]:Queueable),this.complete=!1,this.ready=t,this.running=!1}get complete(){return this.complete}get isReady(){return"function"==typeof this.ready?this.ready():this.ready}get running(){return this.running}get task(){return"function"==typeof this.data?this.data:e=>"function"==typeof e?e({context:this.data}).context:this.data}markCompleted(){let{success:e=!0,error:t=!1,context:r=null}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return this.complete=!0,this.running=!1,{success:e,error:t,context:r}}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}}}Queueable.make=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Queueable;return"object"!=typeof e?new t({task:e}):e.classType?e:(e.task||(e={task:e}),new t(e,t))},Queueable.fromArray=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Queueable;return _Linker.default.fromArray(e,t)};var _default=exports.default=Queueable;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _Stackable = _interopRequireDefault(require("./Stackable"));
|
|
8
|
+
var _Arrayable = _interopRequireDefault(require("../arrayable/Arrayable"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
/**
|
|
11
|
+
* @file stack.
|
|
12
|
+
* @author Joshua Heagle <joshuaheagle@gmail.com>
|
|
13
|
+
* @version 1.0.0
|
|
14
|
+
* @memberOf module:collect-your-stuff
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Store a collection of items which can only be inserted and removed from the top.
|
|
19
|
+
* @see [Java Stack Interface]{@link http://www.cs.williams.edu/~freund/cs136-073/javadoc/structure5/structure5/Stack.html}
|
|
20
|
+
*/
|
|
21
|
+
class Stack {
|
|
22
|
+
stackedList = [];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Instantiate the state with the starter stacked list.
|
|
26
|
+
* @param {Iterable|Arrayable} stackedList
|
|
27
|
+
*/
|
|
28
|
+
constructor() {
|
|
29
|
+
let stackedList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
30
|
+
this.stackedList = stackedList;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Return the internal stacked list.
|
|
35
|
+
* @return {Iterable|LinkedList}
|
|
36
|
+
*/
|
|
37
|
+
get stackedList() {
|
|
38
|
+
return this.stackedList;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Add a stackable task to the top of the stack.
|
|
43
|
+
* @param {Stackable|*} stackable
|
|
44
|
+
*/
|
|
45
|
+
add(stackable) {
|
|
46
|
+
this.stackedList.append(stackable);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Return true if the stack is empty (there are no tasks in the stacked list)
|
|
51
|
+
* @return {boolean}
|
|
52
|
+
*/
|
|
53
|
+
empty() {
|
|
54
|
+
return this.size() <= 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Return a reference to the next stacked task.
|
|
59
|
+
* @return {Stackable}
|
|
60
|
+
*/
|
|
61
|
+
get() {
|
|
62
|
+
return this.stackedList.last;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Return a reference to the next stacked / first stacked task (alias for 'get()')
|
|
67
|
+
* @return {Stackable}
|
|
68
|
+
*/
|
|
69
|
+
getFirst() {
|
|
70
|
+
return this.get();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Take a look at the next stacked task (alias for 'get()')
|
|
75
|
+
* @return {Stackable}
|
|
76
|
+
*/
|
|
77
|
+
peek() {
|
|
78
|
+
return this.get();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Remove the next stacked task and return it.
|
|
83
|
+
* @return {Stackable|null}
|
|
84
|
+
*/
|
|
85
|
+
pop() {
|
|
86
|
+
const next = this.remove();
|
|
87
|
+
if (!next) {
|
|
88
|
+
return {
|
|
89
|
+
success: 'No more stackable tasks in the stack',
|
|
90
|
+
error: false,
|
|
91
|
+
context: this.stackedList
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return next.run();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Push a stackable task to the top of the stack.
|
|
99
|
+
* @param {Stackable|*} stackable
|
|
100
|
+
*/
|
|
101
|
+
push(stackable) {
|
|
102
|
+
this.add(stackable);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Remove the next stacked task and return it.
|
|
107
|
+
* @return {Stackable|null}
|
|
108
|
+
*/
|
|
109
|
+
remove() {
|
|
110
|
+
if (this.empty()) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
return this.stackedList.remove(this.stackedList.last);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Get the size of the current stack.
|
|
118
|
+
* @return {number}
|
|
119
|
+
*/
|
|
120
|
+
size() {
|
|
121
|
+
return this.stackedList.length;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Convert an array to a Stack.
|
|
127
|
+
* @methodof Stack
|
|
128
|
+
* @param {Array} values
|
|
129
|
+
* @param {Stackable} stackableClass
|
|
130
|
+
* @param {Stack|Iterable} listClass
|
|
131
|
+
* @returns {Stack}
|
|
132
|
+
*/
|
|
133
|
+
Stack.fromArray = function () {
|
|
134
|
+
let values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
135
|
+
let stackableClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _Stackable.default;
|
|
136
|
+
let listClass = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _Arrayable.default;
|
|
137
|
+
const list = new listClass(stackableClass);
|
|
138
|
+
list.initialize(stackableClass.fromArray(values, stackableClass).head);
|
|
139
|
+
return new Stack(list);
|
|
140
|
+
};
|
|
141
|
+
var _default = exports.default = Stack;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _Stackable=_interopRequireDefault(require("./Stackable")),_Arrayable=_interopRequireDefault(require("../arrayable/Arrayable"));function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}class Stack{stackedList=[];constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.stackedList=t}get stackedList(){return this.stackedList}add(t){this.stackedList.append(t)}empty(){return this.size()<=0}get(){return this.stackedList.last}getFirst(){return this.get()}peek(){return this.get()}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.add(t)}remove(){return this.empty()?null:this.stackedList.remove(this.stackedList.last)}size(){return this.stackedList.length}}Stack.fromArray=function(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:_Stackable.default;const r=new(arguments.length>2&&void 0!==arguments[2]?arguments[2]:_Arrayable.default)(e);return r.initialize(e.fromArray(t,e).head),new Stack(r)};var _default=exports.default=Stack;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true
|
|
5
|
+
})
|
|
6
|
+
exports.default = void 0
|
|
7
|
+
var _ArrayElement = _interopRequireDefault(require('../arrayable/ArrayElement'))
|
|
8
|
+
function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { default: obj } }
|
|
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
|
+
* Stackable represents a runnable entry in stack.
|
|
18
|
+
* @extends ArrayElement
|
|
19
|
+
* @extends Runnable
|
|
20
|
+
*/
|
|
21
|
+
class Stackable extends _ArrayElement.default {
|
|
22
|
+
/**
|
|
23
|
+
* Instantiate the Stackable which is used in a stack.
|
|
24
|
+
* @param {*} [stack=null]
|
|
25
|
+
* @param stackableClass
|
|
26
|
+
*/
|
|
27
|
+
constructor () {
|
|
28
|
+
const stack = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null
|
|
29
|
+
const stackableClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Stackable
|
|
30
|
+
super(stack, stackableClass)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Retrieve the data which should be formed as a task.
|
|
35
|
+
* @return {*}
|
|
36
|
+
*/
|
|
37
|
+
get task () {
|
|
38
|
+
if (typeof this.data === 'function') {
|
|
39
|
+
return this.data
|
|
40
|
+
}
|
|
41
|
+
return () => this.data
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Run the stacked task.
|
|
46
|
+
* @return {*}
|
|
47
|
+
*/
|
|
48
|
+
run () {
|
|
49
|
+
return this.task()
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Make a new Stackable from the data given if it is not already a valid Stackable.
|
|
55
|
+
* @methodof Stackable
|
|
56
|
+
* @param {Stackable|*} stackable
|
|
57
|
+
* @param {Stackable} [stackableClass=Stackable]
|
|
58
|
+
* @return {Stackable}
|
|
59
|
+
*/
|
|
60
|
+
Stackable.make = function (stackable) {
|
|
61
|
+
const stackableClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Stackable
|
|
62
|
+
if (typeof stackable !== 'object') {
|
|
63
|
+
// It is not an object, so instantiate the Stackable with stackable as the data
|
|
64
|
+
return new stackableClass(stackable)
|
|
65
|
+
}
|
|
66
|
+
if (stackable.classType) {
|
|
67
|
+
// Already valid Stackable, return as-is
|
|
68
|
+
return stackable
|
|
69
|
+
}
|
|
70
|
+
// Create the new node as the configured stackableClass
|
|
71
|
+
return new stackableClass(stackable, stackableClass)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Convert an array into Stackable instances, return the head and tail Stackables.
|
|
76
|
+
* @param {Array} [values=[]]
|
|
77
|
+
* @param {Stackable} [stackableClass=Stackable]
|
|
78
|
+
* @returns {{head: Stackable, tail: Stackable}}
|
|
79
|
+
*/
|
|
80
|
+
Stackable.fromArray = function () {
|
|
81
|
+
const values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []
|
|
82
|
+
const stackableClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Stackable
|
|
83
|
+
return _ArrayElement.default.fromArray(values, stackableClass)
|
|
84
|
+
}
|
|
85
|
+
var _default = exports.default = Stackable
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _ArrayElement=_interopRequireDefault(require("../arrayable/ArrayElement"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}class Stackable extends _ArrayElement.default{constructor(){super(arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,arguments.length>1&&void 0!==arguments[1]?arguments[1]:Stackable)}get task(){return"function"==typeof this.data?this.data:()=>this.data}run(){return this.task()}}Stackable.make=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Stackable;return"object"!=typeof e?new t(e):e.classType?e:new t(e,t)},Stackable.fromArray=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Stackable;return _ArrayElement.default.fromArray(e,t)};var _default=exports.default=Stackable;
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true
|
|
5
|
+
})
|
|
6
|
+
exports.default = void 0
|
|
7
|
+
require('core-js/stable')
|
|
8
|
+
var _Arrayable = _interopRequireDefault(require('./collections/arrayable/Arrayable'))
|
|
9
|
+
var _DoublyLinkedList = _interopRequireDefault(require('./collections/doubly-linked-llist/DoublyLinkedList'))
|
|
10
|
+
var _LinkedList = _interopRequireDefault(require('./collections/linked-list/LinkedList'))
|
|
11
|
+
var _LinkedTreeList = _interopRequireDefault(require('./collections/linked-tree-list/LinkedTreeList'))
|
|
12
|
+
var _Queue = _interopRequireDefault(require('./collections/queue/Queue'))
|
|
13
|
+
var _Stack = _interopRequireDefault(require('./collections/stack/Stack'))
|
|
14
|
+
var _recipes = _interopRequireDefault(require('./recipes/recipes'))
|
|
15
|
+
function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { default: obj } }
|
|
16
|
+
/**
|
|
17
|
+
* All of the collections available.
|
|
18
|
+
* @file
|
|
19
|
+
* @author Joshua Heagle <joshuaheagle@gmail.com>
|
|
20
|
+
* @version 1.0.0
|
|
21
|
+
* @module collect-your-stuff
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* All methods exported from this module are encapsulated within collect-your-stuff.
|
|
26
|
+
* @typedef {module:collect-your-stuff} collectYourStuff
|
|
27
|
+
*/
|
|
28
|
+
const collectYourStuff = {
|
|
29
|
+
Arrayable: _Arrayable.default,
|
|
30
|
+
DoublyLinkedList: _DoublyLinkedList.default,
|
|
31
|
+
LinkedList: _LinkedList.default,
|
|
32
|
+
LinkedTreeList: _LinkedTreeList.default,
|
|
33
|
+
Queue: _Queue.default,
|
|
34
|
+
Stack: _Stack.default,
|
|
35
|
+
recipes: _recipes.default
|
|
36
|
+
}
|
|
37
|
+
var _default = exports.default = collectYourStuff
|
|
38
|
+
if (void 0) {
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
(void 0).collectYourStuff = collectYourStuff
|
|
41
|
+
} else if (typeof window !== 'undefined') {
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
window.collectYourStuff = collectYourStuff
|
|
44
|
+
}
|
package/dist/main.min.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0,require("core-js/stable");var _Arrayable=_interopRequireDefault(require("./collections/arrayable/Arrayable")),_DoublyLinkedList=_interopRequireDefault(require("./collections/doubly-linked-llist/DoublyLinkedList")),_LinkedList=_interopRequireDefault(require("./collections/linked-list/LinkedList")),_LinkedTreeList=_interopRequireDefault(require("./collections/linked-tree-list/LinkedTreeList")),_Queue=_interopRequireDefault(require("./collections/queue/Queue")),_Stack=_interopRequireDefault(require("./collections/stack/Stack")),_recipes=_interopRequireDefault(require("./recipes/recipes"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}const collectYourStuff={Arrayable:_Arrayable.default,DoublyLinkedList:_DoublyLinkedList.default,LinkedList:_LinkedList.default,LinkedTreeList:_LinkedTreeList.default,Queue:_Queue.default,Stack:_Stack.default,recipes:_recipes.default};var _default=exports.default=collectYourStuff;"undefined"!=typeof window&&(window.collectYourStuff=collectYourStuff);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* @file Runnable class recipe.
|
|
9
|
+
* @author Joshua Heagle <joshuaheagle@gmail.com>
|
|
10
|
+
* @version 1.0.0
|
|
11
|
+
* @memberOf module:collect-your-stuff
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Identify a class that can be run.
|
|
16
|
+
*/
|
|
17
|
+
class Runnable {
|
|
18
|
+
data = null;
|
|
19
|
+
constructor() {
|
|
20
|
+
let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
21
|
+
this.data = data;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Retrieve the data which should be formed as a task.
|
|
26
|
+
* @return {Function}
|
|
27
|
+
*/
|
|
28
|
+
get task() {
|
|
29
|
+
if (typeof this.data === 'function') {
|
|
30
|
+
return this.data;
|
|
31
|
+
}
|
|
32
|
+
return () => this.data;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Run the runnable task.
|
|
37
|
+
* @return {*}
|
|
38
|
+
*/
|
|
39
|
+
run() {
|
|
40
|
+
return this.task();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Check if a given thing is Runnable
|
|
46
|
+
* @memberof Runnable
|
|
47
|
+
* @param {*} thing
|
|
48
|
+
* @return {boolean}
|
|
49
|
+
*/
|
|
50
|
+
Runnable.isRunnable = thing => {
|
|
51
|
+
if (typeof thing === 'undefined') {
|
|
52
|
+
// No argument past, this class is runnable
|
|
53
|
+
return void 0 instanceof Runnable;
|
|
54
|
+
}
|
|
55
|
+
if (typeof thing !== 'object') {
|
|
56
|
+
// It is not even an object, so cannot be a class instance
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (typeof thing.task !== 'function') {
|
|
60
|
+
// It does not have a task getter that returns a function
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Finally, it has a runnable 'run' method. This method should return the result of running the task.
|
|
65
|
+
return typeof thing.run === 'function';
|
|
66
|
+
};
|
|
67
|
+
var _default = exports.default = Runnable;
|