2
2
3
3
const yargs = require ( 'yargs/yargs' ) ;
4
4
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' ) ;
6
8
7
9
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 ) ;
9
12
}
10
13
11
14
function hasDefaultValue ( key , value , defaults ) {
12
15
return value === defaults [ key ] ;
13
16
}
14
17
15
18
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?
18
21
}
19
22
20
23
function keyToFlag ( key ) {
@@ -30,7 +33,7 @@ function unparseOption(key, value, unparsed) {
30
33
unparsed . push ( `--no-${ key } ` ) ;
31
34
} else if ( Array . isArray ( value ) ) {
32
35
value . forEach ( ( item ) => unparseOption ( key , item , unparsed ) ) ;
33
- } else if ( isPlainObject ( value ) ) {
36
+ } else if ( isPlainObj ( value ) ) {
34
37
const flattened = flatten ( value , { safe : true } ) ;
35
38
36
39
for ( const flattenedKey in flattened ) {
@@ -78,20 +81,25 @@ function unparsePositional(argv, options, unparsed) {
78
81
}
79
82
80
83
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 ) ;
95
103
}
96
104
}
97
105
0 commit comments