Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 17, 2012
0 parents commit 90eee4a
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Empty file added History.md
Empty file.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,7 @@

test:
@./node_modules/.bin/mocha \
--require should \
--reporter spec

.PHONY: test
44 changes: 44 additions & 0 deletions Readme.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,44 @@

# History

Keep track of and cycle through history, for example chat messages.

## Installation

```
$ component install component/history
```

## API

### History(vals)

Initialize with an array of `vals`.

### History#add(val)

Add a value, for example a chat message or REPL command line.
This method resets the history index, meaning the next call
to `.back()` will be this latest value.

### 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)

The maximum number of entries defaulting to `1000`.

### History#reset()

Reset the index to the latest value.

## License

MIT
7 changes: 7 additions & 0 deletions component.json
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "history",
"description": "Keep track of and cycle through history",
"keywords": ["history"],
"version": "0.0.1",
"scripts": ["index.js"]
}
96 changes: 96 additions & 0 deletions index.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,96 @@

/**
* Expose `History`.
*/

module.exports = History;

/**
* Initialize a `History` with the given `vals`.
*
* @param {Array} vals
* @api public
*/

function History(vals) {
this.vals = vals || [];
this.reset();
this.max(1000);
}

/**
* Cap the entries.
*
* @api private
*/

History.prototype.cap = function(){
var max = this._max;
var len = this.vals.length;
var remove = len - max;
if (remove <= 0) return;
while (remove--) this.vals.shift();
this.reset();
};

/**
* Set the maximum number of entries to `n`.
*
* @param {Number} n
* @return {History}
* @api public
*/

History.prototype.max = function(n){
this._max = n;
this.cap();
return this;
};

/**
* Add a `val`.
*
* @param {Object} val
* @return {History}
* @api public
*/

History.prototype.add = function(val){
this.i = this.vals.push(val) - 1;
this.cap();
return this;
};

/**
* Cycle backwards through history.
*
* @return {Object}
* @api public
*/

History.prototype.back = function(){
return this.vals[this.i--];
};

/**
* Cycle forward through history.
*
* @return {Object}
* @api public
*/

History.prototype.forward = function(){
return this.vals[++this.i];
};

/**
* Reset the history index.
*
* @return {History}
* @api public
*/

History.prototype.reset = function(){
this.i = this.vals.length - 1;
return this;
};
14 changes: 14 additions & 0 deletions package.json
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "history-component",
"description": "Keep track of and cycle through history",
"version": "0.0.1",
"devDependencies": {
"mocha": "*",
"should": "*"
},
"component": {
"scripts": {
"history": "index.js"
}
}
}
98 changes: 98 additions & 0 deletions test/history.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,98 @@

var History = require('..')
, assert = require('assert');

describe('History', function(){
describe('(arr)', function(){
it('should populate the history', function(){
var history = new History(['foo', 'bar', 'baz']);
history.back().should.equal('baz');
history.back().should.equal('bar');
history.back().should.equal('foo');
})
})

describe('.add(obj)', function(){
it('should add to the history', function(){
var history = new History;
history.add('foo');
history.add('bar');
history.add('baz');
history.back().should.equal('baz');
history.back().should.equal('bar');
history.back().should.equal('foo');
})

it('should reset the index', function(){
var history = new History;
history.add('foo');
history.add('bar');
history.add('baz');
history.back().should.equal('baz');
history.back().should.equal('bar');
history.add('hey');
history.back().should.equal('hey');
history.back().should.equal('baz');
history.back().should.equal('bar');
})
})

describe('.back()', function(){
it('should cycle through the history', function(){
var history = new History;
history.add('foo');
history.add('bar');
history.add('baz');
history.back().should.equal('baz');
history.back().should.equal('bar');
history.back().should.equal('foo');
})

it('should return undefined at the end', function(){
var history = new History;
history.add('foo');
history.back().should.equal('foo');
assert(undefined === history.back());
})
})

describe('.forward()', function(){
it('should cycle through the history', function(){
var history = new History;
history.add('foo');
history.add('bar');
history.add('baz');
history.back().should.equal('baz');
history.back().should.equal('bar');
history.back().should.equal('foo');
history.forward().should.equal('foo');
history.forward().should.equal('bar');
history.forward().should.equal('baz');
})

it('should return undefined at the end', function(){
var history = new History;
history.add('foo');
assert(undefined === history.forward());
})
})

describe('.max(n)', function(){
it('should cap the history entries', function(){
var history = new History;
history.max(2);
history.add('foo');
history.add('bar');
history.add('baz');

history.back().should.equal('baz');
history.back().should.equal('bar');
assert(null == history.back());

history.add('raz');
history.back().should.equal('raz');
history.back().should.equal('baz');
assert(null == history.back());
})
})
})

0 comments on commit 90eee4a

Please sign in to comment.