@@ -1980,13 +1980,30 @@ describe('yargs-parser', function () {
1980
1980
parsed . f . should . equal ( - 99 )
1981
1981
} )
1982
1982
1983
- it ( 'applies coercion function to an array' , function ( ) {
1983
+ it ( 'applies coercion function to an implicit array' , function ( ) {
1984
1984
var parsed = parser ( [ '--foo' , '99' , '-f' , '33' ] , {
1985
1985
coerce : {
1986
1986
f : function ( arg ) {
1987
1987
return arg * - 1
1988
1988
}
1989
1989
} ,
1990
+ alias : {
1991
+ f : [ 'foo' ]
1992
+ }
1993
+ } )
1994
+ parsed . f . should . deep . equal ( [ - 99 , - 33 ] )
1995
+ parsed . foo . should . deep . equal ( [ - 99 , - 33 ] )
1996
+ } )
1997
+
1998
+ it ( 'applies coercion function to an explicit array' , function ( ) {
1999
+ var parsed = parser ( [ '--foo' , '99' , '-f' , '33' ] , {
2000
+ coerce : {
2001
+ f : function ( arg ) {
2002
+ return arg . map ( function ( a ) {
2003
+ return a * - 1
2004
+ } )
2005
+ }
2006
+ } ,
1990
2007
array : [ 'foo' ] ,
1991
2008
alias : {
1992
2009
f : [ 'foo' ]
@@ -1996,6 +2013,19 @@ describe('yargs-parser', function () {
1996
2013
parsed . foo . should . deep . equal ( [ - 99 , - 33 ] )
1997
2014
} )
1998
2015
2016
+ it ( 'applies coercion function to _' , function ( ) {
2017
+ var parsed = parser ( [ '99' , '33' ] , {
2018
+ coerce : {
2019
+ _ : function ( arg ) {
2020
+ return arg . map ( function ( a ) {
2021
+ return a * - 1
2022
+ } )
2023
+ }
2024
+ }
2025
+ } )
2026
+ parsed . _ . should . deep . equal ( [ - 99 , - 33 ] )
2027
+ } )
2028
+
1999
2029
// see: https://github.com/yargs/yargs/issues/550
2000
2030
it ( 'coercion function can be used to parse large #s' , function ( ) {
2001
2031
var fancyNumberParser = function ( arg ) {
@@ -2014,7 +2044,7 @@ describe('yargs-parser', function () {
2014
2044
parsed . bar . should . equal ( 998 )
2015
2045
} )
2016
2046
2017
- it ( 'populates argv.error, if an error is returned ' , function ( ) {
2047
+ it ( 'populates argv.error, if an error is thrown ' , function ( ) {
2018
2048
var parsed = parser . detailed ( [ '--foo' , '99' ] , {
2019
2049
coerce : {
2020
2050
foo : function ( arg ) {
@@ -2024,6 +2054,18 @@ describe('yargs-parser', function () {
2024
2054
} )
2025
2055
parsed . error . message . should . equal ( 'banana' )
2026
2056
} )
2057
+
2058
+ it ( 'populates argv.error, if an error is thrown for an explicit array' , function ( ) {
2059
+ var parsed = parser . detailed ( [ '--foo' , '99' ] , {
2060
+ array : [ 'foo' ] ,
2061
+ coerce : {
2062
+ foo : function ( arg ) {
2063
+ throw Error ( 'foo is array: ' + Array . isArray ( arg ) )
2064
+ }
2065
+ }
2066
+ } )
2067
+ parsed . error . message . should . equal ( 'foo is array: true' )
2068
+ } )
2027
2069
} )
2028
2070
2029
2071
// see: https://github.com/yargs/yargs-parser/issues/37
0 commit comments