create an array instead

This commit is contained in:
Radek Stepan 2013-08-13 17:20:08 +01:00
parent 36f3c1cff5
commit e552e38b8a
2 changed files with 36 additions and 29 deletions

View File

@ -34,16 +34,20 @@ module.exports =
_.partial one_status, 'closed'
], cb
# Filter an array of incoming issues based on a regex.
# Filter an array of incoming issues based on a regex & save size on them.
'filter': (collection, regex, cb) ->
warnings = null
try
filtered = _.filter collection, ({ labels, number }) ->
filtered = _.filter collection, (issue) ->
{ labels, number } = issue
number ?= '?'
return false unless labels
switch ( {} for { name } in labels when name and regex.test(name) ).length
when 0 then false
when 1 then true
when 1
# Provide the size attribute on the issue.
issue.size = parseInt name.match(regex)[1]
true
else
warnings ?= []
warnings.push "Issue ##{number} has multiple matching size labels"
@ -54,20 +58,26 @@ module.exports =
catch err
return cb err, warnings
# Map a collection of closed issues into days (does not assume coll to be sorted).
'into_days': (collection, cb) ->
days = {}
# Map a collection of closed issues into days and determine the velocity for the range of all days.
# Assumes collection has been `filter`ed and is ordered.
'into_days': (collection, regex, cb) ->
days = [] ; current = [ -1, null ]
for issue in collection
{ 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(.*)/
return "Issue ##{number} does not match the `closed_at` pattern"
# Explode the matches.
[ date, time ] = matches[1...]
# Init the date?
days[date] ?= []
# Insert into an already sorted array.
days[date].splice _.sortedIndex(days[date], issue, 'closed_at'), 0, issue
# Move the index?
if current[1] isnt date
current[0] += 1
current[1] = date
# Init the array position.
days[current[0]] = { 'date': date, 'issues': [] }
# Save it assuming coll is in order.
days[current[0]].issues.push issue
cb null, days

View File

@ -144,16 +144,17 @@ module.exports =
done.call null
'filter on existing label regex': (done) ->
issues.filter [ { labels: [ { name: 'size 5' } ] } ]
, /size (\d)+$/, (err, warn, data) ->
issues.filter [ { labels: [ { name: 'size 15' } ] } ]
, /size (\d+)$/, (err, warn, data) ->
assert.ifError err
assert.ifError warn
assert.equal data.length, 1
assert.equal data[0].size, 15
done.call null
'filter when no labels': (done) ->
issues.filter [ { } ]
, /size (\d)+$/, (err, warn, data) ->
, /size (\d+)$/, (err, warn, data) ->
assert.ifError err
assert.ifError warn
assert.equal data.length, 0
@ -161,7 +162,7 @@ module.exports =
'filter when empty labels': (done) ->
issues.filter [ { labels: [] } ]
, /size (\d)+$/, (err, warn, data) ->
, /size (\d+)$/, (err, warn, data) ->
assert.ifError err
assert.ifError warn
assert.equal data.length, 0
@ -169,7 +170,7 @@ module.exports =
'filter when not matching regex': (done) ->
issues.filter [ { labels: [ { name: 'size 1A' } ] } ]
, /size (\d)+$/, (err, warn, data) ->
, /size (\d+)$/, (err, warn, data) ->
assert.ifError err
assert.ifError warn
assert.equal data.length, 0
@ -177,25 +178,21 @@ module.exports =
'filter when multiple match the regex': (done) ->
issues.filter [ { labels: [ { name: 'size 1' }, { name: 'size 6' } ] } ]
, /size (\d)+$/, (err, warn, data) ->
, /size (\d+)$/, (err, warn, data) ->
assert.ifError err
assert.equal warn.length, 1
assert.equal data.length, 1
done.call null
'organize issues into days': (done) ->
issues.into_days [
{ number: 1, closed_at: '2013-05-09T10:04:53Z' }
{ number: 2, closed_at: '2013-05-09T09:04:53Z' }
{ number: 3, closed_at: '2013-05-10T09:04:53Z' }
], (err, data) ->
a = { number: 2, closed_at: '2013-05-09T09:04:53Z', size: 6 }
b = { number: 1, closed_at: '2013-05-09T10:04:53Z', size: 4 }
c = { number: 3, closed_at: '2013-05-10T09:04:53Z', size: 2 }
issues.into_days [ a, b, c ], /size (\d+)$/, (err, data) ->
assert.ifError err
assert.deepEqual data,
'2013-05-09': [
{ number: 2, closed_at: '2013-05-09T09:04:53Z' }
{ number: 1, closed_at: '2013-05-09T10:04:53Z' }
]
'2013-05-10': [
{ number: 3, closed_at: '2013-05-10T09:04:53Z' }
]
assert.deepEqual data, [
{ date: '2013-05-09', issues: [ a, b ] }
{ date: '2013-05-10', issues: [ c ] }
]
done.call null