Skip to content

Commit

Permalink
add npm versions
Browse files Browse the repository at this point in the history
closes ehmicky#8
  • Loading branch information
Maxim-Mazurok committed Jun 24, 2022
1 parent fb9dea9 commit 52fc927
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ export const normalizeIndex = function (index) {
const indexItems = index.map(normalizeVersion)
const versions = getAllVersions(indexItems)
const majors = getMajors(indexItems)
return { versions, majors }
const nodeNpmVersions = getAllNodeNpmVersions(indexItems)
return { versions, majors, nodeNpmVersions }
}

const normalizeVersion = function ({ version, lts }) {
const normalizeVersion = function ({ version, lts, npm }) {
// Remove the leading `v`
const versionA = version.slice(1)
const major = semver.major(versionA)
return { version: versionA, major, lts }
return { version: versionA, major, lts, npm }
}

// Array with all version strings, sorted from most to least recent
Expand All @@ -26,6 +27,14 @@ const getVersionField = function ({ version }) {
return version
}

// Array with all {node: ..., npm: ...} version pairs
const getAllNodeNpmVersions = function (indexItems) {
return indexItems.map(({ version, npm }) => ({
node: version,
npm: npm || '0.0.0',
}))
}

// Array with all major releases latest version, sorted from most to least
// recent. Includes `lts` name if any.
const getMajors = function (indexItems) {
Expand Down
37 changes: 37 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,28 @@ const isVersion = function (version) {
return typeof version === 'string' && VERSION_REGEXP.test(version)
}

const isNodeNpmVersion = function (nodeNpmVersion) {
return (
typeof nodeNpmVersion.node === 'string' &&
VERSION_REGEXP.test(nodeNpmVersion.node) &&
typeof nodeNpmVersion.npm === 'string' &&
NPM_VERSION_REGEXP.test(nodeNpmVersion.npm)
)
}

const VERSION_REGEXP = /^\d+\.\d+\.\d+$/u

/**
* Real examples:
* 1.1.0-beta-4
* 1.1.0-alpha-6
* 1.1.18
* 6.5.0-next.0
* 1.1.0-3
*/
const NPM_VERSION_REGEXP =
/^\d+\.\d+\.\d+((-(alpha|beta)-\d+)|(-next\.\d+)|(-\d+))?$/u

test('"versions" are present', async (t) => {
const { versions } = await allNodeVersions({ fetch: true })

Expand All @@ -24,6 +44,23 @@ test('"versions" are sorted', async (t) => {
t.deepEqual(versions, sortedVersions)
})

test('"nodeNpmVersions" are present', async (t) => {
const { nodeNpmVersions } = await allNodeVersions({ fetch: true })

t.true(Array.isArray(nodeNpmVersions))
t.true(nodeNpmVersions.every(isNodeNpmVersion))
})

test('"nodeNpmVersions" are sorted', async (t) => {
const { nodeNpmVersions } = await allNodeVersions({ fetch: true })
// eslint-disable-next-line fp/no-mutating-methods, id-length
const sortedVersions = [...nodeNpmVersions].sort((a, b) =>
semver.rcompare(a.node, b.node),
)

t.deepEqual(nodeNpmVersions, sortedVersions)
})

test('"majors" are present', async (t) => {
const { majors } = await allNodeVersions({ fetch: true })

Expand Down

0 comments on commit 52fc927

Please sign in to comment.