Slightly more efficient implementations of Model.Entry.Shorten

This commit is contained in:
Danielle McLean 2017-10-15 02:37:37 +11:00
parent 940b62b6fc
commit 068ce7a41c
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5

View file

@ -9,20 +9,22 @@ import qualified Data.Text as T
import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy as TL
entryTitle :: Entry -> T.Text entryTitle :: Entry -> T.Text
entryTitle = fromMaybe <$> TL.toStrict . shorten 30 . unMarkdown . entryContent <*> entryName entryTitle = fromMaybe <$> TL.toStrict . unMarkdown . shorten 30 . entryContent <*> entryName
class Shorten a where class Shorten a where
shorten :: Integral n => n -> a -> a shorten :: Int -> a -> a
instance Shorten T.Text where instance Shorten T.Text where
shorten n t shorten i t
| T.length t > fromIntegral n = flip T.append "..." . T.take (fromIntegral n - 1) $ t | T.compareLength t n == GT = flip T.append "..." . T.take (n - 1) $ t
| otherwise = t | otherwise = t
where n = fromIntegral i
instance Shorten TL.Text where instance Shorten TL.Text where
shorten n t shorten i t
| TL.length t > fromIntegral n = flip TL.append "..." . TL.take (fromIntegral n - 1) $ t | TL.compareLength t n == GT = flip TL.append "..." . TL.take (n - 1) $ t
| otherwise = t | otherwise = t
where n = fromIntegral i
instance Shorten Markdown where instance Shorten Markdown where
shorten n (Markdown t) = Markdown $ shorten n t shorten n (Markdown t) = Markdown $ shorten n t