Skip to content

Commit

Permalink
fix(enjoyBy): rework semantics for enjoyBy
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Oct 31, 2018
1 parent 3ed20c0 commit 5684f45
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
25 changes: 14 additions & 11 deletions index.js
Expand Up @@ -21,14 +21,15 @@ function pickManifest (packument, wanted, opts) {
}
const distTags = packument['dist-tags'] || {}
const versions = Object.keys(packument.versions || {}).filter(v => {
return semver.valid(v, true) && (
!time || (
packument.time[v] &&
time >= +(new Date(packument.time[v]))
)
)
return semver.valid(v, true)
})
const undeprecated = versions.filter(v => !packument.versions[v].deprecated)

function enjoyableBy (v) {
return !time || (
packument.time[v] && time >= +(new Date(packument.time[v]))
)
}

let err

if (!versions.length) {
Expand Down Expand Up @@ -56,20 +57,23 @@ function pickManifest (packument, wanted, opts) {
!target &&
tagVersion &&
packument.versions[tagVersion] &&
(!time || versions.indexOf(tagVersion) !== -1) &&
enjoyableBy(tagVersion) &&
semver.satisfies(tagVersion, wanted, true)
) {
target = tagVersion
}

if (!target && !opts.includeDeprecated) {
const undeprecated = versions.filter(v => !packument.versions[v].deprecated && enjoyableBy(v)
)
target = semver.maxSatisfying(undeprecated, wanted, true)
}
if (!target) {
target = semver.maxSatisfying(versions, wanted, true)
const stillFresh = versions.filter(enjoyableBy)
target = semver.maxSatisfying(stillFresh, wanted, true)
}

if (!target && wanted === '*') {
if (!target && wanted === '*' && enjoyableBy(tagVersion)) {
// This specific corner is meant for the case where
// someone is using `*` as a selector, but all versions
// are pre-releases, which don't match ranges at all.
Expand All @@ -78,7 +82,6 @@ function pickManifest (packument, wanted, opts) {

const manifest = (
target &&
(!time || versions.indexOf(target) !== -1) &&
packument.versions[target]
)
if (!manifest) {
Expand Down
32 changes: 23 additions & 9 deletions test/index.js
Expand Up @@ -277,27 +277,41 @@ test('accepts opts.enjoyBy option to do date-based cutoffs', t => {
created: '2018-01-01T00:00:00.000Z',
'1.0.0': '2018-01-01T00:00:00.000Z',
'2.0.0': '2018-01-02T00:00:00.000Z',
'3.0.0': '2018-01-03T00:00:00.000Z'
'2.0.1': '2018-01-03T00:00:00.000Z',
'3.0.0': '2018-01-04T00:00:00.000Z'
},
versions: {
'1.0.0': { version: '1.0.0' },
'2.0.0': { version: '2.0.0' },
'2.0.1': { version: '2.0.1' },
'3.0.0': { version: '3.0.0' }
}
}
const manifest = pickManifest(metadata, '*', {

let manifest = pickManifest(metadata, '*', {
enjoyBy: '2018-01-02'
})
t.equal(manifest.version, '2.0.0', 'filtered out 3.0.0 because of dates')

manifest = pickManifest(metadata, 'latest', {
enjoyBy: '2018-01-02'
})
t.equal(manifest.version, '3.0.0', 'tag specs use tagVersion if nothing else works')

manifest = pickManifest(metadata, '3.0.0', {
enjoyBy: '2018-01-02'
})
t.equal(manifest.version, '3.0.0', 'requesting specific version overrides')

manifest = pickManifest(metadata, '^2', {
enjoyBy: '2018-01-02'
})
t.equal(manifest.version, '2.0.0', 'non-tag ranges filtered')

t.throws(() => {
pickManifest(metadata, '3.0.0', {
enjoyBy: '2018-01-02'
})
}, /Enjoy By date of/, 'trying to find by out-of-range version breaks')
t.throws(() => {
pickManifest(metadata, 'latest', {
pickManifest(metadata, '^3', {
enjoyBy: '2018-01-02'
})
}, /Enjoy By date of/, 'trying to find by tag breaks if date is out of range')
}, /Enjoy By/, 'range for out-of-range spec fails even if defaultTag avail')
t.done()
})

0 comments on commit 5684f45

Please sign in to comment.