From fdc31beac5fdba692abaf1d69eccbee17073f4de Mon Sep 17 00:00:00 2001 From: Niko Martini Date: Wed, 17 Jul 2019 09:42:35 +0200 Subject: [PATCH 1/4] Toolset for conversions --- lib/core/tools/ConvertTo-ByteUnit.psm1 | 190 +++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 lib/core/tools/ConvertTo-ByteUnit.psm1 diff --git a/lib/core/tools/ConvertTo-ByteUnit.psm1 b/lib/core/tools/ConvertTo-ByteUnit.psm1 new file mode 100644 index 0000000..e2db959 --- /dev/null +++ b/lib/core/tools/ConvertTo-ByteUnit.psm1 @@ -0,0 +1,190 @@ +function ConvertTo-Byte() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + B { + $result = $Value; + } + KB { + $result = ($Value * [math]::Pow(10, 3)); + } + MB { + $result = ($Value * [math]::Pow(10, 6)); + } + GB { + $result = ($Value * [math]::Pow(10, 9)); + } + TB { + $result = ($Value * [math]::Pow(10, 12)); + } + PB { + $result = ($Value * [math]::Pow(10, 15)); + } + Default {} + } + + return $result; +} +function ConvertTo-KiloByte() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + B { + $result = ($Value / 1000); + } + KB { + Write-Output $Value; + } + MB { + $result = ($Value * [math]::Pow(10, 3)); + } + GB { + $result = ($Value * [math]::Pow(10, 6)); + } + TB { + $result = ($Value * [math]::Pow(10, 9)); + } + PB { + $result = ($Value * [math]::Pow(10, 12)); + } + Default {} + } + + return $result; +} + +function ConvertTo-MegaByte() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + B { + $result = ($Value / [math]::Pow(10, 6)); + } + KB { + $result = ($Value / [math]::Pow(10, 3)); + } + MB { + $result = $Value; + } + GB { + $result = ($Value * [math]::Pow(10, 3)); + } + TB { + $result = ($Value * [math]::Pow(10, 6)); + } + PB { + $result = ($Value * [math]::Pow(10, 9)); + } + Default {} + } + + return $result; +} + +function ConvertTo-GigaByte() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + B { + $result = ($Value / [math]::Pow(10, 9)); + } + KB { + $result = ($Value / [math]::Pow(10, 6)) + } + MB { + $result = ($Value / [math]::Pow(10, 3)); + } + GB { + $result = $Value; + } + TB { + $result = ($Value * [math]::Pow(10, 3)); + } + PB { + $result = ($Value * [math]::Pow(10, 6)); + } + Default {} + } + + return $result; +} + +function ConvertTo-TeraByte() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + B { + $result = ($Value / [math]::Pow(10, 12)); + } + KB { + $result = ($Value / [math]::Pow(10, 9)); + } + MB { + $result = ($Value / [math]::Pow(10, 6)); + } + GB { + $result = ($Value / [math]::Pow(10, 3)); + } + TB { + $result = $Value; + } + PB { + $result = ($Value * [math]::Pow(10, 3)); + } + Default {} + } + + return $result; +} + +function ConvertTo-PetaByte() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + B { + $result = ($Value / [math]::Pow(10, 15)); + } + KB { + $result = ($Value / [math]::Pow(10, 12)); + } + MB { + $result = ($Value / [math]::Pow(10, 9)); + } + GB { + $result = ($Value / [math]::Pow(10, 6)); + } + TB { + $result = ($Value / [math]::Pow(10, 3)); + } + PB { + $result = $Value; + } + Default {} + } + + return $result; +} \ No newline at end of file From 64ba37e9c658991cb01e10ea0f9ab013357a338c Mon Sep 17 00:00:00 2001 From: Niko Martini Date: Wed, 17 Jul 2019 10:03:58 +0200 Subject: [PATCH 2/4] fix consistency error in ConvertTo-ByteUnit.psm1 --- lib/core/tools/ConvertTo-ByteUnit.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/tools/ConvertTo-ByteUnit.psm1 b/lib/core/tools/ConvertTo-ByteUnit.psm1 index e2db959..6aafb44 100644 --- a/lib/core/tools/ConvertTo-ByteUnit.psm1 +++ b/lib/core/tools/ConvertTo-ByteUnit.psm1 @@ -41,7 +41,7 @@ function ConvertTo-KiloByte() $result = ($Value / 1000); } KB { - Write-Output $Value; + $result = $Value; } MB { $result = ($Value * [math]::Pow(10, 3)); From fe3febe6605d3f2f7c78373fc3144642a05fe71f Mon Sep 17 00:00:00 2001 From: Niko Martini Date: Wed, 17 Jul 2019 16:37:38 +0200 Subject: [PATCH 3/4] New Break instances for switchCase --- lib/core/tools/ConvertTo-ByteUnit.psm1 | 122 ++++++++++++++++--------- 1 file changed, 79 insertions(+), 43 deletions(-) diff --git a/lib/core/tools/ConvertTo-ByteUnit.psm1 b/lib/core/tools/ConvertTo-ByteUnit.psm1 index 6aafb44..09c29f7 100644 --- a/lib/core/tools/ConvertTo-ByteUnit.psm1 +++ b/lib/core/tools/ConvertTo-ByteUnit.psm1 @@ -1,28 +1,34 @@ function ConvertTo-Byte() { param( - [single]$Value, - [string]$Unit + [single]$Value, + [string]$Unit #Validation PT PetaByte ); switch ($Unit) { - B { - $result = $Value; + Byte { + $result = $Value; + break; } - KB { + KiloByte { $result = ($Value * [math]::Pow(10, 3)); + break; } - MB { + MegaByte { $result = ($Value * [math]::Pow(10, 6)); + break; } - GB { + GigaByte { $result = ($Value * [math]::Pow(10, 9)); + break; } - TB { + TeraByte { $result = ($Value * [math]::Pow(10, 12)); + break; } - PB { + PetaByte { $result = ($Value * [math]::Pow(10, 15)); + break; } Default {} } @@ -37,23 +43,29 @@ function ConvertTo-KiloByte() ); switch ($Unit) { - B { - $result = ($Value / 1000); + Byte { + $result = ($Value / 1000); + break; } - KB { + KiloByte { $result = $Value; + break; } - MB { + MegaByte { $result = ($Value * [math]::Pow(10, 3)); + break; } - GB { + GigaByte { $result = ($Value * [math]::Pow(10, 6)); + break; } - TB { + TeraByte { $result = ($Value * [math]::Pow(10, 9)); + break; } - PB { + PetaByte { $result = ($Value * [math]::Pow(10, 12)); + break; } Default {} } @@ -69,23 +81,29 @@ function ConvertTo-MegaByte() ); switch ($Unit) { - B { - $result = ($Value / [math]::Pow(10, 6)); + Byte { + $result = ($Value / [math]::Pow(10, 6)); + break; } - KB { + KiloByte { $result = ($Value / [math]::Pow(10, 3)); + break; } - MB { + MegaByte { $result = $Value; + break; } - GB { + GigaByte { $result = ($Value * [math]::Pow(10, 3)); + break; } - TB { + TeraByte { $result = ($Value * [math]::Pow(10, 6)); + break; } - PB { + PetaByte { $result = ($Value * [math]::Pow(10, 9)); + break; } Default {} } @@ -101,23 +119,29 @@ function ConvertTo-GigaByte() ); switch ($Unit) { - B { + Byte { $result = ($Value / [math]::Pow(10, 9)); + break; } - KB { - $result = ($Value / [math]::Pow(10, 6)) + KiloByte { + $result = ($Value / [math]::Pow(10, 6)); + break; } - MB { + MegaByte { $result = ($Value / [math]::Pow(10, 3)); + break; } - GB { + GigaByte { $result = $Value; + break; } - TB { + TeraByte { $result = ($Value * [math]::Pow(10, 3)); + break; } - PB { + PetaByte { $result = ($Value * [math]::Pow(10, 6)); + break; } Default {} } @@ -133,23 +157,29 @@ function ConvertTo-TeraByte() ); switch ($Unit) { - B { + Byte { $result = ($Value / [math]::Pow(10, 12)); + break; } - KB { + KiloByte { $result = ($Value / [math]::Pow(10, 9)); + break; } - MB { + MegaByte { $result = ($Value / [math]::Pow(10, 6)); + break; } - GB { + GigaByte { $result = ($Value / [math]::Pow(10, 3)); + break; } - TB { + TeraByte { $result = $Value; + break; } - PB { + PetaByte { $result = ($Value * [math]::Pow(10, 3)); + break; } Default {} } @@ -165,23 +195,29 @@ function ConvertTo-PetaByte() ); switch ($Unit) { - B { - $result = ($Value / [math]::Pow(10, 15)); + Byte { + $result = ($Value / [math]::Pow(10, 15)); + break; } - KB { + KiloByte { $result = ($Value / [math]::Pow(10, 12)); + break; } - MB { + MegaByte { $result = ($Value / [math]::Pow(10, 9)); + break; } - GB { + GigaByte { $result = ($Value / [math]::Pow(10, 6)); + break; } - TB { + TeraByte { $result = ($Value / [math]::Pow(10, 3)); + break; } - PB { + PetaByte { $result = $Value; + break; } Default {} } From 3be84379d94ed57c8cd47f339cce09a7fb2f285b Mon Sep 17 00:00:00 2001 From: Niko Martini Date: Mon, 22 Jul 2019 16:22:44 +0200 Subject: [PATCH 4/4] Fix Code-Styling and Validation for Strings --- lib/core/tools/ConvertTo-ByteUnit.psm1 | 220 ++++++++----------------- 1 file changed, 69 insertions(+), 151 deletions(-) diff --git a/lib/core/tools/ConvertTo-ByteUnit.psm1 b/lib/core/tools/ConvertTo-ByteUnit.psm1 index 09c29f7..f2d57b3 100644 --- a/lib/core/tools/ConvertTo-ByteUnit.psm1 +++ b/lib/core/tools/ConvertTo-ByteUnit.psm1 @@ -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;