diff --git a/lib/cli.js b/lib/cli.js index ad42751..7143539 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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); diff --git a/lib/commands.js b/lib/commands.js index 518eb75..ddb9f5d 100644 --- a/lib/commands.js +++ b/lib/commands.js @@ -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; }); diff --git a/test/cli.test.js b/test/cli.test.js index 1f2e88c..a3dead6 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -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); diff --git a/test/commands.test.js b/test/commands.test.js index bd0b06b..81a03ea 100644 --- a/test/commands.test.js +++ b/test/commands.test.js @@ -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(); + }); + }); }); \ No newline at end of file