Skip to content

Commit

Permalink
feat!: create result.values with null prototype (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Apr 17, 2022
1 parent 53ecc2d commit 9d539c3
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 240 deletions.
22 changes: 6 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const {
ArrayPrototypeShift,
ArrayPrototypeSlice,
ArrayPrototypePush,
ObjectDefineProperty,
ObjectEntries,
ObjectPrototypeHasOwnProperty: ObjectHasOwn,
StringPrototypeCharAt,
Expand Down Expand Up @@ -117,19 +116,9 @@ function checkOptionUsage(longOption, optionValue, options,
*/
function storeOption(longOption, optionValue, options, values) {
if (longOption === '__proto__') {
return;
return; // No. Just no.
}

// Can be removed when value has a null prototype
const safeAssignProperty = (obj, prop, value) => {
ObjectDefineProperty(obj, prop, {
value,
writable: true,
enumerable: true,
configurable: true
});
};

// We store based on the option value rather than option type,
// preserving the users intent for author to deal with.
const newValue = optionValue ?? true;
Expand All @@ -138,13 +127,14 @@ function storeOption(longOption, optionValue, options, values) {
// values[longOption] starts out not present,
// first value is added as new array [newValue],
// subsequent values are pushed to existing array.
if (ObjectHasOwn(values, longOption)) {
// (note: values has null prototype, so simpler usage)
if (values[longOption]) {
ArrayPrototypePush(values[longOption], newValue);
} else {
safeAssignProperty(values, longOption, [newValue]);
values[longOption] = [newValue];
}
} else {
safeAssignProperty(values, longOption, newValue);
values[longOption] = newValue;
}
}

Expand Down Expand Up @@ -184,7 +174,7 @@ const parseArgs = (config = { __proto__: null }) => {
);

const result = {
values: {},
values: { __proto__: null },
positionals: []
};

Expand Down
14 changes: 7 additions & 7 deletions test/dash.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ const { parseArgs } = require('../index.js');
// A different usage and example is `git switch -` to switch back to the previous branch.

test("dash: when args include '-' used as positional then result has '-' in positionals", (t) => {
const passedArgs = ['-'];
const expected = { values: {}, positionals: ['-'] };
const args = ['-'];
const expected = { values: { __proto__: null }, positionals: ['-'] };

const result = parseArgs({ args: passedArgs });
const result = parseArgs({ args });

t.deepEqual(result, expected);
t.end();
});

// If '-' is a valid positional, it is symmetrical to allow it as an option value too.
test("dash: when args include '-' used as space-separated option value then result has '-' in option value", (t) => {
const passedArgs = ['-v', '-'];
const passedOptions = { v: { type: 'string' } };
const expected = { values: { v: '-' }, positionals: [] };
const args = ['-v', '-'];
const options = { v: { type: 'string' } };
const expected = { values: { __proto__: null, v: '-' }, positionals: [] };

const result = parseArgs({ args: passedArgs, options: passedOptions });
const result = parseArgs({ args, options });

t.deepEqual(result, expected);
t.end();
Expand Down

0 comments on commit 9d539c3

Please sign in to comment.