-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move splitPackageNames to its own module
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict' | ||
|
||
const splitPackageNames = (path) => { | ||
return path.split('/') | ||
// combine scoped parts | ||
.reduce((parts, part) => { | ||
if (parts.length === 0) | ||
return [part] | ||
|
||
const lastPart = parts[parts.length - 1] | ||
// check if previous part is the first part of a scoped package | ||
if (lastPart[0] === '@' && !lastPart.includes('/')) | ||
parts[parts.length - 1] += '/' + part | ||
else | ||
parts.push(part) | ||
|
||
return parts | ||
}, []) | ||
.join('/node_modules/') | ||
.replace(/(\/node_modules)+/, '/node_modules') | ||
} | ||
|
||
module.exports = splitPackageNames |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const splitPackageNames = require('../../../lib/utils/split-package-names.js') | ||
|
||
test('splitPackageNames', t => { | ||
const assertions = [ | ||
['semver', 'semver'], | ||
['read-pkg/semver', 'read-pkg/node_modules/semver'], | ||
['@npmcli/one/@npmcli/two', '@npmcli/one/node_modules/@npmcli/two'], | ||
['@npmcli/one/semver', '@npmcli/one/node_modules/semver'], | ||
] | ||
|
||
for (const [input, expected] of assertions) | ||
t.equal(splitPackageNames(input), expected, `split ${input} correctly`) | ||
t.end() | ||
}) |