Languages/JavaScript/Templates/JavaScript-Template-Class.ps1


function Template.Class.js {

    <#
    .SYNOPSIS
        Template for a JavaScript `class`
    .DESCRIPTION
        Template for a `class` in JavaScript.
    .EXAMPLE
        Template.Class.js -Name "MyClass" -Body "MyMethod() { return 'hello'}"
    #>

    param(    
    # The name of the function.
    [Parameter(ValueFromPipelineByPropertyName)]
    [string]
    $Name,
    
    # The body of the function.
    [Parameter(ValueFromPipelineByPropertyName)]
    [Alias('Member','Members')]
    [string[]]
    $Body,

    # If provided, will extend from a base class.
    [Alias('Extends')]
    [string]
    $Extend
    )

    process {
        if ($body -match '^\{') {
            $body = $body -replace '^\s{0,}\{' -replace '\}\s{0,}$'
        }
        
@"
class $name$(if ($Extend) { " extends $extend"}) {
    $($Body -join (';' + [Environment]::newLine + ' '))
}
"@
        
    }

}