ref: web: refactor toBloodhoundJson for Data.Text

This has the advantage of there being no extra unpacking/packing of
Data.Text to/from strings where it isn't necessary.
This commit is contained in:
Arsen Arsenović 2021-08-24 16:28:12 +02:00 committed by Simon Michael
parent 9ce55146c8
commit 3504a91b42

View File

@ -13,14 +13,14 @@ module Hledger.Web.Widget.AddForm
import Control.Monad.State.Strict (evalStateT) import Control.Monad.State.Strict (evalStateT)
import Data.Bifunctor (first) import Data.Bifunctor (first)
import Data.Foldable (toList) import Data.Foldable (toList)
import Data.List (dropWhileEnd, intercalate, unfoldr) import Data.List (dropWhileEnd, unfoldr)
import Data.Maybe (isJust) import Data.Maybe (isJust)
import qualified Data.Set as S import qualified Data.Set as S
import Data.Text (Text) import Data.Text (Text)
import Data.Text.Encoding.Base64 (encodeBase64) import Data.Text.Encoding.Base64 (encodeBase64)
import qualified Data.Text as T import qualified Data.Text as T
import Data.Time (Day) import Data.Time (Day)
import Text.Blaze.Internal (Markup, preEscapedString) import Text.Blaze.Internal (Markup, preEscapedText)
import Text.Megaparsec (bundleErrors, eof, parseErrorTextPretty, runParser) import Text.Megaparsec (bundleErrors, eof, parseErrorTextPretty, runParser)
import Yesod import Yesod
@ -91,11 +91,11 @@ addForm j today = identifyForm "add" $ \extra -> do
-- This used to work, but since 1.16, it seems like something changed. -- This used to work, but since 1.16, it seems like something changed.
-- toJSON ("a"::Text) gives String "a" instead of "a", etc. -- toJSON ("a"::Text) gives String "a" instead of "a", etc.
-- preEscapedString . escapeJSSpecialChars . show . toJSON -- preEscapedString . escapeJSSpecialChars . show . toJSON
preEscapedString $ concat [ preEscapedText $ T.concat [
"[", "[",
intercalate "," $ map ( T.intercalate "," $ map (
("{\"value\":" ++). ("{\"value\":" <>).
(++"}"). (<> "}").
-- This will convert a value such as ``hledger!`` into -- This will convert a value such as ``hledger!`` into
-- ``atob("aGxlZGdlciE=")``. When this gets evaluated on the client, -- ``atob("aGxlZGdlciE=")``. When this gets evaluated on the client,
-- the resulting string is ``hledger!`` again. The same data is -- the resulting string is ``hledger!`` again. The same data is
@ -107,8 +107,8 @@ addForm j today = identifyForm "add" $ \extra -> do
"]" "]"
] ]
where where
b64wrap :: Text -> String b64wrap :: Text -> Text
b64wrap = ("atob(\""++) . (++"\")") . T.unpack . encodeBase64 b64wrap = ("atob(\""<>) . (<>"\")") . encodeBase64
validateTransaction :: validateTransaction ::
FormResult Day FormResult Day