-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimize-String.ps1
More file actions
110 lines (91 loc) · 2.3 KB
/
Optimize-String.ps1
File metadata and controls
110 lines (91 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Function Optimize-String
{
Param
(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0
)]
[ValidateNotNullOrEmpty()]
[string]
$String,
[ValidateSet('Diacritics', 'AllSpaces', 'LeadingSpaces', 'TrailingSpaces', 'DoubleSpaces', 'LowerCase', 'UpperCase', 'Numbers', 'SpecialChars', 'NewLines')]
[string[]]
$Remove,
[ValidateSet('LowerToUpperCase', 'UpperToLowerCase', 'DashToHyphen', 'SmartQuotes')]
[string[]]
$Replace
)
Begin
{
}
Process
{
If ($Remove.Count -gt 0)
{
If ('Diacritics' -in $Remove)
{
$String = [Text.Encoding]::ASCII.GetString([Text.Encoding]::GetEncoding('Cyrillic').GetBytes($String))
}
If ('LowerCase' -in $Remove)
{
$String = $String -creplace '[\p{Ll}]'
}
If ('UpperCase' -in $Remove)
{
$String = $String -creplace '[\p{Lu}]'
}
If ('Numbers' -in $Remove)
{
$String = $String -creplace '[\p{N}]'
}
If ('SpecialChars' -in $Remove)
{
$String = $String -creplace '[^\p{L}\p{Nd}\p{Z}\r\n]'
}
If ('NewLines' -in $Remove)
{
$String = $String -creplace '[\r\n]'
}
If ('AllSpaces' -in $Remove)
{
$String = $String -replace ' '
}
If ('LeadingSpaces' -in $Remove)
{
$String = $String.TrimStart()
}
If ('TrailingSpaces' -in $Remove)
{
$String = $String.TrimEnd()
}
If ('DoubleSpaces' -in $Remove)
{
$String = $String -creplace '[\p{Zs}]+', ' '
}
}
If ($Replace.Count -gt 0)
{
If ('DashToHyphen' -in $Replace)
{
$String = $String -creplace '[\p{Pd}]', '-'
}
If ('LowerToUpperCase' -in $Replace)
{
$String = $String.ToUpper()
}
If ('UpperToLowerCase' -in $Replace)
{
$String = $String.ToLower()
}
If ('SmartQuotes' -in $Replace)
{
$String = $String -creplace '[\u2019\u2018]', ''''
$String = $String -creplace '[\u201C\u201D]', '"'
}
}
Write-Output $($String -as [string])
}
}