diff --git a/index.js b/index.js index d01e99e..a35b16d 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,7 @@ const { } = require('./internal/validators'); const { - kEmptyObject + kEmptyObject, } = require('./internal/util'); const { @@ -154,10 +154,9 @@ function storeOption(longOption, optionValue, options, values) { * | boolean * | string[] * | boolean[]} optionValue - default value from option config - * @param {object} options - option configs, from parseArgs({ options }) * @param {object} values - option values returned in `values` by parseArgs */ -function storeDefaultOption(longOption, optionValue, options, values) { +function storeDefaultOption(longOption, optionValue, values) { if (longOption === '__proto__') { return; // No. Just no. } @@ -376,8 +375,9 @@ const parseArgs = (config = kEmptyObject) => { // Phase 3: fill in default values for missing args const defaultValueOptions = ArrayPrototypeFilter( - ObjectEntries(options), (option) => { - return useDefaultValueOption(option, result.values); + ObjectEntries(options), ({ 0: longOption, + 1: optionConfig }) => { + return useDefaultValueOption(longOption, optionConfig, result.values); }); if (defaultValueOptions.length > 0) { @@ -385,7 +385,6 @@ const parseArgs = (config = kEmptyObject) => { 1: optionConfig }) => { storeDefaultOption(longOption, optionConfig.defaultValue, - options, result.values); }); diff --git a/internal/validators.js b/internal/validators.js index 9bf79f3..b5ac4fb 100644 --- a/internal/validators.js +++ b/internal/validators.js @@ -1,5 +1,10 @@ 'use strict'; +// This file is a proxy of the original file located at: +// https://github.com/nodejs/node/blob/main/lib/internal/validators.js +// Every addition or modification to this file must be evaluated +// during the PR review. + const { ArrayIsArray, ArrayPrototypeIncludes, diff --git a/test/default-values.js b/test/default-values.js index a4a83a0..24ee094 100644 --- a/test/default-values.js +++ b/test/default-values.js @@ -166,7 +166,7 @@ test('proto as default value must be ignored', (t) => { }); -test('multiple as false should not expect an array', (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 } }; t.throws(() => { diff --git a/utils.js b/utils.js index 8b4dd95..e0e2178 100644 --- a/utils.js +++ b/utils.js @@ -174,12 +174,12 @@ function findLongOptionForShort(shortOption, options) { * Check if the given option includes a default value * and that option has not been set by the input args. * - * @param {array} options - option configs entry, from parseArgs({ options }) + * @param {string} longOption - long option name e.g. 'foo' + * @param {object} optionConfig - the option configuration properties * @param {object} values - option values returned in `values` by parseArgs */ -function useDefaultValueOption({ 0: longOption, - 1: optionConfig }, values) { - return optionConfig.defaultValue !== undefined && +function useDefaultValueOption(longOption, optionConfig, values) { + return objectGetOwn(optionConfig, 'defaultValue') !== undefined && values[longOption] === undefined; }