Skip to content

Commit

Permalink
Clear cmd.
Browse files Browse the repository at this point in the history
  • Loading branch information
vesln committed Jan 29, 2012
1 parent 78cab5a commit 9e23dfa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/cli.js
Expand Up @@ -25,6 +25,9 @@ app.cmd(/version/, commands.version);
// Lists all todo items.
app.cmd(/ls/, commands.list);

// Clears the whole todo.
app.cmd(/clear/, commands.clear);

// Marks a todo item as done.
app.cmd(/check (.+)/, commands.check);

Expand Down
18 changes: 17 additions & 1 deletion lib/commands.js
Expand Up @@ -122,7 +122,23 @@ commands.delete = function(num) {
num = +num - 1;
storage.get('items', function(err, items) {
items || (items = []);
items = items.splice(num, 1);
items.splice(num, 1);
storage.set('items', items, function() {
storage.save(function(err) {
if (err) throw err;
});
});
});
};

/**
* Clears the whole todo item.
*
* @param {String} Todo item number.
* @api public
*/
commands.clear = function(num) {
storage.set('items', [], function(err, items) {
storage.save(function(err) {
if (err) throw err;
});
Expand Down
1 change: 1 addition & 0 deletions test/cli.test.js
Expand Up @@ -32,6 +32,7 @@ describe('cli', function() {
it('should register routes', function() {
cli.router.routes.version.on.should.eql(commands.version);
cli.router.routes.ls.on.should.eql(commands.list);
cli.router.routes.clear.on.should.eql(commands.clear);
cli.router.routes.rm['(.+)'].on.should.eql(commands.delete);
cli.router.routes.check['(.+)'].on.should.eql(commands.check);
cli.router.routes.undo['(.+)'].on.should.eql(commands.undo);
Expand Down
16 changes: 16 additions & 0 deletions test/commands.test.js
Expand Up @@ -134,4 +134,20 @@ describe('commands', function() {
commands.add('Foo bar');
});
});

describe('.clear()', function(done) {
it('should clear the todo', function(done) {
sinon.stub(storage, 'set', function(key, value, cb) {
key.should.eql('items');
value.should.eql([]);
cb();
});
sinon.stub(storage, 'save', function(key, cb) {
storage.set.restore();
storage.save.restore();
done();
});
commands.clear();
});
});
});

0 comments on commit 9e23dfa

Please sign in to comment.