GUID.psm1

[CmdletBinding()]
param()
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($PSCommandPath)
$script:PSModuleInfo = Test-ModuleManifest -Path "$PSScriptRoot\$baseName.psd1"
$script:PSModuleInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug $_ }
$scriptName = $script:PSModuleInfo.Name
Write-Debug "[$scriptName] - Importing module"
#region [functions] - [public]
Write-Debug "[$scriptName] - [functions] - [public] - Processing folder"
#region [functions] - [public] - [Search-GUID]
Write-Debug "[$scriptName] - [functions] - [public] - [Search-GUID] - Importing"
filter Search-Guid {
    <#
        .SYNOPSIS
        Extracts a GUID from a given string.

        .DESCRIPTION
        This function searches for a GUID within the provided string and returns it.
        The function accepts input from the pipeline and processes each string to extract GUIDs.

        .EXAMPLE
        'The ID is 550e8400-e29b-41d4-a716-446655440000' | Search-Guid

        Extracts and returns the GUID `550e8400-e29b-41d4-a716-446655440000` from the input string.

        .EXAMPLE
        Search-Guid -String 'GUID: 123e4567-e89b-12d3-a456-426614174000'

        Returns the extracted GUID `123e4567-e89b-12d3-a456-426614174000`.

        .LINK
        https://psmodule.io/GUID/Functions/Search-Guid/
    #>

    [CmdletBinding()]
    [Alias('Find-Guid')]
    [OutputType([guid])]
    param(
        # The string containing a potential GUID.
        [Parameter(
            Mandatory,
            ValueFromPipeline,
            ValueFromPipelineByPropertyName
        )]
        [string] $String
    )

    foreach ($item in $String) {
        Write-Verbose "Looking for a GUID in $item"
        $foundGuids = [regex]::Matches($item, $script:GUIDPattern)

        # Output each GUID found.
        foreach ($guid in $foundGuids.Value) {
            Write-Verbose "Found GUID: [$guid]"
            Write-Output $guid
        }
    }
}
Write-Debug "[$scriptName] - [functions] - [public] - [Search-GUID] - Done"
#endregion [functions] - [public] - [Search-GUID]
#region [functions] - [public] - [Test-Guid]
Write-Debug "[$scriptName] - [functions] - [public] - [Test-Guid] - Importing"
filter Test-Guid {
    <#
        .SYNOPSIS
        Validates whether a given string is a GUID.

        .DESCRIPTION
        Checks if the input string matches the standard GUID format.
        The function returns `$true` if the string is a valid GUID, otherwise `$false`.

        .EXAMPLE
        "550e8400-e29b-41d4-a716-446655440000" | Test-Guid

        Returns `$true` since the provided string is a valid GUID.

        .EXAMPLE
        "not-a-guid" | IsGuid

        Returns `$false` because the input is not a valid GUID.

        .EXAMPLE
        Test-Guid -String "550e8400-e29b-41d4-a716-446655440000"

        Directly checks if the provided string is a valid GUID and returns `$true`.

        .LINK
        https://psmodule.io/GUID/Functions/Test-Guid/
    #>


    [Cmdletbinding()]
    [Alias('IsGuid', 'Test-IsGuid')]
    [OutputType([bool])]
    param (
        # The string to validate as a GUID.
        [Parameter(
            Mandatory,
            ValueFromPipeline,
            ValueFromPipelineByPropertyName
        )]
        [string] $String
    )

    # Check GUID against regex
    $String -match $script:GUIDPattern
}
Write-Debug "[$scriptName] - [functions] - [public] - [Test-Guid] - Done"
#endregion [functions] - [public] - [Test-Guid]
Write-Debug "[$scriptName] - [functions] - [public] - Done"
#endregion [functions] - [public]
#region [variables] - [private]
Write-Debug "[$scriptName] - [variables] - [private] - Processing folder"
#region [variables] - [private] - [GUIDPattern]
Write-Debug "[$scriptName] - [variables] - [private] - [GUIDPattern] - Importing"
[regex]$script:HexPattern = '[0-9a-fA-F]'
[regex]$script:GUIDPattern = "$script:HexPattern{8}-$script:HexPattern{4}-$script:HexPattern{4}-$script:HexPattern{4}-$script:HexPattern{12}"
Write-Debug "[$scriptName] - [variables] - [private] - [GUIDPattern] - Done"
#endregion [variables] - [private] - [GUIDPattern]
Write-Debug "[$scriptName] - [variables] - [private] - Done"
#endregion [variables] - [private]

#region Member exporter
$exports = @{
    Alias    = '*'
    Cmdlet   = ''
    Function = @(
        'Search-GUID'
        'Test-Guid'
    )
}
Export-ModuleMember @exports
#endregion Member exporter