Skip to content

Commit 8c4e984

Browse files
committed
chore: added readme, license, codecov
1 parent 730e357 commit 8c4e984

File tree

5 files changed

+1127
-16
lines changed

5 files changed

+1127
-16
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
.nyc_output
2+
coverage/
13
node_modules/

LICENSE.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2020 Henrique Barcelos \<[email protected]\>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Round Linked Queue
2+
3+
[![Coverage Status](https://coveralls.io/repos/github/hbarcelos/round-linked-queue/badge.svg?branch=master)](https://coveralls.io/github/hbarcelos/round-linked-queue?branch=master)
4+
5+
Implements a fixed-size linked-list based queue. When trying to add an element to a full queue, it will evict the first added item in order to make room for the new element. This queue preserves the First In/First Out operation mode.
6+
7+
## How it works?
8+
9+
Given a queue of capacity 3, initially empty:
10+
11+
```
12+
(Nil)
13+
```
14+
15+
Calling `queue.add(1)` will put `1` at the beginning of the queue:
16+
17+
```
18+
(1) -> (Nil)
19+
```
20+
21+
Then `queue.add(2)` will turn it into:
22+
23+
```
24+
(1) -> (2) -> (Nil)
25+
```
26+
27+
Once more, with `queue.add(3)` we will have:
28+
29+
```
30+
(1) -> (2) -> (3) -> (Nil)
31+
```
32+
33+
Now the queue is at capacity. If we try to add another element, it will remove the element that was added first and shift all elements to the right. Consider `queue.add(4)`, the final result will be:
34+
35+
```
36+
(2) -> (3) -> (4) -> (Nil)
37+
38+
(1) // <--- removed element will be returned
39+
```
40+
41+
## Installation
42+
43+
```sh
44+
yarn add round-linked-queue
45+
# or with NPM
46+
# npm install --save round-linked-queue
47+
```
48+
49+
## API
50+
51+
### constructor
52+
53+
```
54+
new RoundLinkedQueue(maxLength: number) => RoudLinkedQueue
55+
```
56+
57+
Params:
58+
- `maxLength: number`: the capacity of the queue.
59+
60+
### #add()
61+
62+
```
63+
add(element: Any) => Any
64+
```
65+
66+
Params:
67+
- `element: Any`: the element to add to the queue.
68+
69+
Returns:
70+
- `undefined`: if the queue is not at capacity, because no element was removed.
71+
- `Any`: the removed element if the queue is at capacity.
72+
73+
### #remove()
74+
75+
```
76+
remove() => Any, throws Error
77+
```
78+
79+
Returns:
80+
- `Any`: the removed element if the queue is not empty.
81+
82+
Throws:
83+
- `Error`: if the queue is empty.
84+
85+
86+
### #toArray()
87+
88+
```
89+
toArray() => [Any]
90+
```
91+
92+
Returns:
93+
- `[Any]`: an Array with all elements in the queue.
94+
95+
### #*\[Symbol.iterator\]()
96+
97+
```
98+
*[Symbol.iterator]() => [Any]
99+
```
100+
101+
Returns:
102+
- `Iterator`: an iterator for the queue.
103+
104+
105+
### .maxLength
106+
107+
```
108+
get maxLength => Number
109+
```
110+
111+
Returns:
112+
- `Number`: the capacity of the queue.
113+
114+
### .first
115+
116+
```
117+
get first => Any, throws Error
118+
```
119+
120+
Returns:
121+
- `Any`: the first element in the queue if it is not empty.
122+
123+
Throws:
124+
- `Error`: if the queue is empty.
125+
126+
127+
### .last
128+
129+
```
130+
get last => Any, throws Error
131+
```
132+
133+
Returns:
134+
- `Any`: the last element in the queue if it is not empty.
135+
136+
Throws:
137+
- `Error`: if the queue is empty.
138+
139+
140+
### static fromArray
141+
142+
```
143+
RoundLinkedQueue.fromArray(inputArray: [Any]) => RoudLinkedQueue
144+
```
145+
146+
Params:
147+
- `inputArray: [Any]`: an arbitrary array of elements.
148+
149+
Returns:
150+
- `RoundLinkedQueue`: a new instance of the queue containing all elements in the array, respecting the insertion order.
151+
152+
## Contributing
153+
154+
### Running tests
155+
156+
After clonnig this repo, run:
157+
158+
```
159+
yarn test **.test.js
160+
# or with NPM
161+
# npm run test **.test.js
162+
```

package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
"description": "Fixed size round linked queue implementation in Javascript",
55
"main": "round-linked-queue.js",
66
"scripts": {
7-
"test": "mocha"
7+
"test": "nyc --reporter=html --reporter=text mocha",
8+
"coverage": "nyc report --reporter=text-lcov | coveralls"
89
},
910
"keywords": [
1011
"queue",
1112
"data structures"
1213
],
13-
"author": "Henrique Barcelos <rick.hjpbarcelos@gmail.com>",
14-
"license": "ISC",
14+
"author": "Henrique Barcelos <hbarcelos909@gmail.com>",
15+
"license": "MIT",
1516
"devDependencies": {
1617
"chai": "^4.2.0",
18+
"coveralls": "^3.0.9",
1719
"eslint": "^6.8.0",
1820
"eslint-config-prettier": "^6.10.0",
1921
"eslint-config-prettier-standard": "^3.0.1",
@@ -24,6 +26,8 @@
2426
"eslint-plugin-promise": "^4.2.1",
2527
"eslint-plugin-standard": "^4.0.1",
2628
"mocha": "^7.0.1",
29+
"mocha-lcov-reporter": "^1.3.0",
30+
"nyc": "^15.0.0",
2731
"prettier": "^1.19.1",
2832
"prettier-config-standard": "^1.0.1"
2933
}

0 commit comments

Comments
 (0)