forked from npm/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace-info.js
31 lines (25 loc) · 817 Bytes
/
replace-info.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
26
27
28
29
30
31
const cleanUrl = require('npm-registry-fetch/lib/clean-url')
const isString = (v) => typeof v === 'string'
// split on \s|= similar to how nopt parses options
const splitAndReplace = (str) => {
// stateful regex, don't move out of this scope
const splitChars = /[\s=]/g
let match = null
let result = ''
let index = 0
while (match = splitChars.exec(str)) {
result += cleanUrl(str.slice(index, match.index)) + match[0]
index = splitChars.lastIndex
}
return result + cleanUrl(str.slice(index))
}
// replaces auth info in an array of arguments or in a strings
function replaceInfo (arg) {
if (isString(arg)) {
return splitAndReplace(arg)
} else if (Array.isArray(arg)) {
return arg.map((a) => isString(a) ? splitAndReplace(a) : a)
}
return arg
}
module.exports = replaceInfo