Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: updates to comments and code per feedback #101

Merged
merged 4 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ const parseArgs = ({
ArrayPrototypePush(expanded, `-${shortOption}`);
} else {
// String option in middle. Yuck.
// ToDo: if strict then throw
// Expand -abfFILE to -a -b -fFILE
ArrayPrototypePush(expanded, `-${StringPrototypeSlice(arg, index)}`);
break; // finished short group
Expand Down
16 changes: 8 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ test('short option group does not consume subsequent positional', (t) => {
t.end();
});

// // See: Guideline 5 https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
// See: Guideline 5 https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
test('if terminal of short-option group configured `type: "string"`, subsequent positional is stored', (t) => {
const passedArgs = ['-rvf', 'foo'];
const passedOptions = { f: { type: 'string' } };
Expand Down Expand Up @@ -249,7 +249,7 @@ test('when expecting `multiple:true` boolean option and option used multiple tim
t.end();
});

test('order of option and positional does not matter (per README)', function(t) {
test('order of option and positional does not matter (per README)', (t) => {
const passedArgs1 = ['--foo=bar', 'baz'];
const passedArgs2 = ['baz', '--foo=bar'];
const passedOptions = { foo: { type: 'string' } };
Expand Down Expand Up @@ -362,17 +362,17 @@ test('invalid argument passed for options', (t) => {
const passedArgs = ['--so=wat'];
const passedOptions = 'bad value';

t.throws(function() { parseArgs({ args: passedArgs, options: passedOptions }); }, {
t.throws(() => { parseArgs({ args: passedArgs, options: passedOptions }); }, {
code: 'ERR_INVALID_ARG_TYPE'
});

t.end();
});

test('then type property missing for option then throw', function(t) {
test('then type property missing for option then throw', (t) => {
const knownOptions = { foo: { } };

t.throws(function() { parseArgs({ options: knownOptions }); }, {
t.throws(() => { parseArgs({ options: knownOptions }); }, {
code: 'ERR_INVALID_ARG_TYPE'
});

Expand All @@ -383,7 +383,7 @@ test('boolean passed to "type" option', (t) => {
const passedArgs = ['--so=wat'];
const passedOptions = { foo: { type: true } };

t.throws(function() { parseArgs({ args: passedArgs, options: passedOptions }); }, {
t.throws(() => { parseArgs({ args: passedArgs, options: passedOptions }); }, {
code: 'ERR_INVALID_ARG_TYPE'
});

Expand All @@ -394,7 +394,7 @@ test('invalid union value passed to "type" option', (t) => {
const passedArgs = ['--so=wat'];
const passedOptions = { foo: { type: 'str' } };

t.throws(function() { parseArgs({ args: passedArgs, options: passedOptions }); }, {
t.throws(() => { parseArgs({ args: passedArgs, options: passedOptions }); }, {
code: 'ERR_INVALID_ARG_TYPE'
});

Expand All @@ -405,7 +405,7 @@ test('invalid short option length', (t) => {
const passedArgs = [];
const passedOptions = { foo: { short: 'fo', type: 'boolean' } };

t.throws(function() { parseArgs({ args: passedArgs, options: passedOptions }); }, {
t.throws(() => { parseArgs({ args: passedArgs, options: passedOptions }); }, {
code: 'ERR_INVALID_ARG_VALUE'
});

Expand Down
18 changes: 9 additions & 9 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function isOptionValue(value) {
}

/**
* Determines if `arg` is a just a short option.
* Determines if `arg` is just a short option.
* @example '-f'
*/
function isLoneShortOption(arg) {
Expand All @@ -57,7 +57,7 @@ function isLoneShortOption(arg) {
* @example
* isLoneLongOption('a') // returns false
* isLoneLongOption('-a') // returns false
* isLoneLongOption('--foo) // returns true
* isLoneLongOption('--foo') // returns true
* isLoneLongOption('--foo=bar') // returns false
*/
function isLoneLongOption(arg) {
Expand All @@ -69,7 +69,7 @@ function isLoneLongOption(arg) {
/**
* Determines if `arg` is a long option and value in the same argument.
* @example
* isLongOptionAndValue('--foo) // returns false
* isLongOptionAndValue('--foo') // returns false
* isLongOptionAndValue('--foo=bar') // returns true
*/
function isLongOptionAndValue(arg) {
Expand All @@ -90,14 +90,14 @@ function isLongOptionAndValue(arg) {
* isShortOptionGroup('-ab', {}) // returns true
* // -fb is an option and a value, not a short option group
* isShortOptionGroup('-fb', {
* options: { f: { type: 'string' }}
* options: { f: { type: 'string' } }
* }) // returns false
* isShortOptionGroup('-bf', {
* options: { f: { type: 'string' }}
* options: { f: { type: 'string' } }
* }) // returns true
* // -bfb is an edge case, return true and caller sorts it out
* isShortOptionGroup('-bfb', {
* options: { f: { type: 'string' }}
* options: { f: { type: 'string' } }
* }) // returns true
*/
function isShortOptionGroup(arg, options) {
Expand All @@ -111,10 +111,10 @@ function isShortOptionGroup(arg, options) {
}

/**
* Determine is arg is a short string option followed by its value.
* Determine if arg is a short string option followed by its value.
* @example
* isShortOptionAndValue('-a, {}); // returns false
* isShortOptionAndValue('-ab, {}); // returns false
* isShortOptionAndValue('-a', {}); // returns false
* isShortOptionAndValue('-ab', {}); // returns false
* isShortOptionAndValue('-fFILE', {
* options: { foo: { short: 'f', type: 'string' }}
* }) // returns true
Expand Down