@@ -1980,13 +1980,30 @@ describe('yargs-parser', function () {
19801980 parsed . f . should . equal ( - 99 )
19811981 } )
19821982
1983- it ( 'applies coercion function to an array' , function ( ) {
1983+ it ( 'applies coercion function to an implicit array' , function ( ) {
19841984 var parsed = parser ( [ '--foo' , '99' , '-f' , '33' ] , {
19851985 coerce : {
19861986 f : function ( arg ) {
19871987 return arg * - 1
19881988 }
19891989 } ,
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+ } ,
19902007 array : [ 'foo' ] ,
19912008 alias : {
19922009 f : [ 'foo' ]
@@ -1996,6 +2013,19 @@ describe('yargs-parser', function () {
19962013 parsed . foo . should . deep . equal ( [ - 99 , - 33 ] )
19972014 } )
19982015
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+
19992029 // see: https://github.com/yargs/yargs/issues/550
20002030 it ( 'coercion function can be used to parse large #s' , function ( ) {
20012031 var fancyNumberParser = function ( arg ) {
@@ -2014,7 +2044,7 @@ describe('yargs-parser', function () {
20142044 parsed . bar . should . equal ( 998 )
20152045 } )
20162046
2017- it ( 'populates argv.error, if an error is returned ' , function ( ) {
2047+ it ( 'populates argv.error, if an error is thrown ' , function ( ) {
20182048 var parsed = parser . detailed ( [ '--foo' , '99' ] , {
20192049 coerce : {
20202050 foo : function ( arg ) {
@@ -2024,6 +2054,18 @@ describe('yargs-parser', function () {
20242054 } )
20252055 parsed . error . message . should . equal ( 'banana' )
20262056 } )
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+ } )
20272069 } )
20282070
20292071 // see: https://github.com/yargs/yargs-parser/issues/37
0 commit comments