Created
November 20, 2020 04:09
-
-
Save nivrith/d3a87a23fd7b7a6f9e2df32fa0084586 to your computer and use it in GitHub Desktop.
Make your own iterables in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class Queue<T> { | |
| constructor(private length: number, private data: T[] = []) {} | |
| add(record) { | |
| const length = this.data.unshift(record); | |
| if (length > this.length) { | |
| this.remove(); | |
| } | |
| } | |
| remove() { | |
| this.data.pop(); | |
| } | |
| size() { | |
| return this.data.length; | |
| } | |
| isMember(item: T) { | |
| if (this.data.indexOf(item) !== -1) { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment