From 662664d707d0c41bc6bebf1c950eaf422152c872 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Sat, 20 Aug 2022 09:33:59 +0200 Subject: [PATCH] chore: rename defaultValue option to default --- README.md | 2 +- examples/is-default-value.js | 2 +- index.js | 34 ++++++-------- test/default-values.js | 88 ++++++++++++++++++------------------ utils.js | 2 +- 5 files changed, 62 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 2bd78bd..3dfcf71 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ changes: times. If `true`, all values will be collected in an array. If `false`, values for the option are last-wins. **Default:** `false`. * `short` {string} A single character alias for the option. - * `defaultValue` {string | boolean | string[] | boolean[]} The default option value when it is not set by args. + * `default` {string | boolean | string[] | boolean[]} The default option value when it is not set by args. * `strict` {boolean} Should an error be thrown when unknown arguments are encountered, or when arguments are passed that do not match the `type` configured in `options`. diff --git a/examples/is-default-value.js b/examples/is-default-value.js index 8ffe8a0..0a67972 100644 --- a/examples/is-default-value.js +++ b/examples/is-default-value.js @@ -7,7 +7,7 @@ const { parseArgs } = require('..'); // in repo const options = { - file: { short: 'f', type: 'string', defaultValue: 'FOO' }, + file: { short: 'f', type: 'string', default: 'FOO' }, }; const { values, tokens } = parseArgs({ options, tokens: true }); diff --git a/index.js b/index.js index a35b16d..2a5657d 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,6 @@ const { ArrayPrototypeForEach, ArrayPrototypeIncludes, ArrayPrototypeMap, - ArrayPrototypeFilter, ArrayPrototypePush, ArrayPrototypePushApply, ArrayPrototypeShift, @@ -332,16 +331,16 @@ const parseArgs = (config = kEmptyObject) => { validateBoolean(multipleOption, `options.${longOption}.multiple`); } - if (ObjectHasOwn(optionConfig, 'defaultValue')) { - const defaultValue = objectGetOwn(optionConfig, 'defaultValue'); + if (ObjectHasOwn(optionConfig, 'default')) { + const defaultValue = objectGetOwn(optionConfig, 'default'); if (optionType === 'string' && !multipleOption) { - validateString(defaultValue, `options.${longOption}.defaultValue`); + validateString(defaultValue, `options.${longOption}.default`); } else if (optionType === 'string' && multipleOption) { - validateStringArray(defaultValue, `options.${longOption}.defaultValue`); + validateStringArray(defaultValue, `options.${longOption}.default`); } else if (optionType === 'boolean' && !multipleOption) { - validateBoolean(defaultValue, `options.${longOption}.defaultValue`); + validateBoolean(defaultValue, `options.${longOption}.default`); } else if (optionType === 'boolean' && multipleOption) { - validateBooleanArray(defaultValue, `options.${longOption}.defaultValue`); + validateBooleanArray(defaultValue, `options.${longOption}.default`); } } } @@ -374,21 +373,18 @@ const parseArgs = (config = kEmptyObject) => { }); // Phase 3: fill in default values for missing args - const defaultValueOptions = ArrayPrototypeFilter( - ObjectEntries(options), ({ 0: longOption, - 1: optionConfig }) => { - return useDefaultValueOption(longOption, optionConfig, result.values); - }); - - if (defaultValueOptions.length > 0) { - ArrayPrototypeForEach(defaultValueOptions, ({ 0: longOption, - 1: optionConfig }) => { + ArrayPrototypeForEach(ObjectEntries(options), ({ 0: longOption, + 1: optionConfig }) => { + const mustSetDefault = useDefaultValueOption(longOption, + optionConfig, + result.values); + if (mustSetDefault) { storeDefaultOption(longOption, - optionConfig.defaultValue, + objectGetOwn(optionConfig, 'default'), result.values); - }); + } + }); - } return result; }; diff --git a/test/default-values.js b/test/default-values.js index 24ee094..75dd1a7 100644 --- a/test/default-values.js +++ b/test/default-values.js @@ -5,75 +5,75 @@ const test = require('tape'); const { parseArgs } = require('../index.js'); -test('defaultValue must be a boolean when option type is boolean', (t) => { +test('default must be a boolean when option type is boolean', (t) => { const args = []; - const options = { alpha: { type: 'boolean', defaultValue: 'not a boolean' } }; + const options = { alpha: { type: 'boolean', default: 'not a boolean' } }; t.throws(() => { parseArgs({ args, options }); - }, /alpha\.defaultValue must be Boolean/ + }, /alpha\.default must be Boolean/ ); t.end(); }); -test('defaultValue must be a boolean array when option type is boolean and multiple', (t) => { +test('default must be a boolean array when option type is boolean and multiple', (t) => { const args = []; - const options = { alpha: { type: 'boolean', multiple: true, defaultValue: 'not an array' } }; + const options = { alpha: { type: 'boolean', multiple: true, default: 'not an array' } }; t.throws(() => { parseArgs({ args, options }); - }, /alpha\.defaultValue must be Array/ + }, /alpha\.default must be Array/ ); t.end(); }); -test('defaultValue must be a boolean array when option type is string and multiple is true', (t) => { +test('default must be a boolean array when option type is string and multiple is true', (t) => { const args = []; - const options = { alpha: { type: 'boolean', multiple: true, defaultValue: [true, true, 42] } }; + const options = { alpha: { type: 'boolean', multiple: true, default: [true, true, 42] } }; t.throws(() => { parseArgs({ args, options }); - }, /alpha\.defaultValue\[2\] must be Boolean/ + }, /alpha\.default\[2\] must be Boolean/ ); t.end(); }); -test('defaultValue must be a string when option type is string', (t) => { +test('default must be a string when option type is string', (t) => { const args = []; - const options = { alpha: { type: 'string', defaultValue: true } }; + const options = { alpha: { type: 'string', default: true } }; t.throws(() => { parseArgs({ args, options }); - }, /alpha\.defaultValue must be String/ + }, /alpha\.default must be String/ ); t.end(); }); -test('defaultValue must be an array when option type is string and multiple is true', (t) => { +test('default must be an array when option type is string and multiple is true', (t) => { const args = []; - const options = { alpha: { type: 'string', multiple: true, defaultValue: 'not an array' } }; + const options = { alpha: { type: 'string', multiple: true, default: 'not an array' } }; t.throws(() => { parseArgs({ args, options }); - }, /alpha\.defaultValue must be Array/ + }, /alpha\.default must be Array/ ); t.end(); }); -test('defaultValue must be a string array when option type is string and multiple is true', (t) => { +test('default must be a string array when option type is string and multiple is true', (t) => { const args = []; - const options = { alpha: { type: 'string', multiple: true, defaultValue: ['str', 42] } }; + const options = { alpha: { type: 'string', multiple: true, default: ['str', 42] } }; t.throws(() => { parseArgs({ args, options }); - }, /alpha\.defaultValue\[1\] must be String/ + }, /alpha\.default\[1\] must be String/ ); t.end(); }); -test('defaultValue accepted input when multiple is true', (t) => { +test('default accepted input when multiple is true', (t) => { const args = ['--inputStringArr', 'c', '--inputStringArr', 'd', '--inputBoolArr', '--inputBoolArr']; const options = { - inputStringArr: { type: 'string', multiple: true, defaultValue: ['a', 'b'] }, - emptyStringArr: { type: 'string', multiple: true, defaultValue: [] }, - fullStringArr: { type: 'string', multiple: true, defaultValue: ['a', 'b'] }, - inputBoolArr: { type: 'boolean', multiple: true, defaultValue: [false, true, false] }, - emptyBoolArr: { type: 'boolean', multiple: true, defaultValue: [] }, - fullBoolArr: { type: 'boolean', multiple: true, defaultValue: [false, true, false] }, + inputStringArr: { type: 'string', multiple: true, default: ['a', 'b'] }, + emptyStringArr: { type: 'string', multiple: true, default: [] }, + fullStringArr: { type: 'string', multiple: true, default: ['a', 'b'] }, + inputBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] }, + emptyBoolArr: { type: 'boolean', multiple: true, default: [] }, + fullBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] }, }; const expected = { values: { __proto__: null, inputStringArr: ['c', 'd'], @@ -88,12 +88,12 @@ test('defaultValue accepted input when multiple is true', (t) => { t.end(); }); -test('when defaultValue is set, the option must be added as result', (t) => { +test('when default is set, the option must be added as result', (t) => { const args = []; const options = { - a: { type: 'string', defaultValue: 'HELLO' }, - b: { type: 'boolean', defaultValue: false }, - c: { type: 'boolean', defaultValue: true } + a: { type: 'string', default: 'HELLO' }, + b: { type: 'boolean', default: false }, + c: { type: 'boolean', default: true } }; const expected = { values: { __proto__: null, a: 'HELLO', b: false, c: true }, positionals: [] }; @@ -103,12 +103,12 @@ test('when defaultValue is set, the option must be added as result', (t) => { t.end(); }); -test('when defaultValue is set, the args value takes precedence', (t) => { +test('when default is set, the args value takes precedence', (t) => { const args = ['--a', 'WORLD', '--b', '-c']; const options = { - a: { type: 'string', defaultValue: 'HELLO' }, - b: { type: 'boolean', defaultValue: false }, - c: { type: 'boolean', defaultValue: true } + a: { type: 'string', default: 'HELLO' }, + b: { type: 'boolean', default: false }, + c: { type: 'boolean', default: true } }; const expected = { values: { __proto__: null, a: 'WORLD', b: true, c: true }, positionals: [] }; @@ -118,12 +118,12 @@ test('when defaultValue is set, the args value takes precedence', (t) => { t.end(); }); -test('tokens should not include the defaultValue options', (t) => { +test('tokens should not include the default options', (t) => { const args = []; const options = { - a: { type: 'string', defaultValue: 'HELLO' }, - b: { type: 'boolean', defaultValue: false }, - c: { type: 'boolean', defaultValue: true } + a: { type: 'string', default: 'HELLO' }, + b: { type: 'boolean', default: false }, + c: { type: 'boolean', default: true } }; const expectedTokens = []; @@ -133,13 +133,13 @@ test('tokens should not include the defaultValue options', (t) => { t.end(); }); -test('tokens:true should not include the defaultValue options after the args input', (t) => { +test('tokens:true should not include the default options after the args input', (t) => { const args = ['--z', 'zero', 'positional-item']; const options = { z: { type: 'string' }, - a: { type: 'string', defaultValue: 'HELLO' }, - b: { type: 'boolean', defaultValue: false }, - c: { type: 'boolean', defaultValue: true } + a: { type: 'string', default: 'HELLO' }, + b: { type: 'boolean', default: false }, + c: { type: 'boolean', default: true } }; const expectedTokens = [ @@ -157,7 +157,7 @@ test('proto as default value must be ignored', (t) => { const options = Object.create(null); // eslint-disable-next-line no-proto - options.__proto__ = { type: 'string', defaultValue: 'HELLO' }; + options.__proto__ = { type: 'string', default: 'HELLO' }; const result = parseArgs({ args, options, allowPositionals: true }); const expected = { values: { __proto__: null }, positionals: [] }; @@ -168,10 +168,10 @@ test('proto as default value must be ignored', (t) => { test('multiple as false should expect a String and not an array', (t) => { const args = []; - const options = { alpha: { type: 'string', multiple: false, defaultValue: 42 } }; + const options = { alpha: { type: 'string', multiple: false, default: 42 } }; t.throws(() => { parseArgs({ args, options }); - }, /alpha\.defaultValue must be String/ + }, /alpha\.default must be String/ ); t.end(); }); diff --git a/utils.js b/utils.js index e0e2178..1434724 100644 --- a/utils.js +++ b/utils.js @@ -179,7 +179,7 @@ function findLongOptionForShort(shortOption, options) { * @param {object} values - option values returned in `values` by parseArgs */ function useDefaultValueOption(longOption, optionConfig, values) { - return objectGetOwn(optionConfig, 'defaultValue') !== undefined && + return objectGetOwn(optionConfig, 'default') !== undefined && values[longOption] === undefined; }