UNPKG

690 BJavaScriptView Raw
1/**
2 * Check if it is a local path
3 * @param {string} input
4 * @returns {boolean}
5 */
6function isLocalPath(input) {
7 return /^[./]|(^[a-zA-Z]:)/.test(input)
8}
9
10/**
11 * Check if a package is scoped package
12 * @param {string} input
13 * @returns {boolean}
14 */
15function isScoped(input) {
16 return input.startsWith('@')
17}
18
19/**
20 * @param {string} input
21 * @param {string} prefix
22 * @returns {string}
23 */
24module.exports = (input, prefix) => {
25 if (isLocalPath(input) || !prefix) {
26 return input
27 }
28
29 if (isScoped(input)) {
30 const RE = new RegExp(`/(${prefix})?`)
31 return input.replace(RE, `/${prefix}`)
32 }
33
34 const RE = new RegExp(`^(${prefix})?`)
35 return input.replace(RE, prefix)
36}