641db7df6b5089fdfe15f79ba189862adc5000f0923b1fb8be76073f318c0026d87c21ae0332f5814d48583d104ae593a17049420e752a54b52bc4f0c84a00 950 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @class Stack
  3. * @util
  4. */
  5. class Stack {
  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 stack.
  16. *
  17. * @param {*} items An item to add.
  18. */
  19. push(...items) {
  20. this.items.push(...items);
  21. }
  22. /**
  23. * Remove the last element from the stack and returns it.
  24. *
  25. * @returns {*}
  26. */
  27. pop() {
  28. return this.items.pop();
  29. }
  30. /**
  31. * Return the last element from the stack (without modification stack).
  32. *
  33. * @returns {*}
  34. */
  35. peek() {
  36. return this.isEmpty() ? void 0 : this.items[this.items.length - 1];
  37. }
  38. /**
  39. * Check if the stack is empty.
  40. *
  41. * @returns {Boolean}
  42. */
  43. isEmpty() {
  44. return !this.size();
  45. }
  46. /**
  47. * Return number of elements in the stack.
  48. *
  49. * @returns {Number}
  50. */
  51. size() {
  52. return this.items.length;
  53. }
  54. }
  55. export default Stack;