@@ -2,14 +2,15 @@ var camelCase = require('camelcase')
2
2
var path = require ( 'path' )
3
3
var tokenizeArgString = require ( './lib/tokenize-arg-string' )
4
4
var util = require ( 'util' )
5
+ var assign = require ( 'object.assign' )
5
6
6
7
function parse ( args , opts ) {
7
- if ( ! opts ) opts = { }
8
+ if ( ! opts ) opts = Object . create ( null )
8
9
// allow a string argument to be passed in rather
9
10
// than an argv array.
10
11
args = tokenizeArgString ( args )
11
12
// aliases might have transitive relationships, normalize this.
12
- var aliases = combineAliases ( opts . alias || { } )
13
+ var aliases = combineAliases ( opts . alias || Object . create ( null ) )
13
14
var configuration = assign ( {
14
15
'short-option-groups' : true ,
15
16
'camel-case-expansion' : true ,
@@ -19,27 +20,27 @@ function parse (args, opts) {
19
20
'duplicate-arguments-array' : true ,
20
21
'flatten-duplicate-arrays' : true
21
22
} , opts . configuration )
22
- var defaults = opts . default || { }
23
+ var defaults = opts . default || Object . create ( null )
23
24
var configObjects = opts . configObjects || [ ]
24
25
var envPrefix = opts . envPrefix
25
- var newAliases = { }
26
+ var newAliases = Object . create ( null )
26
27
// allow a i18n handler to be passed in, default to a fake one (util.format).
27
28
var __ = opts . __ || function ( str ) {
28
29
return util . format . apply ( util , Array . prototype . slice . call ( arguments ) )
29
30
}
30
31
var error = null
31
32
var flags = {
32
- aliases : { } ,
33
- arrays : { } ,
34
- bools : { } ,
35
- strings : { } ,
36
- numbers : { } ,
37
- counts : { } ,
38
- normalize : { } ,
39
- configs : { } ,
40
- defaulted : { } ,
41
- nargs : { } ,
42
- coercions : { }
33
+ aliases : Object . create ( null ) ,
34
+ arrays : Object . create ( null ) ,
35
+ bools : Object . create ( null ) ,
36
+ strings : Object . create ( null ) ,
37
+ numbers : Object . create ( null ) ,
38
+ counts : Object . create ( null ) ,
39
+ normalize : Object . create ( null ) ,
40
+ configs : Object . create ( null ) ,
41
+ defaulted : Object . create ( null ) ,
42
+ nargs : Object . create ( null ) ,
43
+ coercions : Object . create ( null )
43
44
}
44
45
var negative = / ^ - [ 0 - 9 ] + ( \. [ 0 - 9 ] + ) ? /
45
46
@@ -67,11 +68,11 @@ function parse (args, opts) {
67
68
flags . normalize [ key ] = true
68
69
} )
69
70
70
- Object . keys ( opts . narg || { } ) . forEach ( function ( k ) {
71
+ Object . keys ( opts . narg || Object . create ( null ) ) . forEach ( function ( k ) {
71
72
flags . nargs [ k ] = opts . narg [ k ]
72
73
} )
73
74
74
- Object . keys ( opts . coerce || { } ) . forEach ( function ( k ) {
75
+ Object . keys ( opts . coerce || Object . create ( null ) ) . forEach ( function ( k ) {
75
76
flags . coercions [ k ] = opts . coerce [ k ]
76
77
} )
77
78
@@ -80,7 +81,7 @@ function parse (args, opts) {
80
81
flags . configs [ key ] = true
81
82
} )
82
83
} else {
83
- Object . keys ( opts . config || { } ) . forEach ( function ( k ) {
84
+ Object . keys ( opts . config || Object . create ( null ) ) . forEach ( function ( k ) {
84
85
flags . configs [ k ] = opts . config [ k ]
85
86
} )
86
87
}
@@ -417,7 +418,7 @@ function parse (args, opts) {
417
418
// set args from config.json file, this should be
418
419
// applied last so that defaults can be applied.
419
420
function setConfig ( argv ) {
420
- var configLookup = { }
421
+ var configLookup = Object . create ( null )
421
422
422
423
// expand defaults/aliases, in-case any happen to reference
423
424
// the config.json file.
@@ -537,7 +538,7 @@ function parse (args, opts) {
537
538
if ( ! configuration [ 'dot-notation' ] ) keys = [ keys . join ( '.' ) ]
538
539
539
540
keys . slice ( 0 , - 1 ) . forEach ( function ( key ) {
540
- o = ( o [ key ] || { } )
541
+ o = ( o [ key ] || Object . create ( null ) )
541
542
} )
542
543
543
544
var key = keys [ keys . length - 1 ]
@@ -551,8 +552,10 @@ function parse (args, opts) {
551
552
552
553
if ( ! configuration [ 'dot-notation' ] ) keys = [ keys . join ( '.' ) ]
553
554
555
+ keys = keys . map ( sanitizeKey )
556
+
554
557
keys . slice ( 0 , - 1 ) . forEach ( function ( key ) {
555
- if ( o [ key ] === undefined ) o [ key ] = { }
558
+ if ( o [ key ] === undefined ) o [ key ] = Object . create ( null )
556
559
o = o [ key ]
557
560
} )
558
561
@@ -584,7 +587,7 @@ function parse (args, opts) {
584
587
// extend the aliases list with inferred aliases.
585
588
function extendAliases ( ) {
586
589
Array . prototype . slice . call ( arguments ) . forEach ( function ( obj ) {
587
- Object . keys ( obj || { } ) . forEach ( function ( key ) {
590
+ Object . keys ( obj || Object . create ( null ) ) . forEach ( function ( key ) {
588
591
// short-circuit if we've already added a key
589
592
// to the aliases array, for example it might
590
593
// exist in both 'opts.default' and 'opts.key'.
@@ -681,7 +684,7 @@ function parse (args, opts) {
681
684
function combineAliases ( aliases ) {
682
685
var aliasArrays = [ ]
683
686
var change = true
684
- var combined = { }
687
+ var combined = Object . create ( null )
685
688
686
689
// turn alias lookup hash {key: ['alias1', 'alias2']} into
687
690
// a simple array ['key', 'alias1', 'alias2']
@@ -723,27 +726,18 @@ function combineAliases (aliases) {
723
726
return combined
724
727
}
725
728
726
- function assign ( defaults , configuration ) {
727
- var o = { }
728
- configuration = configuration || { }
729
-
730
- Object . keys ( defaults ) . forEach ( function ( k ) {
731
- o [ k ] = defaults [ k ]
732
- } )
733
- Object . keys ( configuration ) . forEach ( function ( k ) {
734
- o [ k ] = configuration [ k ]
735
- } )
736
-
737
- return o
738
- }
739
-
740
729
// this function should only be called when a count is given as an arg
741
730
// it is NOT called to set a default value
742
731
// thus we can start the count at 1 instead of 0
743
732
function increment ( orig ) {
744
733
return orig !== undefined ? orig + 1 : 1
745
734
}
746
735
736
+ function sanitizeKey ( key ) {
737
+ if ( key === '__proto__' ) return '___proto___'
738
+ return key
739
+ }
740
+
747
741
function Parser ( args , opts ) {
748
742
var result = parse ( args . slice ( ) , opts )
749
743
0 commit comments