-
Notifications
You must be signed in to change notification settings - Fork 3
/
New-StringConversion.ps1
338 lines (291 loc) · 10.1 KB
/
New-StringConversion.ps1
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
function New-StringConversion
{
<#
.SYNOPSIS
Function to remove any non-unicode character from a string.
.DESCRIPTION
Function is used to sanitize non-unicode characters from a string.
Function supports custom characters map via the -UnicodeHashTable parameter accepting an hashtable of characters to replace.
If characters not specified in the default character map are found they are replace with a question mark '?' unless a custom
unknown character is specified via -UnknownCharacter parameter.
.PARAMETER StringToConvert
A string containing characters that need to be sanitized/converted.
.PARAMETER UnicodeHashTable
An hashtable containing characters that should be replaced if parameter is not specified default values will be used.
.PARAMETER IgnoreSpaces
By default spaces will be replaced with a dash '-' sign if paramter is specified function will not convert/take into consideraiotn
spaces in the string.
.PARAMETER RemoveSpaces
If parameter is specified spaces will be removed from input string.
.PARAMETER ReplaceSpaces
By default spaces will be replaced with a dash '-' sign if parameter is specified it is possible to specify character to use when
a space is encountered in the string.
.PARAMETER UnknownCharacter
By default any special character not found in the UnicodeHashTable will be replaced with a question mark when parameter is used
it is possible to specify which character will be used for unknown entries.
.EXAMPLE
PS C:\> New-StringConversion
.NOTES
Additional information about the function.
#>
[CmdletBinding(DefaultParameterSetName = 'ReplaceSpaces',
ConfirmImpact = 'High',
HelpUri = 'https://PsCustomObject.github.io')]
param
(
[Parameter(ParameterSetName = 'IgnoreSpaces',
Mandatory = $true)]
[Parameter(ParameterSetName = 'RemoveSpaces')]
[Parameter(ParameterSetName = 'ReplaceSpaces')]
[ValidateNotNullOrEmpty()]
[string]
$StringToConvert,
[Parameter(ParameterSetName = 'IgnoreSpaces')]
[Parameter(ParameterSetName = 'RemoveSpaces')]
[Parameter(ParameterSetName = 'ReplaceSpaces')]
[hashtable]
$UnicodeHashTable,
[Parameter(ParameterSetName = 'IgnoreSpaces')]
[switch]
$IgnoreSpaces,
[Parameter(ParameterSetName = 'RemoveSpaces')]
[switch]
$RemoveSpaces,
[Parameter(ParameterSetName = 'ReplaceSpaces')]
[ValidateNotNullOrEmpty()]
[string]
$ReplaceSpaces,
[Parameter(ParameterSetName = 'IgnoreSpaces')]
[Parameter(ParameterSetName = 'RemoveSpaces')]
[Parameter(ParameterSetName = 'ReplaceSpaces')]
[ValidateNotNullOrEmpty()]
[string]
$UnknownCharacter = '?'
)
begin
{
# Declare control variable
[bool]$isUpperCase = $false
# Check if we should use custom array hash
if (-not ($PSBoundParameters.ContainsKey('UnicodeHashTable')))
{
# Hashtable contaning special characters to replace
[hashtable]$unicodeHashTable = @{
# a
'æ' = 'a'
'à' = 'a'
'â' = 'a'
'ã' = 'a'
'å' = 'a'
'ā' = 'a'
'ă' = 'a'
'ą' = 'a'
'ä' = 'a'
'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= 'z'
}
}
switch ($PSBoundParameters.Keys)
{
'IgnoreSpaces'
{
$UnicodeHashTable.Add(' ', ' ')
break
}
'ReplaceSpaces'
{
# Replace spaces with specified character
$UnicodeHashTable.Add(' ', $ReplaceSpaces)
break
}
'RemoveSpaces'
{
# Replace spaces with specified character
$UnicodeHashTable.Add(' ', '')
break
}
}
# Create new chararray
[System.Collections.ArrayList]$resultStringArray = @()
# Set a regex for additional special characters
[string]$unicodeRegExString = "^([0-9a-zA-Z!#$@.'^_`~-])*$"
}
process
{
# Convert string to array
[array]$stringCharArray = $StringToConvert.ToCharArray()
foreach ($character in $stringCharArray)
{
# Reset control variables
$isUpperCase = $false
# Set Char ref with current value
[string]$currentChar = $character.ToString()
[string]$currentCharLower = $character.ToString().ToLower()
# Get character case
if ($currentChar.CompareTo($currentCharLower) -eq 1)
{
$isUpperCase = $true
}
# Check if character should be translated
if ($UnicodeHashTable.ContainsKey($currentCharLower) -eq $true)
{
# Get unicode equivalent
[string]$tmpChar = $UnicodeHashTable[$currentChar]
# Set character case
switch ($isUpperCase)
{
$true
{
$resultStringArray.Add($tmpChar.ToUpper())
break
}
default
{
$resultStringArray.Add($tmpChar)
}
}
}
else
{
# Check if character should be translated
if (($currentCharLower -match $unicodeRegExString) -eq $false)
{
# Handle characters not in hash
$currentChar = $UnknownCharacter
# Append to result array
$resultStringArray.Add($currentChar).ToString()
}
else
{
# Set character case
switch ($isUpperCase)
{
$true
{
$resultStringArray.Add($currentChar).ToString().ToUpper()
}
default
{
$resultStringArray.Add($currentChar).ToString()
}
}
}
}
}
}
end
{
# Return string
return (-join $resultStringArray)
}
}