(initial = [])
| 1 | module.exports = Queue |
| 2 | |
| 3 | function Queue(initial = []) { |
| 4 | let xs = initial.slice() |
| 5 | let index = 0 |
| 6 | |
| 7 | return { |
| 8 | get length() { |
| 9 | return xs.length - index |
| 10 | }, |
| 11 | remove: (x) => { |
| 12 | const index = xs.indexOf(x) |
| 13 | return index === -1 |
| 14 | ? null |
| 15 | : (xs.splice(index, 1), x) |
| 16 | }, |
| 17 | push: (x) => (xs.push(x), x), |
| 18 | shift: () => { |
| 19 | const out = xs[index++] |
| 20 | |
| 21 | if (index === xs.length) { |
| 22 | index = 0 |
| 23 | xs = [] |
| 24 | } else { |
| 25 | xs[index - 1] = undefined |
| 26 | } |
| 27 | |
| 28 | return out |
| 29 | } |
| 30 | } |
| 31 | } |
no outgoing calls
no test coverage detected