File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 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 ( / ( \/ n o d e _ m o d u l e s ) + / , '/node_modules' )
21+ }
22+
23+ module . exports = splitPackageNames
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments