diff --git a/README.md b/README.md index f120a55..8519e76 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ##Next -Create a range of days between two dates. +Map closed tasks to a range of dates while maintaining the points remaining stats. ##Project Charter diff --git a/package.json b/package.json index 7b359e4..1379889 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "lodash": "~1.3.1" }, "devDependencies": { - "mocha": "~1.12.0" + "mocha": "~1.12.0", + "moment": "~2.1.0" }, "scripts": { "test": "make test" @@ -33,4 +34,4 @@ "bugs": { "url": "https://github.com/radekstepan/github-burndown-chart/issues" } -} +} \ No newline at end of file diff --git a/src/dates.coffee b/src/dates.coffee new file mode 100644 index 0000000..6311e86 --- /dev/null +++ b/src/dates.coffee @@ -0,0 +1,20 @@ +#!/usr/bin/env coffee +{ _ } = require 'lodash' +reg = require './regex' + +module.exports = + # Create a range of days between two dates. + 'range': ({ a, b }, cb) -> + # Swap? + [ b, a ] = [ a, b ] if b < a + + # When do we start & end? + [ year, month, day ] = _.map(a.match(reg.datetime)[1].split('-'), (d) -> parseInt(d) ) + b = b.match(reg.datetime)[1] + + days = [] + do add = (i = 0) -> + days.push c = new Date(year, month - 1, day + i).toJSON().match(reg.datetime)[1] + add(i + 1) unless c is b + + cb null, days \ No newline at end of file diff --git a/src/days.coffee b/src/days.coffee deleted file mode 100644 index 904ef57..0000000 --- a/src/days.coffee +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env coffee -module.exports = - # Create a range of days between two dates. - 'range': ({ a, b }, cb) -> - cb 'Not implemented yet' \ No newline at end of file diff --git a/src/issues.coffee b/src/issues.coffee index 2b4933d..017c116 100644 --- a/src/issues.coffee +++ b/src/issues.coffee @@ -1,7 +1,9 @@ #!/usr/bin/env coffee { _ } = require 'lodash' async = require 'async' + req = require './request' +reg = require './regex' module.exports = # Used on an initial fetch of issues for a repo. @@ -66,7 +68,7 @@ module.exports = { state, number, closed_at } = issue number ?= '?' return "Issue ##{number} does not have a `closed_at` parameter" unless closed_at - unless matches = closed_at.match /^(\d{4}-\d{2}-\d{2})T(.*)/ + unless matches = closed_at.match reg.datetime return "Issue ##{number} does not match the `closed_at` pattern" # Explode the matches. diff --git a/src/regex.coffee b/src/regex.coffee new file mode 100644 index 0000000..2099c68 --- /dev/null +++ b/src/regex.coffee @@ -0,0 +1,4 @@ +#!/usr/bin/env coffee +module.exports = + 'datetime': /^(\d{4}-\d{2}-\d{2})T(.*)/ + 'size_label': /size (\d+)$/ \ No newline at end of file diff --git a/test/dates.coffee b/test/dates.coffee new file mode 100644 index 0000000..d65ef46 --- /dev/null +++ b/test/dates.coffee @@ -0,0 +1,26 @@ +#!/usr/bin/env coffee +assert = require 'assert' +path = require 'path' +{ _ } = require 'lodash' +moment = require 'moment' + +dates = require path.resolve __dirname, '../src/dates.coffee' + +tests = + 'range between two dates': + [ '2013-01-01T00:00:00Z', '2013-01-02T00:00:00Z' ] + 'range regardless of the order': + [ '2013-01-02T00:00:00Z', '2013-01-01T00:00:00Z' ] + 'range across a year': + [ '2012-12-12T00:00:00Z', '2013-01-05T00:00:00Z' ] + 'range on the same day': + [ '2012-12-12T00:00:00Z', '2013-12-12T00:00:00Z' ] + +for key, value of tests then do (key, value) -> + exports[key] = (done) -> + [ a, b ] = value + dates.range { a, b }, (err, data) -> + assert.ifError err + assert.equal data.length, Math.abs(moment(a).diff(moment(b), 'days')) + 1 + _.each data, (date) -> assert moment(date).isValid() + done.call null \ No newline at end of file diff --git a/test/days.coffee b/test/days.coffee deleted file mode 100644 index af5682b..0000000 --- a/test/days.coffee +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env coffee -assert = require 'assert' -path = require 'path' - -days = require path.resolve __dirname, '../src/days.coffee' - -module.exports = - 'days array between two endpoints': (done) -> - days.range { a: '2013-01-05', a: '2012-11-01' }, (err, data) -> - assert.ifError err - done.call null \ No newline at end of file