Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 90eee4a7b4a702c20254a37c145ff1d20fc3e76f 0 parents
@tj tj authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
0  History.md
No changes.
7 Makefile
@@ -0,0 +1,7 @@
+
+test:
+ @./node_modules/.bin/mocha \
+ --require should \
+ --reporter spec
+
+.PHONY: test
44 Readme.md
@@ -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 component.json
@@ -0,0 +1,7 @@
+{
+ "name": "history",
+ "description": "Keep track of and cycle through history",
+ "keywords": ["history"],
+ "version": "0.0.1",
+ "scripts": ["index.js"]
+}
96 index.js
@@ -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 package.json
@@ -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 test/history.js
@@ -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.
Something went wrong with that request. Please try again.