Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/can-dy-jack/queue
Queue implementation in JavaScript (based on Array)
https://github.com/can-dy-jack/queue
data-structures implementation javascript queue
Last synced: about 1 month ago
JSON representation
Queue implementation in JavaScript (based on Array)
- Host: GitHub
- URL: https://github.com/can-dy-jack/queue
- Owner: can-dy-jack
- License: mit
- Created: 2023-01-18T02:33:58.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-18T04:08:43.000Z (about 2 years ago)
- Last Synced: 2024-04-26T06:04:30.488Z (9 months ago)
- Topics: data-structures, implementation, javascript, queue
- Language: JavaScript
- Homepage:
- Size: 39.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# queue
Queue implementation in JavaScript (based on Array)> 队列:先入先出
## install
```sh
npm i @kartjim/queue
```## export
```js
const { Queue } = require('@kartjim/queue');
```or ESM :
```js
import { Queue } from "@kartjim/queue";
```
## Queue API
```ts
export class Queue {
constructor(arr?: T[]);
isEmpty(): boolean;
size(): number;
front(): A;
end(): A;
push(val: A): Queue;
pop(): A;
toArray(): A[];
clear(): void;
}
```
### constructor
create a Queue from an Array.```js
const queue = new Queue();
// or
// new Queue([1, 2, 3]);
```### isEmpty
Checks if the Queue is empty.
```js
console.log(stack.isEmpty()) // true
queue.push(1);
console.log(queue.isEmpty()) // false
```### size
return the number of elements in the queue.
```js
console.log(queue.size()) // 1
```
### push
push an element to the top of the queue.
```js
queue.push(2)
console.log(stack.front()) // 1
console.log(stack.end()) // 2
```
### front
return the element at the front of the queue.
```js
queue.push(5);
console.log(stack.front()) // 1
```### end
return the element at the end of the queue.
```js
queue.push(6);
console.log(stack.end()) // 6
console.log(stack.size()) // 4
```### pop
remove and return the top element in the queue.
```js
console.log(queue.pop()) // 1
console.log(queue.pop()) // 2
console.log(queue.pop()) // 5
console.log(queue.pop()) // 6
console.log(queue.pop()) // undefined
```
### toArray
return an array of elements in the queue.
```js
queue.push(8).push(9).push(11);
console.log(queue.toArray()) // [8, 9, 11]
```
### clear
remove all elements from the queue.
```js
queue.clear();
console.log(queue.size()) // 0
console.log(queue.isEmpty()) // true
```
## Coverage
`🚀 grunt coverage`:```sh
===================== Coverage summary =====================
Statements : 100% ( 80/80 )
Branches : 100% ( 6/6 )
Functions : 100% ( 9/9 )
Lines : 100% ( 80/80 )
============================================================
```