stack.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * @class Stack
  5. * @util
  6. */
  7. var Stack = function () {
  8. function Stack() {
  9. var initial = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  10. _classCallCheck(this, Stack);
  11. /**
  12. * Items collection.
  13. *
  14. * @type {Array}
  15. */
  16. this.items = initial;
  17. }
  18. /**
  19. * Add new item or items at the back of the stack.
  20. *
  21. * @param {*} items An item to add.
  22. */
  23. _createClass(Stack, [{
  24. key: "push",
  25. value: function push() {
  26. var _items;
  27. (_items = this.items).push.apply(_items, arguments);
  28. }
  29. /**
  30. * Remove the last element from the stack and returns it.
  31. *
  32. * @returns {*}
  33. */
  34. }, {
  35. key: "pop",
  36. value: function pop() {
  37. return this.items.pop();
  38. }
  39. /**
  40. * Return the last element from the stack (without modification stack).
  41. *
  42. * @returns {*}
  43. */
  44. }, {
  45. key: "peek",
  46. value: function peek() {
  47. return this.isEmpty() ? void 0 : this.items[this.items.length - 1];
  48. }
  49. /**
  50. * Check if the stack is empty.
  51. *
  52. * @returns {Boolean}
  53. */
  54. }, {
  55. key: "isEmpty",
  56. value: function isEmpty() {
  57. return !this.size();
  58. }
  59. /**
  60. * Return number of elements in the stack.
  61. *
  62. * @returns {Number}
  63. */
  64. }, {
  65. key: "size",
  66. value: function size() {
  67. return this.items.length;
  68. }
  69. }]);
  70. return Stack;
  71. }();
  72. export default Stack;