Skip to content

Commit 886e99c

Browse files
author
Benjamin E. Coe
authored
build: add linter to make code style closer to Node.js' (#15)
1 parent ca3c887 commit 886e99c

File tree

4 files changed

+1359
-136
lines changed

4 files changed

+1359
-136
lines changed

.eslintrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": ["plugin:eslint-plugin-node-core/recommended"],
3+
"env": {
4+
"node": true
5+
},
6+
"rules": {
7+
"linebreak-style": 0
8+
}
9+
}

index.js

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,83 +5,101 @@ const parseArgs = (
55
options = {}
66
) => {
77
if (typeof options !== 'object' || options === null) {
8-
throw new Error('Whoops!')
8+
throw new Error('Whoops!');
99
}
1010
if (options.withValue !== undefined && !Array.isArray(options.withValue)) {
11-
throw new Error('Whoops! options.withValue should be an array.')
11+
throw new Error('Whoops! options.withValue should be an array.');
1212
}
1313

14-
let result = {
14+
const result = {
1515
args: {},
1616
values: {},
1717
positionals: []
18-
}
18+
};
1919

20-
let pos = 0
20+
let pos = 0;
2121
while (pos < argv.length) {
22-
let arg = argv[pos]
22+
let arg = argv[pos];
2323

2424
if (arg.startsWith('-')) {
2525
// Everything after a bare '--' is considered a positional argument
2626
// and is returned verbatim
2727
if (arg === '--') {
28-
result.positionals.push(...argv.slice(++pos))
29-
return result
30-
}
31-
// look for shortcodes: -fXzy
32-
else if (arg.charAt(1) !== '-') {
33-
throw new Error('What are we doing with shortcodes!?!')
28+
result.positionals.push(...argv.slice(++pos));
29+
return result;
30+
} else if (arg.charAt(1) !== '-') { // Look for shortcodes: -fXzy
31+
throw new Error('What are we doing with shortcodes!?!');
3432
}
3533

3634
// Any number of leading dashes are allowed
3735
// remove all leading dashes
38-
arg = arg.replace(/^-+/, '')
36+
arg = arg.replace(/^-+/, '');
3937

4038
if (arg.includes('=')) {
41-
//withValue equals(=) case
42-
const argParts = arg.split('=')
39+
// withValue equals(=) case
40+
const argParts = arg.split('=');
4341

44-
result.args[argParts[0]] = true
45-
//If withValue option is specified, take 2nd part after '=' as value, else set value as undefined
46-
const val = options.withValue && options.withValue.includes(argParts[0]) ? argParts[1] : undefined
47-
//Append value to previous arg values array for case of multiples option, else add to empty array
42+
result.args[argParts[0]] = true;
43+
// If withValue option is specified, take 2nd part after '=' as value,
44+
// else set value as undefined
45+
const val = options.withValue &&
46+
options.withValue.includes(argParts[0]) ?
47+
argParts[1] : undefined;
48+
// Append value to previous arg values array for case of multiples
49+
// option, else add to empty array
4850
result.values[argParts[0]] = [].concat(
49-
options.multiples && options.multiples.includes(argParts[0]) && result.values[argParts[0]] || [],
51+
options.multiples &&
52+
options.multiples.includes(argParts[0]) &&
53+
result.values[argParts[0]] || [],
5054
val,
51-
)
52-
} else if (pos + 1 < argv.length && !argv[pos+1].startsWith('-')) {
53-
//withValue option should also support setting values when '=' isn't used
54-
//ie. both --foo=b and --foo b should work
55+
);
56+
} else if (pos + 1 < argv.length && !argv[pos + 1].startsWith('-')) {
57+
// withValue option should also support setting values when '=
58+
// isn't used ie. both --foo=b and --foo b should work
5559

56-
result.args[arg] = true
57-
//If withValue option is specified, take next position arguement as value and then increment pos so that we don't re-evaluate that arg, else set value as undefined
58-
//ie. --foo b --bar c, after setting b as the value for foo, evaluate --bar next and skip 'b'
59-
const val = options.withValue && options.withValue.includes(arg) ? argv[++pos] : undefined
60-
//Append value to previous arg values array for case of multiples option, else add to empty array
60+
result.args[arg] = true;
61+
// If withValue option is specified, take next position arguement as
62+
// value and then increment pos so that we don't re-evaluate that
63+
// arg, else set value as undefined ie. --foo b --bar c, after setting
64+
// b as the value for foo, evaluate --bar next and skip 'b'
65+
const val = options.withValue && options.withValue.includes(arg) ?
66+
argv[++pos] :
67+
undefined;
68+
// Append value to previous arg values array for case of multiples
69+
// option, else add to empty array
6170
result.values[arg] = [].concat(
62-
options.multiples && options.multiples.includes(arg) && result.values[arg] ? result.values[arg] : [],
63-
val)
71+
options.multiples && options.multiples.includes(arg) &&
72+
result.values[arg] ?
73+
result.values[arg] :
74+
[],
75+
val);
6476
} else {
65-
//cases when an arg is specified without a value, example '--foo --bar' <- 'foo' and 'bar' args should be set to true and have value as undefined
66-
result.args[arg] = true
67-
//Append undefined to previous arg values array for case of multiples option, else add to empty array
77+
// Cases when an arg is specified without a value, example
78+
// '--foo --bar' <- 'foo' and 'bar' args should be set to true and
79+
// shave value as undefined
80+
result.args[arg] = true;
81+
// Append undefined to previous arg values array for case of
82+
// multiples option, else add to empty array
6883
result.values[arg] = [].concat(
69-
options.multiples && options.multiples.includes(arg) && result.values[arg] ? result.values[arg] : [],
84+
options.multiples && options.multiples.includes(arg) &&
85+
result.values[arg] ?
86+
result.values[arg] :
87+
[],
7088
undefined
71-
)
89+
);
7290
}
7391

7492
} else {
75-
//Arguements without a dash prefix are considered "positional"
76-
result.positionals.push(arg)
93+
// Arguements without a dash prefix are considered "positional"
94+
result.positionals.push(arg);
7795
}
7896

79-
pos++
97+
pos++;
8098
}
8199

82-
return result
83-
}
100+
return result;
101+
};
84102

85103
module.exports = {
86104
parseArgs
87-
}
105+
};

0 commit comments

Comments
 (0)