all files / src/ Stack.js

100% Statements 13/13
100% Branches 2/2
100% Functions 7/7
100% Lines 13/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                    21010× 21010× 20993×         38539×                                  
'use strict';
 
var Class = require('class.extend'),
    _ = require('underscore'),
    Stack;
 
Stack = Class.extend({
 
   init: function(maxSize) {
      this._maxSize = maxSize;
      this._arr = [];
   },
 
   push: function(obj) {
      this._arr.push(obj);
      if (this._arr.length > this._maxSize) {
         this._arr.shift();
      }
   },
 
   contains: function(obj) {
      return _.contains(this._arr, obj);
   },
 
   toArray: function() {
      return _.rest(this._arr, 0);
   },
 
});
 
Stack.NO_OP = {
   push: function() {
      // do nothing
   },
   contains: function() {
      return false;
   },
   toArray: function() {
      throw new Error('toArray not supported on NO_OP Stack');
   },
};
 
module.exports = Stack;