Skip to content

Commit d6817f0

Browse files
committed
move splitPackageNames to its own module
1 parent a9c4b15 commit d6817f0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/utils/split-package-names.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const splitPackageNames = (path) => {
4+
return path.split('/')
5+
// combine scoped parts
6+
.reduce((parts, part) => {
7+
if (parts.length === 0)
8+
return [part]
9+
10+
const lastPart = parts[parts.length - 1]
11+
// check if previous part is the first part of a scoped package
12+
if (lastPart[0] === '@' && !lastPart.includes('/'))
13+
parts[parts.length - 1] += '/' + part
14+
else
15+
parts.push(part)
16+
17+
return parts
18+
}, [])
19+
.join('/node_modules/')
20+
.replace(/(\/node_modules)+/, '/node_modules')
21+
}
22+
23+
module.exports = splitPackageNames
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const splitPackageNames = require('../../../lib/utils/split-package-names.js')
5+
6+
test('splitPackageNames', t => {
7+
const assertions = [
8+
['semver', 'semver'],
9+
['read-pkg/semver', 'read-pkg/node_modules/semver'],
10+
['@npmcli/one/@npmcli/two', '@npmcli/one/node_modules/@npmcli/two'],
11+
['@npmcli/one/semver', '@npmcli/one/node_modules/semver'],
12+
]
13+
14+
for (const [input, expected] of assertions)
15+
t.equal(splitPackageNames(input), expected, `split ${input} correctly`)
16+
t.end()
17+
})

0 commit comments

Comments
 (0)