packages feed

qr-imager-0.1.2.0: src/Data/QRCodes/Utils.hs

module Data.QRCodes.Utils where

import qualified Data.ByteString.Char8 as BS
import Data.Char (toLower, toUpper)
import Data.List.Utils (replace)

-- | function applied to byteStrings before saving to QR code so that uppercase/lowercase signatures can be preserverd
preserveUpper :: BS.ByteString -> BS.ByteString
preserveUpper = lift pU
    where pU = concatMap (\c -> if c `elem` ['A'..'Z'] then ((toLower c) : "---") else return c)

-- | resolve coded string to string with uppercase
resolveUpper :: BS.ByteString -> BS.ByteString
resolveUpper = lift rU
    where rU = foldr (.) id (map (\s -> replace (s : "---") ((pure . toUpper) s)) ['a'..'z'])

--kind of hacky and could be fixed in all likelihood
lift :: (String -> String) -> (BS.ByteString -> BS.ByteString)
lift f = BS.pack . f . BS.unpack

--liftIO = either (show) (((++) "success! ") . show)

liftEither :: (Show b, Monad m) => (t -> m a) -> Either b t -> m a
liftEither = either (error . show)