Skip to content

Commit 036fd30

Browse files
coreyfarrellBenjamin E. Coe
authored andcommitted
refactor: drop lodash some, castArray and omitBy (#42)
1 parent 707b456 commit 036fd30

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

index.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
const yargs = require('yargs/yargs');
44
const flatten = require('flat');
5-
const { castArray, some, isPlainObject, camelCase, kebabCase, omitBy } = require('lodash');
5+
const camelcase = require('camelcase');
6+
const decamelize = require('decamelize');
7+
const isPlainObj = require('is-plain-obj');
68

79
function isAlias(key, alias) {
8-
return some(alias, (aliases) => castArray(aliases).indexOf(key) !== -1);
10+
// TODO Switch to Object.values one Node.js 6 is dropped
11+
return Object.keys(alias).some((id) => [].concat(alias[id]).indexOf(key) !== -1);
912
}
1013

1114
function hasDefaultValue(key, value, defaults) {
1215
return value === defaults[key];
1316
}
1417

1518
function isCamelCased(key, argv) {
16-
return /[A-Z]/.test(key) && camelCase(key) === key && // Is it camel case?
17-
argv[kebabCase(key)] != null; // Is the standard version defined?
19+
return /[A-Z]/.test(key) && camelcase(key) === key && // Is it camel case?
20+
argv[decamelize(key, '-')] != null; // Is the standard version defined?
1821
}
1922

2023
function keyToFlag(key) {
@@ -30,7 +33,7 @@ function unparseOption(key, value, unparsed) {
3033
unparsed.push(`--no-${key}`);
3134
} else if (Array.isArray(value)) {
3235
value.forEach((item) => unparseOption(key, item, unparsed));
33-
} else if (isPlainObject(value)) {
36+
} else if (isPlainObj(value)) {
3437
const flattened = flatten(value, { safe: true });
3538

3639
for (const flattenedKey in flattened) {
@@ -78,20 +81,25 @@ function unparsePositional(argv, options, unparsed) {
7881
}
7982

8083
function unparseOptions(argv, options, knownPositional, unparsed) {
81-
const optionsArgv = omitBy(argv, (value, key) =>
82-
// Remove positional arguments
83-
knownPositional.includes(key) ||
84-
// Remove special _, -- and $0
85-
['_', '--', '$0'].includes(key) ||
86-
// Remove aliases
87-
isAlias(key, options.alias) ||
88-
// Remove default values
89-
hasDefaultValue(key, value, options.default) ||
90-
// Remove camel-cased
91-
isCamelCased(key, argv));
92-
93-
for (const key in optionsArgv) {
94-
unparseOption(key, optionsArgv[key], unparsed);
84+
for (const key of Object.keys(argv)) {
85+
const value = argv[key];
86+
87+
if (
88+
// Remove positional arguments
89+
knownPositional.includes(key) ||
90+
// Remove special _, -- and $0
91+
['_', '--', '$0'].includes(key) ||
92+
// Remove aliases
93+
isAlias(key, options.alias) ||
94+
// Remove default values
95+
hasDefaultValue(key, value, options.default) ||
96+
// Remove camel-cased
97+
isCamelCased(key, argv)
98+
) {
99+
continue;
100+
}
101+
102+
unparseOption(key, argv[key], unparsed);
95103
}
96104
}
97105

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@
5858
"yargs-parser": "^13.1.1"
5959
},
6060
"dependencies": {
61+
"camelcase": "^5.3.1",
62+
"decamelize": "^1.2.0",
6163
"flat": "^4.1.0",
62-
"lodash": "^4.17.15",
64+
"is-plain-obj": "^1.1.0",
6365
"yargs": "^13.3.0"
6466
}
6567
}

test/index.spec.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,15 @@ describe('options', () => {
130130
const alias = {
131131
string: 's', // Single alias
132132
number: ['n', 'no'], // Multiple aliases
133+
substr: 'substring',
133134
};
134-
const argv = parse(['--string', 'foo', '--number', '1'], { alias });
135+
const argv = parse(['--string', 'foo', '--number', '1', '--substr', 'a'], { alias });
135136

136-
expect(unparse(argv, { alias })).toEqual(['--string', 'foo', '--number', '1']);
137+
expect(unparse(argv, { alias })).toEqual([
138+
'--string', 'foo',
139+
'--number', '1',
140+
'--substr', 'a',
141+
]);
137142
});
138143

139144
it('should filter flags with default values via `options.defaults`', () => {

0 commit comments

Comments
 (0)