Skip to content

Commit 981533a

Browse files
fringdbcoe
authored andcommitted
fix: address bug with camelCased flattened keys (#32)
1 parent 7375966 commit 981533a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ function unparseOption(key, value, unparsed) {
3939
const flattened = flatten(value, { safe: true });
4040

4141
for (const flattenedKey in flattened) {
42-
unparseOption(`${key}.${flattenedKey}`, flattened[flattenedKey], unparsed);
42+
if (!isCamelCased(flattenedKey, flattened)) {
43+
unparseOption(`${key}.${flattenedKey}`, flattened[flattenedKey], unparsed);
44+
}
4345
}
4446
// Fallback case (numbers and other types)
4547
} else if (value != null) {

test/index.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ it('should keep camel-cased keys if standard ones were not found on argv', () =>
8080
expect(unparse(argv)).toEqual(['--someNumber', '1']);
8181
});
8282

83+
it('should not duplicate nested keys that are camel-cased', () => {
84+
const argv = parse(['--paths.some-string', 'foo']);
85+
86+
expect(unparse(argv)).toEqual(['--paths.some-string', 'foo']);
87+
});
88+
89+
it('should keep nested camel-cased keys if standard ones were not found on argv', () => {
90+
const argv = parse(['--paths.someNumber', '1']);
91+
92+
expect(unparse(argv)).toEqual(['--paths.someNumber', '1']);
93+
});
94+
8395
it('should ignore nullish option values', () => {
8496
const argv = parse(['node', 'cli.js'], {
8597
number: 'n',

0 commit comments

Comments
 (0)