ec386a8039d8b3a9baf950fc0a26308469b4444e63ae84eb571bf60badec4ca8c6dbfe7588fc72f84b215e5fefef111bf5e9e8e2a5b86900a3ed7190670a6d 947 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @class Queue
  3. * @util
  4. */
  5. class Queue {
  6. constructor(initial = []) {
  7. /**
  8. * Items collection.
  9. *
  10. * @type {Array}
  11. */
  12. this.items = initial;
  13. }
  14. /**
  15. * Add new item or items at the back of the queue.
  16. *
  17. * @param {*} items An item to add.
  18. */
  19. enqueue(...items) {
  20. this.items.push(...items);
  21. }
  22. /**
  23. * Remove the first element from the queue and returns it.
  24. *
  25. * @returns {*}
  26. */
  27. dequeue() {
  28. return this.items.shift();
  29. }
  30. /**
  31. * Return the first element from the queue (without modification queue stack).
  32. *
  33. * @returns {*}
  34. */
  35. peek() {
  36. return this.isEmpty() ? void 0 : this.items[0];
  37. }
  38. /**
  39. * Check if the queue is empty.
  40. *
  41. * @returns {Boolean}
  42. */
  43. isEmpty() {
  44. return !this.size();
  45. }
  46. /**
  47. * Return number of elements in the queue.
  48. *
  49. * @returns {Number}
  50. */
  51. size() {
  52. return this.items.length;
  53. }
  54. }
  55. export default Queue;