Last active
February 7, 2023 01:33
-
-
Save shinayser/2fb924009e4d3958d65f2a8729c0481d to your computer and use it in GitHub Desktop.
Functions to convert text into Pascal or Snake case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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