Skip to content

Commit

Permalink
refactor: simplify getPackageName and fix tests (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Mar 1, 2025
1 parent 18905e8 commit 43ee7e7
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 263 deletions.
20 changes: 7 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,17 @@ const sortObjectBySemver = sortObjectBy((a, b) => {
})

const getPackageName = (ident) => {
const parts = ident.split('@')

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

// Handle cases where package name doesn't start with '@'
return parts.length > 1 ? parts.slice(0, -1).join('@') : ident
const index = ident.indexOf('@', ident.startsWith('@') ? 1 : 0)
// This should not happen, unless user manually edit the package.json file
return index === -1 ? ident : ident.slice(0, index)
}

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

if (PackageNameA < PackageNameB) return -1
if (PackageNameA > PackageNameB) return 1
if (packageNameA < packageNameB) return -1
if (packageNameA > packageNameB) return 1
return 0
}

Expand Down
20 changes: 10 additions & 10 deletions tests/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ function sortObjectWithRangeAlphabetically(t, options = {}) {
...options,
value: keysToObject(
[
'@[email protected]',
'@z-package/package@1.2.3',
'[email protected]',
'[email protected]',
'@[email protected]',
'@a-package/package@1.2.3',
'[email protected]',
'@b-package',
'@[email protected]',
'@[email protected]',
'@b-package/package',
'@e-package/package@1.2.3',
'@ch-package/package@1.2.3',
'[email protected]',
'[email protected]',
],
Expand All @@ -84,11 +84,11 @@ function sortObjectWithRangeAlphabetically(t, options = {}) {
expect ||
keysToObject(
[
'@[email protected]',
'@b-package',
'@[email protected]',
'@[email protected]',
'@[email protected]',
'@a-package/package@1.2.3',
'@b-package/package',
'@ch-package/package@1.2.3',
'@e-package/package@1.2.3',
'@z-package/package@1.2.3',
'[email protected]',
'[email protected]',
'[email protected]',
Expand Down
Binary file modified tests/snapshots/cli.js.snap
Binary file not shown.
480 changes: 240 additions & 240 deletions tests/snapshots/deps.js.md

Large diffs are not rendered by default.

Binary file modified tests/snapshots/deps.js.snap
Binary file not shown.
Binary file modified tests/snapshots/eslint.js.snap
Binary file not shown.
Binary file modified tests/snapshots/fields.js.snap
Binary file not shown.
Binary file modified tests/snapshots/main.js.snap
Binary file not shown.
Binary file modified tests/snapshots/prettier.js.snap
Binary file not shown.

0 comments on commit 43ee7e7

Please sign in to comment.