Skip to content

Commit

Permalink
refactor: sortObjectByIdent
Browse files Browse the repository at this point in the history
- renamed getIdent to getPackageName
- moved getPackageName and parseNameAndVersionRange functions to outer scope.
  • Loading branch information
rickh18 committed Aug 21, 2024
1 parent e3a0a8c commit f473ce9
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,24 @@ const overProperty =
: object
const sortGitHooks = sortObjectBy(gitHooks)

const sortObjectBySemver = sortObjectBy((a, b) => {
const parseNameAndVersionRange = (specifier) => {
// 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 }
}
const splitIndex = atMatches.pop().index
return {
name: nameAndVersion.substring(0, splitIndex),
range: nameAndVersion.substring(splitIndex + 1),
}
const parseNameAndVersionRange = (specifier) => {
// 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 }
}
const splitIndex = atMatches.pop().index
return {
name: nameAndVersion.substring(0, splitIndex),
range: nameAndVersion.substring(splitIndex + 1),
}
}

const sortObjectBySemver = sortObjectBy((a, b) => {
const { name: aName, range: aRange } = parseNameAndVersionRange(a)
const { name: bName, range: bRange } = parseNameAndVersionRange(b)

Expand All @@ -87,24 +88,24 @@ const sortObjectBySemver = sortObjectBy((a, b) => {
return semver.compare(semver.minVersion(aRange), semver.minVersion(bRange))
})

const sortObjectByIdent = (a, b) => {
const getIdent = (ident) => {
const parts = ident.split('@')

if (ident.startsWith('@')) {
// Handle cases where ident starts with '@'
return parts.length > 2 ? parts.slice(0, -1).join('@') : ident
}
const getPackageName = (ident) => {
const parts = ident.split('@')

// Handle cases where ident doesn't start with '@'
return parts.length > 1 ? parts.slice(0, -1).join('@') : ident
if (ident.startsWith('@')) {
// Handle cases where package name starts with '@'
return parts.length > 2 ? parts.slice(0, -1).join('@') : ident
}

const identA = getIdent(a)
const identB = getIdent(b)
// Handle cases where package name doesn't start with '@'
return parts.length > 1 ? parts.slice(0, -1).join('@') : ident
}

const sortObjectByIdent = (a, b) => {
const PackageNameA = getPackageName(a)
const PackageNameB = getPackageName(b)

if (identA < identB) return -1
if (identA > identB) return 1
if (PackageNameA < PackageNameB) return -1
if (PackageNameA > PackageNameB) return 1
return 0
}

Expand Down

0 comments on commit f473ce9

Please sign in to comment.