Skip to content
Browse files

add min / max cap

  • Loading branch information...
1 parent 90eee4a commit 46096c0a5b4230284c4742286e5d1bc3b6752488 @tj tj committed Aug 17, 2012
Showing with 17 additions and 7 deletions.
  1. +1 −3 Readme.md
  2. +3 −0 index.js
  3. +13 −4 test/history.js
View
4 Readme.md
@@ -1,7 +1,7 @@
# History
- Keep track of and cycle through history, for example chat messages.
+ Keep track of and cycle through capped history (chat messages, REPL command-lines etc).
## Installation
@@ -24,12 +24,10 @@ $ component install component/history
### History#back()
Cycle backwards through history, returning a value added by `.add()`.
- When the end of history is reached `undefined` is returned.
### History#back()
Cycle forwards through history, returning a value added by `.add()`.
- When the end of history is reached `undefined` is returned.
### History#max(n)
View
3 index.js
@@ -69,6 +69,7 @@ History.prototype.add = function(val){
*/
History.prototype.back = function(){
+ if (this.i < 0) return;
return this.vals[this.i--];
};
@@ -80,6 +81,8 @@ History.prototype.back = function(){
*/
History.prototype.forward = function(){
+ var len = this.vals.length;
+ if (this.i == len - 1) return;
return this.vals[++this.i];
};
View
17 test/history.js
@@ -48,11 +48,15 @@ describe('History', function(){
history.back().should.equal('foo');
})
- it('should return undefined at the end', function(){
+ it('should cap the index', function(){
var history = new History;
history.add('foo');
history.back().should.equal('foo');
- assert(undefined === history.back());
+ history.back();
+ history.back();
+ history.back();
+ history.back();
+ history.forward().should.equal('foo');
})
})
@@ -70,10 +74,15 @@ describe('History', function(){
history.forward().should.equal('baz');
})
- it('should return undefined at the end', function(){
+ it('should cap the index', function(){
var history = new History;
history.add('foo');
- assert(undefined === history.forward());
+ history.forward();
+ history.forward();
+ history.forward();
+ history.forward();
+ history.forward();
+ history.back().should.equal('foo');
})
})

0 comments on commit 46096c0

Please sign in to comment.
Something went wrong with that request. Please try again.