byteunits 0.2.0.2 → 0.3.0.2
raw patch · 3 files changed
+71/−29 lines, 3 filesdep +HUnitPVP ok
version bump matches the API change (PVP)
Dependencies added: HUnit
API changes (from Hackage documentation)
- Data.ByteUnits: getUnits :: ByteUnit -> Float -> Float
+ Data.ByteUnits: ByteValue :: Float -> ByteUnit -> ByteValue
+ Data.ByteUnits: convertByteUnit :: ByteValue -> ByteUnit -> ByteValue
+ Data.ByteUnits: data ByteValue
+ Data.ByteUnits: getBytes :: ByteValue -> Float
+ Data.ByteUnits: getShortHand :: ByteValue -> String
+ Data.ByteUnits: instance GHC.Classes.Eq Data.ByteUnits.ByteValue
+ Data.ByteUnits: instance GHC.Show.Show Data.ByteUnits.ByteValue
- Data.ByteUnits: getAppropriateUnits :: Float -> (ByteUnit, Float)
+ Data.ByteUnits: getAppropriateUnits :: ByteValue -> ByteValue
Files
- byteunits.cabal +2/−1
- src/Data/ByteUnits.hs +51/−20
- tests/Tests.hs +18/−8
byteunits.cabal view
@@ -1,5 +1,5 @@ name: byteunits-version: 0.2.0.2+version: 0.3.0.2 description: Human friendly conversion between byte units (KB, MB, GB...)... synopsis: Human friendly conversion between byte units (KB, MB, GB...) license: BSD3@@ -31,3 +31,4 @@ , Cabal , QuickCheck , byteunits+ , HUnit
src/Data/ByteUnits.hs view
@@ -1,30 +1,61 @@ module Data.ByteUnits where import Safe+import Numeric data ByteUnit = Bytes | KiloBytes | MegaBytes | GigaBytes | TeraBytes | PetaBytes | ExaBytes deriving (Show, Eq) -getUnits :: ByteUnit -> Float -> Float-getUnits bytesUnit bytes = case bytesUnit of- Bytes -> bytes- KiloBytes -> bytes / (1024 ** 1)- MegaBytes -> bytes / (1024 ** 2)- GigaBytes -> bytes / (1024 ** 3)- TeraBytes -> bytes / (1024 ** 4)- PetaBytes -> bytes / (1024 ** 4)- ExaBytes -> bytes / (1024 ** 5)+data ByteValue = ByteValue Float ByteUnit deriving (Show, Eq) --- | Rounds up to the highest unit provided it's > 1+-- | Gets the value of bytes from a ByteValue type ----- >>> getAppropriateUnits 1024--- (KiloBytes,1.0)+getBytes :: ByteValue -> Float+getBytes (ByteValue v bu) = case bu of+ Bytes -> v+ KiloBytes -> v * (1024 ** 1)+ MegaBytes -> v * (1024 ** 2)+ GigaBytes -> v * (1024 ** 3)+ TeraBytes -> v * (1024 ** 4)+ PetaBytes -> v * (1024 ** 4)+ ExaBytes -> v * (1024 ** 5)++-- | Converts the ByteValue to an ByteValue with the specified ByteUnit ----- >>> getAppropriateUnits (3.5 * 1024* 1024)--- (MegaBytes,3.5)-getAppropriateUnits :: Float -> (ByteUnit, Float)-getAppropriateUnits bytes = do- let units = fmap (\bu -> (bu, getUnits bu bytes)) [Bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes, ExaBytes]- let appropriateUnits = filter (((<=) 1.0) . snd) units+convertByteUnit :: ByteValue -> ByteUnit -> ByteValue+convertByteUnit bv bu = case bu of+ Bytes -> ByteValue bytes Bytes+ KiloBytes -> ByteValue (bytes / (1024 ** 1)) KiloBytes+ MegaBytes -> ByteValue (bytes / (1024 ** 2)) MegaBytes+ GigaBytes -> ByteValue (bytes / (1024 ** 3)) GigaBytes+ TeraBytes -> ByteValue (bytes / (1024 ** 4)) TeraBytes+ PetaBytes -> ByteValue (bytes / (1024 ** 4)) PetaBytes+ ExaBytes -> ByteValue (bytes / (1024 ** 5)) ExaBytes+ where bytes = getBytes bv++-- | Converts to the largest unit size provided the float value is > 1+--+-- >>> getAppropriateUnits (ByteValue 1024 Bytes)+-- ByteValue 1 KiloBytes+--+-- >>> getAppropriateUnits (ByteValue (3.5 * 1024* 1024) Bytes)+-- ByteValue 3.5 MegaBytes+getAppropriateUnits :: ByteValue -> ByteValue+getAppropriateUnits bv = do+ let bUnits = [Bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes, ExaBytes]+ let bytes = getBytes bv+ let units = fmap (\bu -> convertByteUnit (ByteValue bytes Bytes) bu) bUnits+ let appropriateUnits = filter (\(ByteValue v' _) -> (v' >= 1.0)) units case (lastMay appropriateUnits) of- Just (x) -> x- Nothing -> (Bytes, bytes)+ Just (bv') -> bv'+ Nothing -> bv++getShortHand :: ByteValue -> String+getShortHand (ByteValue v bu) = (showFFloat (Just 2) v) (" " ++buShort) where+ buShort = case bu of+ Bytes -> "B"+ KiloBytes -> "KB"+ MegaBytes -> "MB"+ GigaBytes -> "GB"+ TeraBytes -> "TB"+ PetaBytes -> "PB"+ ExaBytes -> "EB"
tests/Tests.hs view
@@ -1,13 +1,23 @@ module Main where -import Test.QuickCheck (quickCheck)+import Test.QuickCheck import Data.ByteUnits+import Test.HUnit +hUnitTests = test [+ "" ~: "" ~: getBytes (ByteValue 125 Bytes) ~=? 125+ , "" ~: "" ~: getBytes (ByteValue 1024 Bytes) ~=? 1024+ , "" ~: "" ~: getBytes (ByteValue 2048 KiloBytes) ~=? (2048 * 1024)+ , "" ~: "" ~: getBytes (ByteValue 3.0 MegaBytes) ~=? (3 * 1024 * 1024)+ , "" ~: "" ~: (convertByteUnit (ByteValue (3 * 1024 * 1024) Bytes) MegaBytes) ~=? ByteValue 3.0 MegaBytes+ , "" ~: "" ~: (convertByteUnit (ByteValue (3 * 1024 * 1024) Bytes) MegaBytes) ~=? ByteValue 3.0 MegaBytes+ , "" ~: "" ~: getAppropriateUnits (ByteValue 125 Bytes) ~=? (ByteValue 125 Bytes)+ -- TODO: figure out if Hunit has a not equal assertion+ , "" ~: "" ~: getAppropriateUnits (ByteValue 125 Bytes) /= ByteValue 3.0 MegaBytes ~=? True+ , "" ~: "" ~: getBytes (ByteValue 3 KiloBytes) ~=? (3 * 1024)+ , "" ~: "" ~: convertByteUnit (ByteValue (1024 * 1024) KiloBytes) MegaBytes ~=? ByteValue 1024 MegaBytes+ , "" ~: "" ~: getShortHand (ByteValue 1024 MegaBytes) ~=? "1024.00 MB"+ ]+ main = do- quickCheck (getUnits KiloBytes 1024 == 1.0)- quickCheck (getUnits KiloBytes 2048 == 2.0)- quickCheck (getUnits MegaBytes (1024 * 1024) == 1.0)- quickCheck (getUnits MegaBytes (3 * 1024 * 1024) == 3.0)- quickCheck (getAppropriateUnits (3 * 1024 * 1024) == (MegaBytes, 3.0))- quickCheck (getAppropriateUnits (125) == (Bytes, 125.0))- quickCheck (getAppropriateUnits (125) /= (MegaBytes, 3.0))+ runTestTT hUnitTests