Skip to content

Commit cff6390

Browse files
author
PsCustomObject
committed
Released New-StringConversion function
1 parent 47c4a61 commit cff6390

File tree

3 files changed

+637
-0
lines changed

3 files changed

+637
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*tempPoint*

New-StringConversion.TempPoint.ps1

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
function New-StringConversion
2+
{ #TODO: Implement ability to replace unknown characters
3+
<#
4+
.SYNOPSIS
5+
A brief description of the New-StringConversion function.
6+
7+
.DESCRIPTION
8+
A detailed description of the New-StringConversion function.
9+
10+
.PARAMETER StringToConvert
11+
A description of the StringToConvert parameter.
12+
13+
.PARAMETER UnicodeHashTable
14+
Specify a custom hash of characters to replace
15+
16+
.PARAMETER IgnoreSpaces
17+
A description of the IgnoreSpaces parameter.
18+
19+
.PARAMETER ReplaceSpace
20+
A description of the ReplaceSpace parameter.
21+
22+
.PARAMETER RemoveSpaces
23+
A description of the RemoveSpaces parameter.
24+
25+
.PARAMETER UnknownCharacter
26+
A description of the UnknownCharacter parameter.
27+
28+
.EXAMPLE
29+
PS C:\> New-StringConversion
30+
31+
.OUTPUTS
32+
string, string, string
33+
34+
.NOTES
35+
Additional information about the function.
36+
#>
37+
38+
[OutputType([string], ParameterSetName = 'IgnoreSpaces')]
39+
[OutputType([string], ParameterSetName = 'ReplaceSpaces')]
40+
[OutputType([string], ParameterSetName = 'RemoveSpaces')]
41+
[OutputType([string])]
42+
param
43+
(
44+
[Parameter(ParameterSetName = 'IgnoreSpaces')]
45+
[Parameter(ParameterSetName = 'RemoveSpaces')]
46+
[Parameter(ParameterSetName = 'ReplaceSpaces',
47+
Mandatory = $true)]
48+
[ValidateNotNullOrEmpty()]
49+
[string]
50+
$StringToConvert,
51+
[Parameter(ParameterSetName = 'IgnoreSpaces')]
52+
[Parameter(ParameterSetName = 'RemoveSpaces')]
53+
[Parameter(ParameterSetName = 'ReplaceSpaces')]
54+
[ValidateNotNullOrEmpty()]
55+
[object]
56+
$UnicodeHashTable = $unicodeHashTable,
57+
[Parameter(ParameterSetName = 'IgnoreSpaces',
58+
Mandatory = $true)]
59+
[switch]
60+
$IgnoreSpaces,
61+
[Parameter(ParameterSetName = 'ReplaceSpaces',
62+
Mandatory = $true)]
63+
[ValidateNotNullOrEmpty()]
64+
[string]
65+
$ReplaceSpace,
66+
[Parameter(ParameterSetName = 'RemoveSpaces',
67+
Mandatory = $true)]
68+
[switch]
69+
$RemoveSpaces,
70+
[Parameter(ParameterSetName = 'IgnoreSpaces')]
71+
[Parameter(ParameterSetName = 'RemoveSpaces')]
72+
[Parameter(ParameterSetName = 'ReplaceSpaces')]
73+
[string]
74+
$UnknownCharacter = '?'
75+
)
76+
77+
if ($UnicodeHashTable -eq $null)
78+
{
79+
# HashTable contaning special characters to replace
80+
[hashtable]$unicodeHashTable = @{
81+
82+
# a
83+
'æ' = 'a'
84+
'à' = 'a'
85+
'â' = 'a'
86+
'ã' = 'a'
87+
'å' = 'a'
88+
'ā' = 'a'
89+
'ă' = 'a'
90+
'ą' = 'a'
91+
'ä' = 'a'
92+
'á' = 'a'
93+
94+
# b
95+
'ƀ' = 'b'
96+
'ƃ' = 'b'
97+
98+
# Tone six
99+
'ƅ' = 'b'
100+
101+
# c
102+
'ç' = 'c'
103+
'ć' = 'c'
104+
'ĉ' = 'c'
105+
'ċ' = 'c'
106+
'č' = 'c'
107+
'ƈ' = 'c'
108+
109+
# d
110+
'ď' = 'd'
111+
'đ' = 'd'
112+
'ƌ' = 'd'
113+
114+
# e
115+
'è' = 'e'
116+
'é' = 'e'
117+
'ê' = 'e'
118+
'ë' = 'e'
119+
'ē' = 'e'
120+
'ĕ' = 'e'
121+
'ė' = 'e'
122+
'ę' = 'e'
123+
'ě' = 'e'
124+
125+
# g
126+
'ĝ' = 'e'
127+
'ğ' = 'e'
128+
'ġ' = 'e'
129+
'ģ' = 'e'
130+
131+
# h
132+
'ĥ' = 'h'
133+
'ħ' = 'h'
134+
135+
# i
136+
'ì' = 'i'
137+
'í' = 'i'
138+
'î' = 'i'
139+
'ï' = 'i'
140+
'ĩ' = 'i'
141+
'ī' = 'i'
142+
'ĭ' = 'i'
143+
'į' = 'i'
144+
'ı' = 'i'
145+
146+
# j
147+
'ij' = 'j'
148+
'ĵ' = 'j'
149+
150+
# k
151+
'ķ' = 'k'
152+
'ĸ' = 'k'
153+
154+
# l
155+
'ĺ' = 'l'
156+
'ļ' = 'l'
157+
'ľ' = 'l'
158+
'ŀ' = 'l'
159+
'ł' = 'l'
160+
161+
# n
162+
'ñ' = 'n'
163+
'ń' = 'n'
164+
'ņ' = 'n'
165+
'ň' = 'n'
166+
'ʼn' = 'n'
167+
'ŋ' = 'n'
168+
169+
# o
170+
'ð' = 'o'
171+
'ó' = 'o'
172+
'õ' = 'o'
173+
'ô' = 'o'
174+
'ö' = 'o'
175+
'ø' = 'o'
176+
'ō' = 'o'
177+
'ŏ' = 'o'
178+
'ő' = 'o'
179+
'œ' = 'o'
180+
181+
# r
182+
'ŕ' = 'r'
183+
'ŗ' = 'r'
184+
'ř' = 'r'
185+
186+
# s
187+
'ś' = 's'
188+
'ŝ' = 's'
189+
'ş' = 's'
190+
'š' = 's'
191+
'ß' = 'ss'
192+
'ſ' = 's'
193+
194+
# t
195+
'ţ' = 't'
196+
'ť' = 't'
197+
'ŧ' = 't'
198+
199+
# u
200+
'ù' = 'u'
201+
'ú' = 'u'
202+
'û' = 'u'
203+
'ü' = 'u'
204+
'ũ' = 'u'
205+
'ū' = 'u'
206+
'ŭ' = 'u'
207+
'ů' = 'u'
208+
'ű' = 'u'
209+
'ų' = 'u'
210+
211+
# w
212+
'ŵ' = 'w'
213+
214+
# y
215+
'ý' = 'y'
216+
'ÿ' = 'y'
217+
'ŷ' = 'y'
218+
219+
# z
220+
'ź' = 'z'
221+
'ż' = 'z'
222+
#'ž' = 'z'
223+
}
224+
}
225+
226+
# Check if we have a custom regex
227+
if ([string]::IsNullOrEmpty($RegExCharacters) -eq $true)
228+
{
229+
# Set a regex for additional special characters
230+
[string]$unicodeRegExString = "^([0-9a-zA-Z!#$@.'^_`~-])*$"
231+
232+
$RegExCharacters = $unicodeRegExString
233+
}
234+
235+
# Handle white spaces
236+
if ($IgnoreSpaces)
237+
{
238+
$UnicodeHashTable.Add(' ', ' ')
239+
}
240+
elseif ($ReplaceSpace)
241+
{
242+
$UnicodeHashTable.Add(' ', $ReplaceSpace)
243+
}
244+
elseif ($RemoveSpaces)
245+
{
246+
$StringToConvert = $StringToConvert.Replace(' ', '')
247+
}
248+
else
249+
{
250+
$UnicodeHashTable.Add(' ', '-')
251+
}
252+
253+
# Check if we have special characters
254+
if (!($StringToConvert -match $RegExCharacters))
255+
{
256+
$CharArray = $StringToConvert.ToCharArray()
257+
258+
# Declare new character array
259+
[array]$NewCharArray = @()
260+
261+
# Parse the string
262+
foreach ($Char in $CharArray)
263+
{
264+
[bool]$ToUpper = $False
265+
266+
# Set Char ref with current value
267+
$CharCurrent = $Char.ToString()
268+
$CharLower = $Char.ToString().ToLower()
269+
270+
# Run a compare to check character case
271+
[int]$CharComp = $CharCurrent.CompareTo($CharLower)
272+
273+
# Upper character for rendering
274+
if ($CharComp -eq 1)
275+
{
276+
$ToUpper = $True
277+
}
278+
279+
if (!($Char.ToString() -match $RegExCharacters))
280+
{
281+
if ($UnicodeHashTable.ContainsKey($Char.ToString().ToLower()) -eq $true)
282+
{
283+
$Char = $UnicodeHashTable["$($Char)"]
284+
if ($ToUpper)
285+
{
286+
$Char = $Char.ToString().ToUpper()
287+
}
288+
289+
$NewCharArray += $Char
290+
}
291+
else
292+
{
293+
$NewCharArray += '?'
294+
}
295+
}
296+
else
297+
{
298+
if ($ToUpper)
299+
{
300+
$Char = $Char.ToString().ToUpper()
301+
}
302+
303+
$NewCharArray += $Char
304+
}
305+
}
306+
$unicodeString = -join [char[]]$NewCharArray
307+
}
308+
else
309+
{
310+
$unicodeString = $StringToConvert
311+
}
312+
return $unicodeString
313+
}
314+
315+
New-StringConversion -StringToConvert "üö üöž" -UnknownCharacter l -RemoveSpaces

0 commit comments

Comments
 (0)