Last active
April 13, 2018 16:26
-
-
Save yarkovaleksei/f7078a8e7b2e7415a52f2f2ea0117630 to your computer and use it in GitHub Desktop.
This file contains 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
class Queue { | |
constructor (queue = []) { | |
this._q = queue | |
} | |
get queue () { | |
return this._q.reverse() | |
} | |
push (key, val) { | |
this._q.push({ key, val }) | |
return this | |
} | |
pop (key) { | |
let find | |
if (key) { | |
const index = this.queue.findIndex(e => e.key === key) | |
if (index > -1) { | |
find = this.queue[index] | |
this._q.splice(index, 1) | |
return find.val | |
} | |
return find | |
} | |
find = this.queue.pop() | |
return find ? find.val : find | |
} | |
} | |
const que = new Queue() | |
que.push('/api/v1/url1', 1) | |
.push('/api/v1/url2', 2) | |
console.log(que.pop('/api/v1/url1')) | |
console.log(que.pop()) | |
console.log(que.pop()) | |
console.log(que.pop()) | |
console.log(que.pop()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment