Skip to content

Commit a35c9b7

Browse files
author
Benjamin E. Coe
authored
refactor: use primordials (#37)
1 parent 421c6e2 commit a35c9b7

7 files changed

Lines changed: 566 additions & 96 deletions

File tree

.eslintrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"extends": ["plugin:eslint-plugin-node-core/recommended"],
33
"env": {
4-
"node": true
4+
"node": true,
5+
"es6": true
56
},
67
"rules": {
78
"linebreak-style": 0
8-
}
9+
},
10+
"ignorePatterns": ["README.md"]
911
}

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
coverage
2+
test
3+
.nycrc
4+
.eslintrc
5+
.github
6+
CONTRIBUTING.md

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const { parseArgs } = require('@pkgjs/parseargs');
9898

9999
```js
100100
// unconfigured
101+
const { parseArgs } = require('@pkgjs/parseargs');
101102
const argv = ['-f', '--foo=a', '--bar', 'b'];
102103
const options = {};
103104
const { flags, values, positionals } = parseArgs(argv, options);
@@ -107,6 +108,7 @@ const { flags, values, positionals } = parseArgs(argv, options);
107108
```
108109

109110
```js
111+
const { parseArgs } = require('@pkgjs/parseargs');
110112
// withValue
111113
const argv = ['-f', '--foo=a', '--bar', 'b'];
112114
const options = {
@@ -119,6 +121,7 @@ const { flags, values, positionals } = parseArgs(argv, options);
119121
```
120122

121123
```js
124+
const { parseArgs } = require('@pkgjs/parseargs');
122125
// withValue & multiples
123126
const argv = ['-f', '--foo=a', '--foo', 'b'];
124127
const options = {
@@ -132,6 +135,7 @@ const { flags, values, positionals } = parseArgs(argv, options);
132135
```
133136

