Fix Code-Styling and Validation for Strings

This commit is contained in:
Niko Martini 2019-07-22 16:22:44 +02:00
parent fe3febe660
commit 3be84379d9

View file

@ -1,36 +1,24 @@
#
function ConvertTo-Byte() function ConvertTo-Byte()
{ {
param( param(
[single]$Value, [single]$Value,
[string]$Unit #Validation PT PetaByte [string]$Unit
); );
switch ($Unit) { switch ($Unit) {
Byte { { 'B', 'Byte' -contains $_ } { $result = $Value; $boolOption = $true; }
$result = $Value; { 'KB', 'KiloByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 3)); $boolOption = $true; }
break; { 'MB', 'MegaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 6)); $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 9)); $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 12)); $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 15)); $boolOption = $true; }
default {
if (-Not $boolOption) {
Throw 'Invalid input';
} }
KiloByte {
$result = ($Value * [math]::Pow(10, 3));
break;
} }
MegaByte {
$result = ($Value * [math]::Pow(10, 6));
break;
}
GigaByte {
$result = ($Value * [math]::Pow(10, 9));
break;
}
TeraByte {
$result = ($Value * [math]::Pow(10, 12));
break;
}
PetaByte {
$result = ($Value * [math]::Pow(10, 15));
break;
}
Default {}
} }
return $result; return $result;
@ -43,31 +31,17 @@ function ConvertTo-KiloByte()
); );
switch ($Unit) { switch ($Unit) {
Byte { { 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
$result = ($Value / 1000); { 'KB', 'KiloByte' -contains $_ } { $result = $Value; $boolOption = $true; }
break; { 'MB', 'MegaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 3)); $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 6)); $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 9)); $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 12)); $boolOption = $true; }
default {
if (-Not $boolOption) {
Throw 'Invalid input';
} }
KiloByte {
$result = $Value;
break;
} }
MegaByte {
$result = ($Value * [math]::Pow(10, 3));
break;
}
GigaByte {
$result = ($Value * [math]::Pow(10, 6));
break;
}
TeraByte {
$result = ($Value * [math]::Pow(10, 9));
break;
}
PetaByte {
$result = ($Value * [math]::Pow(10, 12));
break;
}
Default {}
} }
return $result; return $result;
@ -81,31 +55,17 @@ function ConvertTo-MegaByte()
); );
switch($Unit) { switch($Unit) {
Byte { { 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 6)); $boolOption = $true; }
$result = ($Value / [math]::Pow(10, 6)); { 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
break; { 'MB', 'MegaByte' -contains $_ } { $result = $Value; $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 3)); $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 6)); $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 9)); $boolOption = $true; }
default {
if (-Not $boolOption) {
Throw 'Invalid input';
} }
KiloByte {
$result = ($Value / [math]::Pow(10, 3));
break;
} }
MegaByte {
$result = $Value;
break;
}
GigaByte {
$result = ($Value * [math]::Pow(10, 3));
break;
}
TeraByte {
$result = ($Value * [math]::Pow(10, 6));
break;
}
PetaByte {
$result = ($Value * [math]::Pow(10, 9));
break;
}
Default {}
} }
return $result; return $result;
@ -119,31 +79,17 @@ function ConvertTo-GigaByte()
); );
switch($Unit) { switch($Unit) {
Byte { { 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 9)); $boolOption = $true; }
$result = ($Value / [math]::Pow(10, 9)); { 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 6)); $boolOption = $true; }
break; { 'MB', 'MegaByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = $Value; $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 3)); $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 6)); $boolOption = $true; }
default {
if (-Not $boolOption) {
Throw 'Invalid input';
} }
KiloByte {
$result = ($Value / [math]::Pow(10, 6));
break;
} }
MegaByte {
$result = ($Value / [math]::Pow(10, 3));
break;
}
GigaByte {
$result = $Value;
break;
}
TeraByte {
$result = ($Value * [math]::Pow(10, 3));
break;
}
PetaByte {
$result = ($Value * [math]::Pow(10, 6));
break;
}
Default {}
} }
return $result; return $result;
@ -157,31 +103,17 @@ function ConvertTo-TeraByte()
); );
switch($Unit) { switch($Unit) {
Byte { { 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 12)); $boolOption = $true; }
$result = ($Value / [math]::Pow(10, 12)); { 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 9)); $boolOption = $true; }
break; { 'MB', 'MegaByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 6)); $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = $Value; $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 3)); $boolOption = $true; }
default {
if (-Not $boolOption) {
Throw 'Invalid input';
} }
KiloByte {
$result = ($Value / [math]::Pow(10, 9));
break;
} }
MegaByte {
$result = ($Value / [math]::Pow(10, 6));
break;
}
GigaByte {
$result = ($Value / [math]::Pow(10, 3));
break;
}
TeraByte {
$result = $Value;
break;
}
PetaByte {
$result = ($Value * [math]::Pow(10, 3));
break;
}
Default {}
} }
return $result; return $result;
@ -195,31 +127,17 @@ function ConvertTo-PetaByte()
); );
switch($Unit) { switch($Unit) {
Byte { { 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 15)); $boolOption = $true; }
$result = ($Value / [math]::Pow(10, 15)); { 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 12)); $boolOption = $true; }
break; { 'MB', 'MegaByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 9)); $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 6)); $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = $Value; $boolOption = $true; }
default {
if (-Not $boolOption) {
Throw 'Invalid input';
} }
KiloByte {
$result = ($Value / [math]::Pow(10, 12));
break;
} }
MegaByte {
$result = ($Value / [math]::Pow(10, 9));
break;
}
GigaByte {
$result = ($Value / [math]::Pow(10, 6));
break;
}
TeraByte {
$result = ($Value / [math]::Pow(10, 3));
break;
}
PetaByte {
$result = $Value;
break;
}
Default {}
} }
return $result; return $result;