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,38 +1,26 @@
#
function ConvertTo-Byte()
{
param(
[single]$Value,
[string]$Unit #Validation PT PetaByte
[single]$Value,
[string]$Unit
);
switch ($Unit) {
Byte {
$result = $Value;
break;
{ 'B', 'Byte' -contains $_ } { $result = $Value; $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 3)); $boolOption = $true; }
{ '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;
}
function ConvertTo-KiloByte()
@ -43,31 +31,17 @@ function ConvertTo-KiloByte()
);
switch ($Unit) {
Byte {
$result = ($Value / 1000);
break;
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = $Value; $boolOption = $true; }
{ '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;
@ -80,32 +54,18 @@ function ConvertTo-MegaByte()
[string]$Unit
);
switch ($Unit) {
Byte {
$result = ($Value / [math]::Pow(10, 6));
break;
switch($Unit) {
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 6)); $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
{ '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;
@ -118,32 +78,18 @@ function ConvertTo-GigaByte()
[string]$Unit
);
switch ($Unit) {
Byte {
$result = ($Value / [math]::Pow(10, 9));
break;
switch($Unit) {
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 9)); $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 6)); $boolOption = $true; }
{ '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;
@ -156,32 +102,18 @@ function ConvertTo-TeraByte()
[string]$Unit
);
switch ($Unit) {
Byte {
$result = ($Value / [math]::Pow(10, 12));
break;
switch($Unit) {
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 12)); $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 9)); $boolOption = $true; }
{ '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;
@ -194,32 +126,18 @@ function ConvertTo-PetaByte()
[string]$Unit
);
switch ($Unit) {
Byte {
$result = ($Value / [math]::Pow(10, 15));
break;
switch($Unit) {
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 15)); $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 12)); $boolOption = $true; }
{ '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;