Skip to content

Instantly share code, notes, and snippets.

@mavaddat
Last active April 18, 2023 02:30
Show Gist options
  • Save mavaddat/cfa0292815de83b2f2b53d0d313dfe61 to your computer and use it in GitHub Desktop.
Save mavaddat/cfa0292815de83b2f2b53d0d313dfe61 to your computer and use it in GitHub Desktop.
Calculate the acronym for a string
# Converts any text into acronym
function Get-Acronym
{
<#
.SYNOPSIS
Converts a string into its acronym form.
.DESCRIPTION
This function "acronymize" a string given in entry
.PARAMETER Text
String. Text to acronymize
.PARAMETER Periods
Switch. If the switch is set, it will add a period after each letter of the acronym.
.PARAMETER LowerCase
Switch. If the switch is set, it will return the lowercase first letter of each word in the "Text" string.
.EXAMPLE
PS> Get-Acronym "That's all folks!"
Will return "TAF"
.EXAMPLE
PS> Get-Acronym "That's all Folks!" -Periods
Will return "T.A.F."
.EXAMPLE
PS> Get-Acronym "That's all Folks!" -LowerCase -Periods
Will return "t.a.f."
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $True, ValueFromPipeline = $True, Position = 0)]
[String]
$Text,
[Parameter(Mandatory = $False, Position = 1)]
[Switch]
$Periods,
[Parameter()]
[Switch]
$LowerCase
)
$bytes = [System.Text.Encoding]::GetEncoding('Cyrillic').GetBytes($text)
if ($LowerCase)
{
$result = [System.Text.Encoding]::ASCII.GetString($bytes).ToLower()
} else {
$result = [System.Text.Encoding]::ASCII.GetString($bytes).ToUpper()
}
$rx = [System.Text.RegularExpressions.Regex]
$result = $rx::Replace($result, '[^a-zA-Z0-9\s-]', '')
if ($Periods)
{
$result = $rx::Replace($result, '\b(\w)\S+', "`$1.")
} else {
$result = $rx::Replace($result, '\b(\w)\S+', "`$1")
}
$result = $rx::Replace($result, '\s+', '').Trim()
Write-Output $result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment