Private/ConvertTo-GhostMemberParameter.ps1

function ConvertTo-GhostMemberParameter {
    <#
    .SYNOPSIS
        Turns the parameters of a member function into the fields of a member.

    .DESCRIPTION
        Two fields of a member do not have the form of their parameter. Ghost takes the newsletters of a member
        as objects with an id, and the values of the custom fields as metafields.custom. This function returns a
        copy of the bound parameters in which Newsletters has that form and Metafields is added for -CustomField,
        with the keys in alphabetical order so that the body is the same on every run.

        The bound parameters themselves are left alone: what is added to them would still be there for the next
        object from the pipeline.

    .EXAMPLE
        ConvertTo-GhostMemberParameter -Parameters $PSBoundParameters

        Returns the parameters, with Newsletters as @{ id = ... } objects and Metafields for -CustomField.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        # The parameters of the calling function, usually $PSBoundParameters.
        [Parameter(Mandatory)]
        [System.Collections.IDictionary] $Parameters
    )

    $result = [hashtable]::new([System.StringComparer]::OrdinalIgnoreCase)
    foreach ($name in $Parameters.Keys) {
        $result[$name] = $Parameters[$name]
    }

    if ($result.ContainsKey('Newsletters')) {
        $result['Newsletters'] = @($result['Newsletters'] | ForEach-Object { @{ id = $_ } })
    }

    if ($result.ContainsKey('CustomField') -and $null -ne $result['CustomField']) {
        $custom = [ordered]@{}
        foreach ($key in $result['CustomField'].Keys | Sort-Object) {
            $custom[[string] $key] = $result['CustomField'][$key]
        }
        $result['Metafields'] = @{ custom = $custom }
    }

    $result
}