byteunits (empty) → 0.1.0.0
raw patch · 6 files changed
+112/−0 lines, 6 filesdep +Cabaldep +QuickCheckdep +basesetup-changed
Dependencies added: Cabal, QuickCheck, base, byteunits, safe
Files
- ByteUnits.hs +30/−0
- ChangeLog.md +5/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- byteunits.cabal +32/−0
- tests/Tests.hs +13/−0
+ ByteUnits.hs view
@@ -0,0 +1,30 @@+module ByteUnits where++import Safe++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)++-- | Rounds up to the highest unit provided it's > 1+--+-- >>> getAppropriateUnits 1024+-- (KiloBytes,1.0)+--+-- >>> 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+ case (lastMay appropriateUnits) of+ Just (x) -> x+ Nothing -> (Bytes, bytes)
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for app++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2017, Chris Stryczynski++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Chris Stryczynski nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ byteunits.cabal view
@@ -0,0 +1,32 @@+name: byteunits+version: 0.1.0.0+description: Human friendly conversion between byte units (KB, MB, GB...)+synopsis: Human friendly conversion between byte units (KB, MB, GB...)+license: BSD3+license-file: LICENSE+author: CabalSaneDefault+maintainer: nobody+category: Library+build-type: Simple+extra-source-files: ChangeLog.md+cabal-version: >=1.10+source-repository head+ type: git+ location: https://github.com/chrissound/byteunits++library+ default-language: Haskell2010+ exposed-modules: ByteUnits+ build-depends: base >=4.9 && <4.10+ , safe == 0.3.15+ default-language: Haskell2010++Test-Suite testing-example+ default-language: Haskell2010+ Type: exitcode-stdio-1.0+ Main-is: Tests.hs+ hs-source-dirs: tests+ build-depends: base+ , Cabal+ , QuickCheck+ , byteunits
+ tests/Tests.hs view
@@ -0,0 +1,13 @@+module Main where++import Test.QuickCheck (quickCheck)+import ByteUnits++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))