-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
25 lines (22 loc) · 810 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function stringInvariant(description, value) {
const type = typeof value;
if (type !== 'string') {
throw new TypeError(
`Invalid package ${description}. Expected type 'string' but got '${type}'`
);
}
}
module.exports = function(prefix, packageName) {
stringInvariant('prefix', prefix);
stringInvariant('name', packageName);
const hasScope = packageName.startsWith('@');
const splitPoint = hasScope ? packageName.indexOf('/') + 1 : 0;
const scope = packageName.substr(0, splitPoint);
const baseName = packageName.substr(splitPoint);
const needsPrefix = !baseName.startsWith(prefix);
const needsHyphen = needsPrefix && prefix.length > 0;
return [scope, needsPrefix && prefix, needsHyphen && '-', baseName]
.filter(Boolean)
.join('')
.replace(/\s+/g, '-');
};