Skip to content

Commit f7c3a2d

Browse files
authored
Merge pull request #3 from typescript-package/develop
v1.0.0-beta
2 parents 5f68bdc + 5140121 commit f7c3a2d

19 files changed

+1034
-146
lines changed

.github/FUNDING.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: [angular-package] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4+
patreon: angularpackage # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

README.md

Lines changed: 208 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ A lightweight TypeScript library for managing various queue and stack structures
2222

2323
* [Installation](#installation)
2424
* [Api](#api)
25-
* `ProcessingQueue`
26-
* `Queue`
27-
* `Stack`
25+
* [`Elements`](#elements)
26+
* [`ProcessingQueue`](#processingqueue)
27+
* [`Processing`](#processing)
28+
* [`Queue`](#queue)
29+
* [`Stack`](#stack)
2830
* [Git](#git)
2931
* [Commit](#commit)
3032
* [Versioning](#versioning)
@@ -40,12 +42,215 @@ npm install @typescript-package/queue
4042

4143
```typescript
4244
import {
45+
// Class.
46+
Elements,
47+
// Abstract.
4348
ProcessingQueue,
4449
Queue,
4550
Stack
4651
} from '@typescript-package/queue';
4752
```
4853

54+
### `Elements`
55+
56+
```typescript
57+
import { Elements } from '@typescript-package/queue';
58+
59+
let elements = new Elements(
60+
[1, 2, 3, 4], // Array type elements.
61+
15 // Maximum size of the elements.
62+
);
63+
64+
// Appends the value at the end of an array state.
65+
elements.append(5);
66+
console.log(elements.state); // Output: [1, 2, 3, 4, 5]
67+
68+
// Inserts the value at the specified index into the array state.
69+
elements.insert(2, 10);
70+
console.log(elements.state); // Output: [1, 2, 10, 3, 4, 5]
71+
72+
// Adds the values at the beginning of array state.
73+
elements.prepend(0);
74+
console.log(elements.state); // Output: [0, 1, 2, 10, 3, 4, 5]
75+
76+
// Updates the value at the index in the array state.
77+
elements.update(0, 127);
78+
console.log(elements.state); // Output: [127, 1, 2, 10, 3, 4, 5]
79+
```
80+
81+
### `ProcessingQueue`
82+
83+
```typescript
84+
import { ProcessingQueue } from '@typescript-package/queue';
85+
86+
// Initialize the `ProcessingQueue`.
87+
let processingQueue = new ProcessingQueue(
88+
2, // concurrency
89+
10, // size
90+
1, 2, 3 // items
91+
);
92+
93+
// The maximum number of elements that can be processed concurrently.
94+
console.log(`concurrency, `, processingQueue.concurrency); // Output: 2
95+
96+
// A set containing all elements that have been successfully processed.
97+
console.log(`processed, `, processingQueue.processed); // Output: Set(0)
98+
99+
// Checks whether the queue is empty.
100+
console.log(`isEmpty(), `, processingQueue.isEmpty()); // Output: false
101+
102+
// Checks whether the queue is full.
103+
console.log(`isFull(), `, processingQueue.isFull()); // Output: false
104+
105+
// The maximum queue size.
106+
console.log(`size, `, processingQueue.size); // Output: 10
107+
108+
// The actual queue Elements state - raw array state of the queue.
109+
console.log(`state, `, processingQueue.state); // Output: [1, 2, 3]
110+
111+
// The actual queue length.
112+
console.log(`length, `, processingQueue.length); // Output: 3
113+
114+
// Adds a new element to the queue.
115+
console.log(`enqueue(4), `, processingQueue.enqueue(4));
116+
117+
// The actual queue length.
118+
console.log(`length, `, processingQueue.length); // Output: 4
119+
120+
// Returns the first element in the queue.
121+
console.log(`peek(), `, processingQueue.peek()); // Output: 1
122+
123+
// Returns the first element in the queue.
124+
console.log(`dequeue(), `, processingQueue.dequeue()); // Output: 1
125+
126+
// The actual queue length.
127+
console.log(`length, `, processingQueue.length); // Output: 3
128+
129+
// Adds to the full.
130+
processingQueue.enqueue(5).enqueue(6).enqueue(7).enqueue(8).enqueue(9).enqueue(10).enqueue(11);
131+
132+
// The actual queue Elements state - raw array state of the queue.
133+
console.log(`state, `, processingQueue.state); // Output: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
134+
135+
// Waits for all elements in the queue to be processed and returns the set of processed elements.
136+
processingQueue.isCompleted().then(
137+
processed => console.log(`Completed`, processed), // Output: Completed Set(10)
138+
reason => console.log(reason)
139+
);
140+
141+
// Starts processing elements in the queue using the provided callback function.
142+
processingQueue.run(element => console.log(`Processed`, element)); // Output: Processed {element}
143+
144+
// A set containing all elements that have been successfully processed.
145+
console.log(`processed, `, processingQueue.processed); // Output: Set(10)
146+
```
147+
148+
### `Processing`
149+
150+
### `Queue`
151+
152+
```typescript
153+
import { Queue } from '@typescript-package/queue';
154+
155+
// Initialize the new `Queue`.
156+
let queue = new Queue(
157+
10, // size
158+
1, 2, 3 // item
159+
);
160+
161+
// Adds a new element to the queue.
162+
queue.enqueue(4);
163+
164+
// The actual queue Elements state - raw array state of the queue.
165+
console.log(`state,`, queue.state); // Output: [1, 2, 3, 4]
166+
167+
// Returns the first element in the queue.
168+
console.log(`peek(), `, queue.peek()); // Output: 1
169+
170+
// Checks if the queue is empty.
171+
console.log(`isEmpty(),`, queue.isEmpty()); // Output: false
172+
173+
// The maximum queue size.
174+
console.log(`size,`, queue.size); // Output: 10
175+
176+
// The actual queue Elements state - raw array state of the queue.
177+
console.log(`state,`, queue.state); // Output: [1, 2, 3, 4]
178+
179+
// Adds to the full.
180+
queue.enqueue(5).enqueue(6).enqueue(7).enqueue(8).enqueue(9).enqueue(10);
181+
182+
// Checks whether the queue is full.
183+
console.log(`isFull(), `, queue.isFull()); // Output: true
184+
185+
try {
186+
queue.enqueue(11);
187+
} catch(error) {
188+
console.log(error); // Error: Queue is full.
189+
}
190+
191+
// Clears the queue.
192+
console.log(`clear(), `, queue.clear());
193+
194+
// Checks if the queue is empty.
195+
console.log(`isEmpty(),`, queue.isEmpty()); // Output: true
196+
197+
// The actual queue Elements state - raw array state of the queue.
198+
console.log(`state,`, queue.state); // Output: []
199+
200+
```
201+
202+
### `Stack`
203+
204+
```typescript
205+
import { Stack } from '@typescript-package/queue';
206+
207+
// Initialize the `Stack`.
208+
let stack = new Stack(
209+
10, // size
210+
1, 2, 3 // items
211+
);
212+
213+
// The actual stack length.
214+
console.log(`length, `, stack.length); // Output: 3
215+
216+
// Adds a new element on the stack.
217+
stack.push(4);
218+
219+
// The maximum stack size.
220+
console.log(`size, `, stack.size); // Output: 10
221+
222+
// The actual stack length.
223+
console.log(`length, `, stack.length); // Output: 4
224+
225+
// Returns the top element on the stack.
226+
console.log(`peek(), `, stack.peek()); // Output: 4
227+
228+
// The actual stack `Elements` state.
229+
console.log(`state, `, stack.state); // Output: [1, 2, 3, 4]
230+
231+
// Removes and returns the top element from the stack.
232+
console.log(`pop(), `, stack.pop()); // Output: 4
233+
234+
// The actual stack `Elements` state.
235+
console.log(`state, `, stack.state); // Output: [1, 2, 3]
236+
237+
// Adds to the full.
238+
stack.push(4).push(5).push(6).push(7).push(8).push(9).push(10);
239+
240+
// The actual stack `Elements` state.
241+
console.log(`state, `, stack.state); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
242+
243+
// Checks if the stack is full.
244+
console.log(`isFull(), `, stack.isFull()); // Output: true
245+
246+
// Clears the queue.
247+
stack.clear()
248+
249+
// The actual stack `Elements` state.
250+
console.log(`state, `, stack.state); // Output: []
251+
console.log(`isEmpty(), `, stack.isEmpty()); // Output: true
252+
```
253+
49254
## GIT
50255

51256
### Commit

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@typescript-package/queue",
3-
"version": "0.0.1",
3+
"version": "1.0.0-beta",
44
"author": "wwwdev.io <dev@wwwdev.io>",
55
"description": "A lightweight TypeScript library for managing various queue and stack structures.",
66
"license": "MIT",

src/lib/elements.class.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Abstract.
2+
import { ArrayState } from "@typescript-package/state";
3+
/**
4+
* @description Array state elements in data structures such as Stack and Queue.
5+
* @export
6+
* @class Elements
7+
* @template Type
8+
* @extends {ArrayState<Type>}
9+
*/
10+
export class Elements<Type, Size extends number = number> extends ArrayState<Type>{
11+
/**
12+
* @description The maximum size of the `Elements`.
13+
* @public
14+
* @readonly
15+
* @type {Size}
16+
*/
17+
public get size(): Size {
18+
return this.#size;
19+
}
20+
21+
/**
22+
* @description Privately stored maximum elements size.
23+
* @type {Size}
24+
*/
25+
#size: Size;
26+
27+
/**
28+
* Creates an instance of `Elements`.
29+
* @constructor
30+
* @param {Type[]} elements
31+
* @param {Size} [size=Infinity as Size]
32+
*/
33+
constructor(elements: Type[], size: Size = Infinity as Size) {
34+
super(elements.length <= size ? elements : []);
35+
// Sets the size.
36+
this.#size = size;
37+
// Throws an error if the elements exceeds the maximum size.
38+
if (elements.length > size) {
39+
throw new Error(`The \`elements\` size exceeds the maximum size ${size} by ${elements.length - size}.`);
40+
}
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
* @public
46+
* @param {Type} element The element of `Type` to append.
47+
* @returns {this}
48+
*/
49+
public override append(element: Type): this {
50+
this.#checkFull();
51+
super.append(element);
52+
return this;
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
* @public
58+
* @param {number} index The index under which the specified `element` is inserted.
59+
* @param {Type} element The element of `Type` to insert at specified `index`.
60+
* @returns {this}
61+
*/
62+
public override insert(index: number, element: Type): this {
63+
this.#checkFull();
64+
super.insert(index, element);
65+
return this;
66+
}
67+
68+
/**
69+
* @description Checks whether the `Elements` state is full, equal to size.
70+
* @public
71+
* @returns {boolean}
72+
*/
73+
public isFull(): boolean {
74+
return this.#size === this.length;
75+
}
76+
77+
/**
78+
* @description Add the element at the beginning of `array` state.
79+
* @public
80+
* @param {Type} element The element of `Type` to prepend.
81+
* @returns {this}
82+
*/
83+
public override prepend(element: Type): this {
84+
this.#checkFull();
85+
super.prepend(element);
86+
return this;
87+
}
88+
89+
/**
90+
* @inheritdoc
91+
* @public
92+
* @param {number} index The index to update update element.
93+
* @param {Type} element The element of `Type` to update under the specified `index`.
94+
* @returns {this}
95+
*/
96+
public override update(index: number, element: Type): this {
97+
super.update(index, element);
98+
return this;
99+
}
100+
101+
/**
102+
* @description Checks whether length of the array is equal to maximum size.
103+
* @returns {this}
104+
*/
105+
#checkFull(): this {
106+
if (this.isFull()) {
107+
throw new Error(`Elements array state is full of size ${super.length}.`);
108+
}
109+
return this;
110+
}
111+
};

src/lib/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Class.
2+
export { Elements } from './elements.class';
3+
export { Processing } from './processing.class';
4+
// Abstract.
15
export { ProcessingQueue } from './processing-queue.class';
26
export { Queue } from './queue.abstract';
37
export { Stack } from './stack.abstract';

0 commit comments

Comments
 (0)