Skip to content

Commit

Permalink
chore: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
westrik committed Mar 29, 2024
1 parent 3cd735b commit ba94ce5
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,23 @@ const sortGitHooks = sortObjectBy(gitHooks)

const sortObjectBySemver = sortObjectBy((a, b) => {
const parseNameAndVersionRange = (specifier) => {
const [nameAndVersion, childName] = specifier.split('>')
// Ignore anything after > & rely on fallback alphanumeric sorting for that
const [nameAndVersion] = specifier.split('>')
const atMatches = [...nameAndVersion.matchAll('@')]
if (
!atMatches.length ||
(atMatches.length === 1 && atMatches[0].index === 0)
) {
return { name: specifier, childName }
return { name: specifier }
}
const splitIndex = atMatches.pop().index
return {
name: nameAndVersion.substring(0, splitIndex),
range: nameAndVersion.substring(splitIndex + 1),
childName,
}
}
const {
name: aName,
range: aRange,
childDep: aChildDep,
} = parseNameAndVersionRange(a)
const {
name: bName,
range: bRange,
childDep: bChildDep,
} = parseNameAndVersionRange(b)
const { name: aName, range: aRange } = parseNameAndVersionRange(a)
const { name: bName, range: bRange } = parseNameAndVersionRange(b)

if (aName !== bName) {
return aName.localeCompare(bName)
Expand All @@ -92,13 +84,6 @@ const sortObjectBySemver = sortObjectBy((a, b) => {
if (!bRange) {
return 1
}
if (aRange === bRange && (aChildDep || bChildDep)) {
if (aChildDep) {
return aChildDep.localeCompare(bChildDep)
} else {
return -1
}
}
return semver.compare(semver.minVersion(aRange), semver.minVersion(bRange))
})

Expand Down

0 comments on commit ba94ce5

Please sign in to comment.