Permalink
Browse files

Begin using todos for commands

  • Loading branch information...
RStankov committed Feb 5, 2012
1 parent 3c85e5b commit 4607ce553c90d65a9d515e05cd484dc777c3f1a1
Showing with 26 additions and 73 deletions.
  1. +14 −69 lib/commands.js
  2. +1 −1 lib/formatter.js
  3. +4 −0 lib/todos.js
  4. +5 −1 test/commands.test.js
  5. +2 −2 test/formatter.test.js
View
@@ -12,6 +12,9 @@
*/
var commands = module.exports;
+commands.storage = require('./storage');
+commands.todos = require('./todos').init(commands.storage);
+
/**
* Print alias.
*
@@ -26,19 +29,12 @@ commands.print = console.log;
*/
var app = require('./app');
-/**
- * commands.storage. Just an alias to application config.
- *
- * @type {Object}
- */
-commands.storage = require('./storage');
-
/**
* Formatter.
*
* @type {Object}
*/
-var formatter = require('./formatter');
+var format = require('./formatter').format;
/**
* File writing.
@@ -60,18 +56,10 @@ commands.version = function() {
* @api public
*/
commands.list = function() {
- var out = [];
- commands.storage.get('items', function(err, items) {
- items || (items = []);
- for (var i = -1, len = items.length; ++i < len;) {
- if (!app.argv.all && items[i].done) continue;
- out.push(formatter.format(items[i], i + 1));
- }
-
- out.push('') && out.unshift('');
- out.map(function(line) {
- commands.print(line);
- });
+ this.todos[app.argv.all ? 'all' : 'undone'](function(items) {
+ commands.print('');
+ items.map(format).map(commands.print);
+ commands.print('');
});
};
@@ -82,8 +70,7 @@ commands.list = function() {
* @api public
*/
commands.check = function(num) {
- num = +num - 1;
- commands.toggle(num, true);
+ this.todos.check(+num - 1, true);
};
/**
@@ -93,28 +80,7 @@ commands.check = function(num) {
* @api public
*/
commands.undo = function(num) {
- num = +num - 1;
- commands.toggle(num, false);
-};
-
-/**
- * Toggles an item state.
- *
- * @param {Number} Item index.
- * @param {Boolean} State.
- * @api private
- */
-commands.toggle = function(num, state) {
- commands.storage.get('items', function(err, items) {
- items || (items = []);
- if (!items[num]) throw new Error('There is no todo item with number ' + num + 1);
- items[num].done = state;
- commands.storage.set('items', items, function() {
- commands.storage.save(function(err) {
- if (err) throw err;
- });
- });
- });
+ this.todos.check(+num - 1, false);
};
/**
@@ -124,16 +90,7 @@ commands.toggle = function(num, state) {
* @api public
*/
commands.destroy = function(num) {
- num = +num - 1;
- commands.storage.get('items', function(err, items) {
- items || (items = []);
- items.splice(num, 1);
- commands.storage.set('items', items, function() {
- commands.storage.save(function(err) {
- if (err) throw err;
- });
- });
- });
+ this.todos.destroy(+num - 1);
};
/**
@@ -143,11 +100,7 @@ commands.destroy = function(num) {
* @api public
*/
commands.clear = function(num) {
- commands.storage.set('items', [], function(err, items) {
- commands.storage.save(function(err) {
- if (err) throw err;
- });
- });
+ this.todos.clear();
};
/**
@@ -157,15 +110,7 @@ commands.clear = function(num) {
* @api public
*/
commands.add = function(item) {
- commands.storage.get('items', function(err, items) {
- items || (items = []);
- items.push({ text: item, done: false });
- commands.storage.set('items', items, function(err) {
- commands.storage.save(function(err) {
- if (err) throw err;
- });
- });
- });
+ this.todos.add(item);
};
/**
@@ -184,7 +129,7 @@ commands.write = function(filename) {
items || (items = []);
for (var i = -1, len = items.length; ++i < len;) {
if (!app.argv.all && items[i].done) continue;
- out.push(formatter.format(items[i], i + 1));
+ out.push(format(items[i], i));
}
out.push('') && out.unshift('');
View
@@ -29,7 +29,7 @@ formatter.format = function(item, num) {
var state = (item.done) ? ''.green : ''.red;
return ' '
- + '#' + num + ' '
+ + '#' + (num + 1) + ' '
+ state + ' '
+ item.text;
};
View
@@ -2,6 +2,10 @@ var Todos = module.exports = function(storage) {
this.storage = storage;
};
+Todos.init = function(storage) {
+ return new Todos(storage);
+};
+
Todos.prototype.all = function(callback) {
this.storage.get('items', function(err, items) {
callback(items || []);
View
@@ -7,13 +7,17 @@
var sinon = require('sinon'),
commands = require('../lib/commands'),
+ Todos = require('../lib/todos'),
Storage = require('./support/fake_storage');
describe('commands', function() {
var storage;
beforeEach(function() {
- storage = commands.storage = new Storage();
+ commands.storage = new Storage();
+ commands.todos = new Todos(commands.storage);
+
+ storage = commands.storage;
});
describe('version', function() {
View
@@ -15,11 +15,11 @@ var formatter = require('../lib/formatter');
describe('formatter', function() {
describe('.format()', function() {
it('should format done items properly', function() {
- formatter.format({ text: 'Foo', done: true }, 1).should.eql(' #1 \u001b[32m√\u001b[39m Foo');
+ formatter.format({ text: 'Foo', done: true }, 0).should.eql(' #1 \u001b[32m√\u001b[39m Foo');
});
it('should format unfinished items properly', function() {
- formatter.format({ text: 'Foo', done: false }, 1).should.eql(' #1 \u001b[31m✖\u001b[39m Foo');
+ formatter.format({ text: 'Foo', done: false }, 0).should.eql(' #1 \u001b[31m✖\u001b[39m Foo');
});
});
});

0 comments on commit 4607ce5

Please sign in to comment.