Last active
August 29, 2015 14:15
-
-
Save XPlantefeve/adf352d758ccf276959d to your computer and use it in GitHub Desktop.
Rewriting of ConvertFrom-Csv, as an exercise. Quickly done and unfinished.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function ConvertFrom-Csave { | |
[CmdletBinding(DefaultParameterSetName='UseDelimiter')] | |
Param ( | |
[Parameter(Position=2,ParameterSetName='UseDelimiter')] | |
[char]$Delimiter = ',', | |
# FIXME | |
[string[]] $Header, | |
[Parameter(Mandatory=$True,Position=1,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] | |
[PSObject] $InputObject, | |
[Parameter(Mandatory=$True,Position=2,ParameterSetName='UseCulture')] | |
[switch] $UseCulture | |
) | |
Begin { | |
$OutputObject = New-Object PSCustomObject | |
$HeadersDone = $false | |
$Properties = @() | |
If ( $UseCulture ) { $Delimiter = (Get-Culture).TextInfo.ListSeparator } | |
} | |
Process { | |
If ( $InputObject -like '#TYPE *') { | |
$OutputObject.pstypenames.insert(0,'CSV:' + $InputObject.ToString().Replace('#TYPE ','')) | |
} ElseIf ( !$HeadersDone ) { | |
foreach ( $Property in $InputObject.Split($Delimiter) ) { | |
$Properties += $property -Replace('^"(.*)"$', '$1') | |
Add-Member -InputObject $OutputObject -MemberType NoteProperty -Name ($property -Replace('^"(.*)"$', '$1')) -Value "" | |
} | |
$HeadersDone = $True | |
} else { | |
$i = 0 | |
foreach ( $Property in $InputObject.Split($Delimiter) ) { | |
$OutputObject."$($Properties[$i])" = $property -Replace('^"(.*)"$', '$1') | |
$i++ | |
} | |
$OutputObject | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment