Created
July 12, 2017 07:31
-
-
Save tacamy/265d69d55365e0b8929f6cd3b20753f2 to your computer and use it in GitHub Desktop.
CodeGrid Count of Genko
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const api = { | |
uri: { | |
entry: 'https://app.codegrid.net/api/entry', | |
author: 'https://app.codegrid.net/api/author', | |
}, | |
options: { | |
mode: 'cors', | |
} | |
} | |
const countout = [ | |
'soto', | |
'y-maru', | |
'hokaccha', | |
'studio-kakky', | |
'matori', | |
] | |
function filterAuthors(authors) { | |
return authors.filter((author) => { | |
return !countout.includes(author.slug) | |
}) | |
} | |
function filterEntriesByYear(entries, year) { | |
return entries.filter(entry => { | |
return ( | |
new Date(entry.published).getFullYear() === year | |
) | |
}) | |
} | |
function countEntryByAuthor(entries, authors) { | |
return authors.map(author => { | |
let counter = 0 | |
entries.forEach(entry => { | |
if (!entry.authors.includes(author._id)) return | |
if (entry.authors.length === 1) { | |
counter += 1 | |
} | |
else if (entry.authors.length === 2) { | |
counter += 0.5 | |
} | |
}) | |
return { | |
author: author.slug, | |
count: counter, | |
} | |
}).sort((a, b) => a.count > b.count ? -1 : 1) | |
} | |
function createMsg(entryCount) { | |
return entryCount.map(data => { | |
return `${data.author}: ${data.count}` | |
}).join('\n') | |
} | |
function exec(year) { | |
Promise.resolve() | |
.then(() => fetch(api.uri.entry, api.options)) | |
.then(res => res.json()) | |
.then(entries => filterEntriesByYear(entries, year)) | |
.then(entries => { | |
return Promise.resolve() | |
.then(() => fetch(api.uri.author, api.options)) | |
.then(res => res.json()) | |
.then(authors => filterAuthors(authors)) | |
.then(authors => countEntryByAuthor(entries, authors)) | |
.then(entryCount => createMsg(entryCount)) | |
.then(msg => console.log(msg)) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment