UNPKG

1.23 kBJavaScriptView Raw
1module.exports = (sargs) => {
2 let props = {}
3 let lones = []
4
5 const convertIfApplicable = (value) => (isNaN(value) ? (value.toString().toLowerCase() === 'true' ? true : (value.toString().toLowerCase() === 'false' ? false : value)) : Number(value))
6 const removeStartHyphens = (value) => value.replace(/^\-+/g, '')
7
8 for (let i = 0; i < sargs.length; i++) {
9 const equalsIndex = sargs[i].indexOf('=')
10 const isNextRefProp = sargs[i].charAt(0) === '-' && sargs.length - 1 >= i + 1 && sargs[i + 1].indexOf('=') === -1 && sargs[i + 1].charAt(0) !== '-'
11 const argName = equalsIndex === -1 ? removeStartHyphens(sargs[i]) : removeStartHyphens(sargs[i].slice(0, equalsIndex))
12
13 if (equalsIndex !== -1) {
14 props[argName] = convertIfApplicable(sargs[i].slice(equalsIndex + 1))
15 }
16 else if (isNextRefProp) {
17 props[argName] = convertIfApplicable(sargs[i + 1])
18 i++
19 } else if (sargs[i].charAt(0) === '-') {
20 if (sargs[i].charAt(1) === '-') {
21 props[argName] = true
22 }
23 else {
24 for (let b = 0; b < argName.length; b++) {
25 props[argName.charAt(b)] = true
26 }
27 }
28 } else {
29 lones.push(convertIfApplicable(argName))
30 }
31 }
32
33 return Object.assign(props, {
34 '_': lones
35 })
36}
\No newline at end of file