diff --git a/examples/General.ps1 b/examples/General.ps1 deleted file mode 100644 index e193423..0000000 --- a/examples/General.ps1 +++ /dev/null @@ -1,19 +0,0 @@ -<# - .SYNOPSIS - This is a general example of how to use the module. -#> - -# Import the module -Import-Module -Name 'PSModule' - -# Define the path to the font file -$FontFilePath = 'C:\Fonts\CodeNewRoman\CodeNewRomanNerdFontPropo-Regular.tff' - -# Install the font -Install-Font -Path $FontFilePath -Verbose - -# List installed fonts -Get-Font -Name 'CodeNewRomanNerdFontPropo-Regular' - -# Uninstall the font -Get-Font -Name 'CodeNewRomanNerdFontPropo-Regular' | Uninstall-Font -Verbose diff --git a/src/functions/public/ConvertFrom-Base64.ps1 b/src/functions/public/ConvertFrom-Base64.ps1 new file mode 100644 index 0000000..fa00cfc --- /dev/null +++ b/src/functions/public/ConvertFrom-Base64.ps1 @@ -0,0 +1,35 @@ +filter ConvertFrom-Base64 { + <# + .SYNOPSIS + Convert to string from base64 + + .DESCRIPTION + Converts a base64 encoded string to a string. + + .EXAMPLE + ConvertFrom-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' + ThisIsANiceString + + Converts the base64 encoded string 'VGhpc0lzQU5pY2VTdHJpbmc=' to a string. + + .EXAMPLE + 'SGVsbG8gV29ybGQ=' | ConvertFrom-Base64String + Hello World + + Converts the string from base64 to a regular string. + #> + [Alias('ConvertFrom-Base64String')] + [OutputType([string])] + [CmdletBinding()] + param( + # The base64 encoded string to convert. + [Parameter( + Mandatory, + ValueFromPipeline + )] + [ValidateScript({ Test-Base64 -Base64String $_ }, ErrorMessage = 'Invalid Base64 string')] + [string] $Base64String + ) + + return [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($Base64String)) +} diff --git a/src/functions/public/ConvertTo-Base64.ps1 b/src/functions/public/ConvertTo-Base64.ps1 new file mode 100644 index 0000000..05a1fd6 --- /dev/null +++ b/src/functions/public/ConvertTo-Base64.ps1 @@ -0,0 +1,34 @@ +filter ConvertTo-Base64 { + <# + .SYNOPSIS + Convert a string to base64 + + .DESCRIPTION + Converts a string to a base64 encoded string. + + .EXAMPLE + ConvertTo-Base64 -String 'ThisIsANiceString' + VGhpc0lzQU5pY2VTdHJpbmc= + + Converts the string 'ThisIsANiceString' to a base64 encoded string. + + .EXAMPLE + 'Hello World' | ConvertTo-Base64 + SGVsbG8gV29ybGQ= + + Converts the string 'Hello World' to base64. + #> + [Alias('ConvertTo-Base64String')] + [OutputType([string])] + [CmdletBinding()] + param( + # The string to convert to base64 + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [string] $String + ) + return [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($String)) +} diff --git a/src/functions/public/Get-PSModuleTest.ps1 b/src/functions/public/Get-PSModuleTest.ps1 deleted file mode 100644 index 0e9aacf..0000000 --- a/src/functions/public/Get-PSModuleTest.ps1 +++ /dev/null @@ -1,20 +0,0 @@ -#Requires -Modules Utilities - -function Get-PSModuleTest { - <# - .SYNOPSIS - Performs tests on a module. - - .EXAMPLE - Test-PSModule -Name 'World' - - "Hello, World!" - #> - [CmdletBinding()] - param ( - # Name of the person to greet. - [Parameter(Mandatory)] - [string] $Name - ) - Write-Output "Hello, $Name!" -} diff --git a/src/functions/public/Test-Base64.ps1 b/src/functions/public/Test-Base64.ps1 new file mode 100644 index 0000000..ade81cd --- /dev/null +++ b/src/functions/public/Test-Base64.ps1 @@ -0,0 +1,37 @@ +filter Test-Base64 { + <# + .SYNOPSIS + Test if a string is a valid base64 string. + + .DESCRIPTION + Test if a string is a valid base64 string. + + .EXAMPLE + Test-Base64 -Base64String 'U29tZSBkYXRh' + True + + Returns $true as the string is a valid base64 string. + + .EXAMPLE + 'U29tZSBkYXRh' | Test-Base64 + True + + Returns $true as the string is a valid base64 string. + #> + [OutputType([bool])] + [CmdletBinding()] + param ( + [Parameter( + Mandatory, + ValueFromPipeline + )] + [string] $Base64String + ) + + try { + $null = [Convert]::FromBase64String($Base64String) + return $true + } catch { + return $false + } +} diff --git a/tests/Module.Tests.ps1 b/tests/Module.Tests.ps1 index d329e70..457967d 100644 --- a/tests/Module.Tests.ps1 +++ b/tests/Module.Tests.ps1 @@ -1,14 +1,28 @@ -Describe 'Module' { - It 'Function: Get-PSModuleTest' { - Get-PSModuleTest -Name 'World' | Should -Be 'Hello, World!' +Describe 'Base64' { + Context 'Function: Test-Base64' { + It "Test-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' -> true" { + Test-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' | Should -Be $true + } + It "'SGVsbG8gV29ybGQ=' | Test-Base64 -> true" { + 'SGVsbG8gV29ybGQ=' | Test-Base64 | Should -Be $true + } } - It 'Function: New-PSModuleTest' { - New-PSModuleTest -Name 'World' | Should -Be 'Hello, World!' + Context 'Function: ConvertTo-Base64' { + It "ConvertTo-Base64 -String 'ThisIsANiceString' -> VGhpc0lzQU5pY2VTdHJpbmc=" { + ConvertTo-Base64 -String 'ThisIsANiceString' | Should -Be 'VGhpc0lzQU5pY2VTdHJpbmc=' + } + + It "'Hello World' | ConvertTo-Base64 -> SGVsbG8gV29ybGQ=" { + 'Hello World' | ConvertTo-Base64 | Should -Be 'SGVsbG8gV29ybGQ=' + } } - It 'Function: Set-PSModuleTest' { - Set-PSModuleTest -Name 'World' | Should -Be 'Hello, World!' - } - It 'Function: Test-PSModuleTest' { - Test-PSModuleTest -Name 'World' | Should -Be 'Hello, World!' + Context 'Function: ConvertFrom-Base64' { + It "ConvertFrom-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' -> ThisIsANiceString" { + ConvertFrom-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' | Should -Be 'ThisIsANiceString' + } + + It "'SGVsbG8gV29ybGQ=' | ConvertFrom-Base64 -> Hello World" { + 'SGVsbG8gV29ybGQ=' | ConvertFrom-Base64 | Should -Be 'Hello World' + } } }