generate a range of days between two dates

This commit is contained in:
Radek Stepan 2013-08-13 23:33:50 +01:00
parent 7cd90dbd2d
commit 58801fa38c
8 changed files with 57 additions and 20 deletions

View File

@ -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

View File

@ -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"
}
}
}

20
src/dates.coffee Normal file
View File

@ -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

View File

@ -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'

View File

@ -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.

4
src/regex.coffee Normal file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env coffee
module.exports =
'datetime': /^(\d{4}-\d{2}-\d{2})T(.*)/
'size_label': /size (\d+)$/

26
test/dates.coffee Normal file
View File

@ -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

View File

@ -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