-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 90eee4a
Showing
8 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
node_modules |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |||
} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |||
}) | |||
}) | |||
}) |