134137
```js
138+
const { parseArgs } = require('@pkgjs/parseargs');
135139
// shorts
136140
const argv = ['-f', 'b'];
137141
const options = {

index.js

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
'use strict';
22

3+
const {
4+
ArrayIsArray,
5+
ArrayPrototypeConcat,
6+
ArrayPrototypeIncludes,
7+
ArrayPrototypeSlice,
8+
ArrayPrototypePush,
9+
StringPrototypeCharAt,
10+
StringPrototypeIncludes,
11+
StringPrototypeSlice,
12+
StringPrototypeSplit,
13+
StringPrototypeStartsWith,
14+
} = require('./primordials');
15+
316
function getMainArgs() {
417
// This function is a placeholder for proposed process.mainArgs.
518
// Work out where to slice process.argv for user supplied arguments.
@@ -18,18 +31,20 @@ function getMainArgs() {
1831
// (Not included in tests as hopefully temporary example.)
1932
/* c8 ignore next 3 */
2033
if (process.versions && process.versions.electron && !process.defaultApp) {
21-
return process.argv.slice(1);
34+
return ArrayPrototypeSlice(process.argv, 1);
2235
}
2336

2437
// Check node options for scenarios where user CLI args follow executable.
2538
const execArgv = process.execArgv;
26-
if (execArgv.includes('-e') || execArgv.includes('--eval') ||
27-
execArgv.includes('-p') || execArgv.includes('--print')) {
28-
return process.argv.slice(1);
39+
if (StringPrototypeIncludes(execArgv, '-e') ||
40+
StringPrototypeIncludes(execArgv, '--eval') ||
41+
StringPrototypeIncludes(execArgv, '-p') ||
42+
StringPrototypeIncludes(execArgv, '--print')) {
43+
return ArrayPrototypeSlice(process.argv, 1);
2944
}
3045

3146
// Normally first two arguments are executable and script, then CLI arguments
32-
return process.argv.slice(2);
47+
return ArrayPrototypeSlice(process.argv, 2);
3348
}
3449

3550
const parseArgs = (
@@ -39,7 +54,7 @@ const parseArgs = (
3954
if (typeof options !== 'object' || options === null) {
4055
throw new Error('Whoops!');
4156
}
42-
if (options.withValue !== undefined && !Array.isArray(options.withValue)) {
57+
if (options.withValue !== undefined && !ArrayIsArray(options.withValue)) {
4358
throw new Error('Whoops! options.withValue should be an array.');
4459
}
4560

@@ -53,37 +68,44 @@ const parseArgs = (
5368
while (pos < argv.length) {
5469
let arg = argv[pos];
5570

56-
if (arg.startsWith('-')) {
71+
if (StringPrototypeStartsWith(arg, '-')) {
5772
// Everything after a bare '--' is considered a positional argument
5873
// and is returned verbatim
5974
if (arg === '--') {
60-
result.positionals.push(...argv.slice(++pos));
75+
result.positionals = ArrayPrototypeConcat(
76+
result.positionals,
77+
ArrayPrototypeSlice(argv, ++pos)
78+
);
6179
return result;
62-
} else if (arg.charAt(1) !== '-') { // Look for shortcodes: -fXzy
80+
} else if (
81+
StringPrototypeCharAt(arg, 1) !== '-'
82+
) { // Look for shortcodes: -fXzy
6383
throw new Error('What are we doing with shortcodes!?!');
6484
}
6585

66-
arg = arg.slice(2); // remove leading --
86+
arg = StringPrototypeSlice(arg, 2); // remove leading --
6787

68-
if (arg.includes('=')) {
88+
if (StringPrototypeIncludes(arg, '=')) {
6989
// withValue equals(=) case
70-
const argParts = arg.split('=');
90+
const argParts = StringPrototypeSplit(arg, '=');
7191

7292
result.flags[argParts[0]] = true;
7393
// If withValue option is specified, take 2nd part after '=' as value,
7494
// else set value as undefined
7595
const val = options.withValue &&
76-
options.withValue.includes(argParts[0]) ?
96+
ArrayPrototypeIncludes(options.withValue, argParts[0]) ?
7797
argParts[1] : undefined;
7898
// Append value to previous values array for case of multiples
7999
// option, else add to empty array
80-
result.values[argParts[0]] = [].concat(
81-
options.multiples &&
82-
options.multiples.includes(argParts[0]) &&
100+
result.values[argParts[0]] = ArrayPrototypeConcat([],
101+
options.multiples &&
102+
ArrayPrototypeIncludes(options.multiples, argParts[0]) &&
83103
result.values[argParts[0]] || [],
84-
val,
104+
val,
85105
);
86-
} else if (pos + 1 < argv.length && !argv[pos + 1].startsWith('-')) {
106+
} else if (pos + 1 < argv.length &&
107+
!StringPrototypeStartsWith(argv[pos + 1], '-')
108+
) {
87109
// withValue option should also support setting values when '=
88110
// isn't used ie. both --foo=b and --foo b should work
89111

@@ -92,36 +114,39 @@ const parseArgs = (
92114
// value and then increment pos so that we don't re-evaluate that
93115
// arg, else set value as undefined ie. --foo b --bar c, after setting
94116
// b as the value for foo, evaluate --bar next and skip 'b'
95-
const val = options.withValue && options.withValue.includes(arg) ?
96-
argv[++pos] :
117+
const val = options.withValue &&
118+
ArrayPrototypeIncludes(options.withValue, arg) ? argv[++pos] :
97119
undefined;
98120
// Append value to previous values array for case of multiples
99121
// option, else add to empty array
100-
result.values[arg] = [].concat(
101-
options.multiples && options.multiples.includes(arg) &&
102-
result.values[arg] ?
103-
result.values[arg] :
104-
[],
105-
val);
122+
result.values[arg] = ArrayPrototypeConcat(
123+
[],
124+
options.multiples &&
125+
ArrayPrototypeIncludes(options.multiples, arg) &&
126+
result.values[arg] ||
127+
[],
128+
val
129+
);
106130
} else {
107131
// Cases when an arg is specified without a value, example
108132
// '--foo --bar' <- 'foo' and 'bar' flags should be set to true and
109133
// shave value as undefined
110134
result.flags[arg] = true;
111135
// Append undefined to previous values array for case of
112136
// multiples option, else add to empty array
113-
result.values[arg] = [].concat(
114-
options.multiples && options.multiples.includes(arg) &&
115-
result.values[arg] ?
116-
result.values[arg] :
117-
[],
137+
result.values[arg] = ArrayPrototypeConcat(
138+
[],
139+
options.multiples &&
140+
ArrayPrototypeIncludes(options.multiples, arg) &&
141+
result.values[arg] ||
142+
[],
118143
undefined
119144
);
120145
}
121146

122147
} else {
123148
// Arguements without a dash prefix are considered "positional"
124-
result.positionals.push(arg);
149+
ArrayPrototypePush(result.positionals, arg);
125150
}
126151

127152
pos++;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"scripts": {
77
"coverage": "c8 --check-coverage node test/index.js",
88
"test": "c8 node test/index.js",
9-
"posttest": "eslint index.js",
10-
"fix": "eslint index.js --fix"
9+
"posttest": "eslint .",
10+
"fix": "npm run posttest -- --fix"
1111
},
1212
"repository": {
1313
"type": "git",

0 commit comments

Comments
 (0)