Skip to content

Instantly share code, notes, and snippets.

@shinayser
Last active February 7, 2023 01:33
Show Gist options
  • Save shinayser/2fb924009e4d3958d65f2a8729c0481d to your computer and use it in GitHub Desktop.
Save shinayser/2fb924009e4d3958d65f2a8729c0481d to your computer and use it in GitHub Desktop.
Functions to convert text into Pascal or Snake case
function toPascalCase([string] $text) {
return $text -replace '(?:^|_)(\p{L})', { $_.Groups[1].Value.ToUpper() }
}
function toSnakeCase([string] $text) {
$text = $text -replace '\s+', '_' # Replace spaces with underscores
$text = $text -replace '[-\.]', '_' # Replace dashes and dots with underscores
$text = $text -creplace '(?<![-_A-Z])(?<!^)[A-Z]', { "_$_" } # Insert underscores before uppercase letters
return $text.ToLower()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment