diff --git a/libs/live-fns/nuget.js b/libs/live-fns/nuget.js index fd08783..07a32b9 100644 --- a/libs/live-fns/nuget.js +++ b/libs/live-fns/nuget.js @@ -1,14 +1,13 @@ const axios = require('../axios.js') const semColor = require('../utils/sem-color.js') +const pre = versions => versions.filter(v => v.includes('-')) +const stable = versions => versions.filter(v => !v.includes('-')) +const latest = versions => versions.length > 0 && versions.slice(-1)[0] + module.exports = async function (method, project, channel) { const endpoint = `https://api.nuget.org/v3-flatcontainer/${project}/index.json` const { versions } = await axios.get(endpoint).then(res => res.data) - const unknownBadgeData = { - subject: 'nuget', - status: 'unknown', - color: 'grey' - } switch (method) { case 'v': @@ -16,38 +15,28 @@ module.exports = async function (method, project, channel) { switch (channel) { case 'latest': - if (versions.length > 0) { - version = versions.slice(-1)[0] - } + version = latest(versions) break case 'pre': - const preReleaseVersions = versions.filter(v => v.indexOf('-') !== -1) - if (preReleaseVersions.length > 0) { - version = preReleaseVersions.slice(-1)[0] - } + version = latest(pre(versions)) break - default: // by default get stable version - const stableVersions = versions.filter(v => v.indexOf('-') === -1) - if (stableVersions.length > 0) { - version = stableVersions.slice(-1)[0] - } + default: // get stable version + version = latest(stable(versions)) } - // if in case version is still empty, try to get the latest - if (!version && versions.length > 0) { - version = versions.slice(-1)[0] - } + // in case version is still empty, try to get the latest + version = version || latest(versions) - if (version) { - return { - subject: 'nuget', - status: 'v' + version, - color: semColor(version) - } - } else { - return unknownBadgeData + return { + subject: 'nuget', + status: version ? `v${version}` : 'unknown', + color: semColor(version) } default: - return unknownBadgeData + return { + subject: 'nuget', + status: 'unknown', + color: 'grey' + } } } diff --git a/libs/utils/sem-color.js b/libs/utils/sem-color.js index e682a46..b85f222 100644 --- a/libs/utils/sem-color.js +++ b/libs/utils/sem-color.js @@ -11,7 +11,7 @@ */ module.exports = function vc (version) { - if (version.match(/\b(alpha|beta|canary)\b/)) { + if (version.match(/\b(alpha|beta|canary)/)) { return 'cyan' }