Skip to content

Commit 43ee7e7

Browse files
authored
refactor: simplify getPackageName and fix tests (#347)
1 parent 18905e8 commit 43ee7e7

File tree

9 files changed

+257
-263
lines changed

9 files changed

+257
-263
lines changed

index.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,17 @@ const sortObjectBySemver = sortObjectBy((a, b) => {
9090
})
9191

9292
const getPackageName = (ident) => {
93-
const parts = ident.split('@')
94-
95-
if (ident.startsWith('@')) {
96-
// Handle cases where package name starts with '@'
97-
return parts.length > 2 ? parts.slice(0, -1).join('@') : ident
98-
}
99-
100-
// Handle cases where package name doesn't start with '@'
101-
return parts.length > 1 ? parts.slice(0, -1).join('@') : ident
93+
const index = ident.indexOf('@', ident.startsWith('@') ? 1 : 0)
94+
// This should not happen, unless user manually edit the package.json file
95+
return index === -1 ? ident : ident.slice(0, index)
10296
}
10397

10498
const sortObjectByIdent = (a, b) => {
105-
const PackageNameA = getPackageName(a)
106-
const PackageNameB = getPackageName(b)
99+
const packageNameA = getPackageName(a)
100+
const packageNameB = getPackageName(b)
107101

108-
if (PackageNameA < PackageNameB) return -1
109-
if (PackageNameA > PackageNameB) return 1
102+
if (packageNameA < packageNameB) return -1
103+
if (packageNameA > packageNameB) return 1
110104
return 0
111105
}
112106

tests/_helpers.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ function sortObjectWithRangeAlphabetically(t, options = {}) {
6767
...options,
6868
value: keysToObject(
6969
[
70-
70+
'@z-package/package@1.2.3',
7171
7272
73-
73+
'@a-package/package@1.2.3',
7474
75-
'@b-package',
76-
77-
75+
'@b-package/package',
76+
'@e-package/package@1.2.3',
77+
'@ch-package/package@1.2.3',
7878
7979
8080
],
@@ -84,11 +84,11 @@ function sortObjectWithRangeAlphabetically(t, options = {}) {
8484
expect ||
8585
keysToObject(
8686
[
87-
88-
'@b-package',
89-
90-
91-
87+
'@a-package/package@1.2.3',
88+
'@b-package/package',
89+
'@ch-package/package@1.2.3',
90+
'@e-package/package@1.2.3',
91+
'@z-package/package@1.2.3',
9292
9393
9494

tests/snapshots/cli.js.snap

34 Bytes
Binary file not shown.

tests/snapshots/deps.js.md

Lines changed: 240 additions & 240 deletions
Large diffs are not rendered by default.

tests/snapshots/deps.js.snap

86 Bytes
Binary file not shown.

tests/snapshots/eslint.js.snap

-20 Bytes
Binary file not shown.

tests/snapshots/fields.js.snap

0 Bytes
Binary file not shown.

tests/snapshots/main.js.snap

-7 Bytes
Binary file not shown.

tests/snapshots/prettier.js.snap

8 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)