1const { cleanUrl } = require('npm-registry-fetch') 2const isString = (v) => typeof v === 'string' 3 4// split on \s|= similar to how nopt parses options 5const splitAndReplace = (str) => { 6 // stateful regex, don't move out of this scope 7 const splitChars = /[\s=]/g 8 9 let match = null 10 let result = '' 11 let index = 0 12 while (match = splitChars.exec(str)) { 13 result += cleanUrl(str.slice(index, match.index)) + match[0] 14 index = splitChars.lastIndex 15 } 16 17 return result + cleanUrl(str.slice(index)) 18} 19 20// replaces auth info in an array of arguments or in a strings 21function replaceInfo (arg) { 22 if (isString(arg)) { 23 return splitAndReplace(arg) 24 } else if (Array.isArray(arg)) { 25 return arg.map((a) => isString(a) ? splitAndReplace(a) : a) 26 } 27 28 return arg 29} 30 31module.exports = replaceInfo 32