Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Main function now takeseither a string (username) or an array of cont…
Browse files Browse the repository at this point in the history
…ributions.
  • Loading branch information
millette committed May 26, 2016
1 parent e9e13b8 commit 66f60c3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 31 deletions.
45 changes: 27 additions & 18 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,30 @@ const cli = meow([
' ... stats for bob'
])

rollodeqcGhUserStreak(cli.input[0] || 'millette')
.then((response) => {
if (!response.streaks.length) {
console.log('No commits in last 365 days.')
return
}
const latest = sort(response.streaks, 'begin').reverse()[0]
console.log(chalk.green(`Longest streak in a year: ${response.streaks[0].commits.length} days (${response.streaks[0].commits.reduce((p, c) => p + c)} commits), started ${response.streaks[0].begin}.`))
if (response.streaks[0].overlaps) {
console.log(chalk.red.bold('Note that the streak may be longer since it started at least 365 days ago.'))
}
if (latest.begin !== response.streaks[0].begin) {
console.log(`Latest streak: ${latest.commits.length} days (${latest.commits.reduce((p, c) => p + c)} commits), started ${latest.begin}.`)
}
})
.catch((e) => {
console.error('ERROR:', e)
})
const username = cli.input[0] || 'millette'

if (cli.flags.details) {
rollodeqcGhUserStreak.fetchContribs(username)
.then((response) => {
console.log(JSON.stringify(response, null, ' '))
})
} else {
rollodeqcGhUserStreak(username)
.then((response) => {
if (!response.streaks.length) {
console.log('No commits in last 365 days.')
return
}
const latest = sort(response.streaks, 'begin').reverse()[0]
console.log(chalk.green(`Longest streak in a year: ${response.streaks[0].commits.length} days (${response.streaks[0].commits.reduce((p, c) => p + c)} commits), started ${response.streaks[0].begin}.`))
if (response.streaks[0].overlaps) {
console.log(chalk.red.bold('Note that the streak may be longer since it started at least 365 days ago.'))
}
if (latest.begin !== response.streaks[0].begin) {
console.log(`Latest streak: ${latest.commits.length} days (${latest.commits.reduce((p, c) => p + c)} commits), started ${latest.begin}.`)
}
})
.catch((e) => {
console.error('ERROR:', e)
})
}
42 changes: 30 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ const counter = (days) => {

const dailyContribs = (str) => sort(flatten(str.match(weeksRe).map(counter)), 'date')

const fetchContribs = (username) => got(`https://github.com/users/${username}/contributions`)
.then((response) => response.body)
.then((body) => dailyContribs(body))
const fetchContribs = (username) => {
return got(`https://github.com/users/${username}/contributions`)
.then((response) => response.body)
.then((body) => dailyContribs(body))
}

const findStreaks = (contribs) => {
const s = []
Expand Down Expand Up @@ -76,14 +78,30 @@ const findStreaks = (contribs) => {
), (x) => x.commits.length).reverse()
}

module.exports = (username) => fetchContribs(username)
.then((contribs) => {
return {
streaks: findStreaks(contribs),
commitDays: contribs.filter((x) => x.count).length,
days: 365,
commits: contribs.reduce((p, c) => p + c.count, 0)
}
})
module.exports = (username) => {
switch (typeof username) {
case 'string':
return fetchContribs(username)
.then((contribs) => {
return {
streaks: findStreaks(contribs),
commitDays: contribs.filter((x) => x.count).length,
days: 365,
commits: contribs.reduce((p, c) => p + c.count, 0)
}
})

case 'object':
return Promise.resolve({
streaks: findStreaks(username),
commitDays: username.filter((x) => x.count).length,
days: 365,
commits: username.reduce((p, c) => p + c.count, 0)
})

default:
return Promise.reject(new Error('The username argument should a string or an array of commits for a year.'))
}
}

module.exports.fetchContribs = fetchContribs
25 changes: 24 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,31 @@ test('millette', async t => {
t.truthy(result.commits > 500)
})

test('millette with details', async t => {
const resulta = await fn.fetchContribs('millette')
const result = await fn(resulta)
t.falsy(result.streaks[0].overlaps)
t.truthy(result.streaks[0].commits.length > 50)
t.truthy(result.commitDays > 70)
t.truthy(result.commits > 500)
})

test.skip('millette with details', async t => {
const resulta = await fn.fetchContribs('jipiboily')
console.log(resulta.length)
console.log(resulta)
/*
const result = await fn(resulta)
t.falsy(result.streaks[0].overlaps)
t.truthy(result.streaks[0].commits.length > 50)
t.truthy(result.commitDays > 70)
t.truthy(result.commits > 500)
*/
})

test('overlapping', async t => {
const result = await fn('ldionne')
t.is(result.streaks[0].commits.length, 36)
t.is(result.streaks[0].commits.length, 30)
t.truthy(result.streaks[0].overlaps)
t.truthy(result.commitDays > 250)
t.truthy(result.commits > 1000)
Expand All @@ -27,3 +49,4 @@ test('no commits', async t => {
})

test('bad username', async t => await t.throws(fn('millette666'), 'Response code 404 (Not Found)'))
test('bad username type', async t => await t.throws(fn(666), 'The username argument should a string or an array of commits for a year.'))

0 comments on commit 66f60c3

Please sign in to comment.