Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a pipe-able function that can reduce results

Tags:

powershell

In my script I'm joining-by-comma a lot and would like to create a helper function that I can pipe so I could do

$fileNames | %{ "../$_.js" } | Join-ByComma

rather than having to do

($fileNames | %{ "../$_.js" }) -join ', '

I'm having trouble figuring out how to do this in a way that works with pipeline input. I've tried something like this

function Join-ByComma($arr) { 
    $arr -join ', '
}

and

function Join-ByComma($arr) { 
    Process { $_ }
    End { $arr -join ', ' }
}

and neither works

like image 279
George Mauer Avatar asked Sep 29 '22 06:09

George Mauer


1 Answers

You can use $Input auto-variable, that represent pipeline input:

function Join-ByComma {
    @($Input) -join ', '
}
like image 53
user4003407 Avatar answered Oct 07 '22 13:10

user4003407