From 6095f6a5864ce599430d45f580dc05773f2f0fe2 Mon Sep 17 00:00:00 2001 From: Crited Date: Wed, 30 Oct 2019 13:52:37 +0100 Subject: [PATCH] Add Conversion for both IEC and SI Byte-Suffix (10^X & 2^X) --- lib/core/tools/Convert-Bytes.psm1 | 16 +- lib/core/tools/ConvertTo-ByteUnitIEC.psm1 | 143 ++++++++++++++++++ ...yteUnit.psm1 => ConvertTo-ByteUnitSI.psm1} | 2 +- 3 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 lib/core/tools/ConvertTo-ByteUnitIEC.psm1 rename lib/core/tools/{ConvertTo-ByteUnit.psm1 => ConvertTo-ByteUnitSI.psm1} (99%) diff --git a/lib/core/tools/Convert-Bytes.psm1 b/lib/core/tools/Convert-Bytes.psm1 index 445b1c4..3fc538f 100644 --- a/lib/core/tools/Convert-Bytes.psm1 +++ b/lib/core/tools/Convert-Bytes.psm1 @@ -5,19 +5,29 @@ function Convert-Bytes() [string]$Unit ); - If (($Value -Match "(^[0-9]*) ?(B|b|kb|KB|kB|Kb|mb|Mb|mB|MB|Gb|gB|gb|GB|tb|Tb|tB|TB|PT|pt|pT|Pt)")) { + If (($Value -Match "(^[0-9]*) ?(B|KB|MB|GB|TB|PT|Kibi|Mibi|Gibi|Tibi|Pibi)")) { [single]$CurrentValue = $Matches[1]; [string]$CurrentUnit = $Matches[2]; - $CurrentValue = ConvertTo-Byte $CurrentValue $CurrentUnit; + switch ($CurrentUnit) { + { 'KiBi', 'Mibi', 'Gibi', 'Tibi', 'Pibi' -contains $_} { $CurrentValue = ConvertTo-ByteIEC $CurrentValue $CurrentUnit; $boolOption = $true;} + { 'KB', 'MB', 'GB', 'TB', 'PB' -contains $_} { $CurrentValue = ConvertTo-ByteSI $CurrentValue $CurrentUnit; $boolOption = $true;} + } + switch ($Unit) { - { 'B' -contains $_} { $FinalValue = ConvertTo-Byte $CurrentValue -Unit B; $boolOption = $true;} + #{ 'B' -contains $_} { $FinalValue = ConvertTo-ByteSI $CurrentValue -Unit B; $boolOption = $true;} { 'KB' -contains $_} { $FinalValue = ConvertTo-KiloByte $CurrentValue -Unit B; $boolOption = $true;} { 'MB' -contains $_} { $FinalValue = ConvertTo-MegaByte $CurrentValue -Unit B; $boolOption = $true;} { 'GB' -contains $_} { $FinalValue = ConvertTo-GigaByte $CurrentValue -Unit B; $boolOption = $true;} { 'TB' -contains $_} { $FinalValue = ConvertTo-TeraByte $CurrentValue -Unit B; $boolOption = $true;} { 'PT' -contains $_} { $FinalValue = ConvertTo-PetaByte $CurrentValue -Unit B; $boolOption = $true;} + { 'Kibi' -contains $_} { $FinalValue = ConvertTo-KibiByte $CurrentValue -Unit B; $boolOption = $true;} + { 'Mibi' -contains $_} { $FinalValue = ConvertTo-MibiByte $CurrentValue -Unit B; $boolOption = $true;} + { 'Gibi' -contains $_} { $FinalValue = ConvertTo-GibiByte $CurrentValue -Unit B; $boolOption = $true;} + { 'Tibi' -contains $_} { $FinalValue = ConvertTo-TibiByte $CurrentValue -Unit B; $boolOption = $true;} + { 'Piti' -contains $_} { $FinalValue = ConvertTo-PetaByte $CurrentValue -Unit B; $boolOption = $true;} + default { if (-Not $boolOption) { Throw 'Invalid input'; diff --git a/lib/core/tools/ConvertTo-ByteUnitIEC.psm1 b/lib/core/tools/ConvertTo-ByteUnitIEC.psm1 new file mode 100644 index 0000000..ec9256c --- /dev/null +++ b/lib/core/tools/ConvertTo-ByteUnitIEC.psm1 @@ -0,0 +1,143 @@ +function ConvertTo-ByteIEC() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + { 'B', 'Byte' -contains $_ } { $result = $Value; $boolOption = $true; } + { 'Kibi', 'KibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 10)); $boolOption = $true; } + { 'Mibi', 'MibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 20)); $boolOption = $true; } + { 'Gibi', 'GibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 30)); $boolOption = $true; } + { 'Tibi', 'TibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 40)); $boolOption = $true; } + { 'Piti', 'PitiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 50)); $boolOption = $true; } + default { + if (-Not $boolOption) { + Throw 'Invalid input'; + } + } + } + + return $result; +} + +function ConvertTo-KibiByte() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + { 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(2, 10)); $boolOption = $true; } + { 'Kibi', 'KibiByte' -contains $_ } { $result = $Value; $boolOption = $true; } + { 'Mibi', 'MibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 10)); $boolOption = $true; } + { 'Gibi', 'GibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 20)); $boolOption = $true; } + { 'Tibi', 'TibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 30)); $boolOption = $true; } + { 'Piti', 'PitiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 40)); $boolOption = $true; } + default { + if (-Not $boolOption) { + Throw 'Invalid input'; + } + } + } + + return $result; +} + +function ConvertTo-MibiByte() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + { 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(2, 20)); $boolOption = $true; } + { 'Kibi', 'KibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 10)); $boolOption = $true; } + { 'Mibi', 'MibiByte' -contains $_ } { $result = $Value; $boolOption = $true; } + { 'Gibi', 'GibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 10)); $boolOption = $true; } + { 'Tibi', 'TibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 20)); $boolOption = $true; } + { 'Piti', 'PitiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 30)); $boolOption = $true; } + default { + if (-Not $boolOption) { + Throw 'Invalid input'; + } + } + } + + return $result; +} + +function ConvertTo-GibiByte() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + { 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(2, 30)); $boolOption = $true; } + { 'Kibi', 'KibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 20)); $boolOption = $true; } + { 'Mibi', 'MibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 10)); $boolOption = $true; } + { 'Gibi', 'GibiByte' -contains $_ } { $result = $Value; $boolOption = $true; } + { 'Tibi', 'TibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 10)); $boolOption = $true; } + { 'Piti', 'PitiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 20)); $boolOption = $true; } + default { + if (-Not $boolOption) { + Throw 'Invalid input'; + } + } + } + + return $result; +} + +function ConvertTo-TibiByte() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + { 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(2, 40)); $boolOption = $true; } + { 'Kibi', 'KibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 30)); $boolOption = $true; } + { 'Mibi', 'MibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 20)); $boolOption = $true; } + { 'Gibi', 'GibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 10)); $boolOption = $true; } + { 'Tibi', 'TibiByte' -contains $_ } { $result = $Value; $boolOption = $true; } + { 'Piti', 'PitiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 10)); $boolOption = $true; } + default { + if (-Not $boolOption) { + Throw 'Invalid input'; + } + } + } + + return $result; +} + +function ConvertTo-PitiByte() +{ + param( + [single]$Value, + [string]$Unit + ); + + switch ($Unit) { + { 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(2, 50)); $boolOption = $true; } + { 'Kibi', 'KibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 40)); $boolOption = $true; } + { 'Mibi', 'MibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 30)); $boolOption = $true; } + { 'Gibi', 'GibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 20)); $boolOption = $true; } + { 'Tibi', 'TibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 10)); $boolOption = $true; } + { 'Piti', 'PitiByte' -contains $_ } { $result = $Value; $boolOption = $true; } + default { + if (-Not $boolOption) { + Throw 'Invalid input'; + } + } + } + + return $result; +} diff --git a/lib/core/tools/ConvertTo-ByteUnit.psm1 b/lib/core/tools/ConvertTo-ByteUnitSI.psm1 similarity index 99% rename from lib/core/tools/ConvertTo-ByteUnit.psm1 rename to lib/core/tools/ConvertTo-ByteUnitSI.psm1 index 67c8c33..107dc42 100644 --- a/lib/core/tools/ConvertTo-ByteUnit.psm1 +++ b/lib/core/tools/ConvertTo-ByteUnitSI.psm1 @@ -14,7 +14,7 @@ .NOTES #> -function ConvertTo-Byte() +function ConvertTo-ByteSI() { param( [single]$Value,