{- | Conversion between 'Data.Text.Text' and 'Data.JSString.JSString'  -}

module Data.JSString.Text
    ( textToJSString
    , textFromJSString
    , lazyTextToJSString
    , lazyTextFromJSString
    , textFromJSVal
    , lazyTextFromJSVal
    ) where

import GHCJS.Prim.Internal (JSVal)

import Data.Coerce (coerce)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL

import Data.JSString.Internal.Type

import Language.Javascript.JSaddle.Types (JSM, GHCJSPure(..))
import Language.Javascript.JSaddle.Native.Internal
       (valueToString)

textToJSString :: T.Text -> JSString
textToJSString :: Text -> JSString
textToJSString = Text -> JSString
forall a b. Coercible a b => a -> b
coerce
{-# INLINE textToJSString #-}

textFromJSString :: JSString -> T.Text
textFromJSString :: JSString -> Text
textFromJSString = JSString -> Text
forall a b. Coercible a b => a -> b
coerce
{-# INLINE  textFromJSString #-}

lazyTextToJSString :: TL.Text -> JSString
lazyTextToJSString :: Text -> JSString
lazyTextToJSString = Text -> JSString
forall a b. Coercible a b => a -> b
coerce (Text -> JSString) -> (Text -> Text) -> Text -> JSString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
TL.toStrict
{-# INLINE lazyTextToJSString #-}

lazyTextFromJSString :: JSString -> TL.Text
lazyTextFromJSString :: JSString -> Text
lazyTextFromJSString = Text -> Text
TL.fromStrict (Text -> Text) -> (JSString -> Text) -> JSString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSString -> Text
forall a b. Coercible a b => a -> b
coerce
{-# INLINE lazyTextFromJSString #-}

-- | returns the empty Text if not a string
textFromJSVal :: JSVal -> GHCJSPure T.Text
textFromJSVal :: JSVal -> GHCJSPure Text
textFromJSVal JSVal
j = JSM Text -> GHCJSPure Text
forall a. JSM a -> GHCJSPure a
GHCJSPure (JSM Text -> GHCJSPure Text) -> JSM Text -> GHCJSPure Text
forall a b. (a -> b) -> a -> b
$ JSString -> Text
textFromJSString (JSString -> Text) -> JSM JSString -> JSM Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal -> JSM JSString
valToJSString JSVal
j
{-# INLINE textFromJSVal #-}

-- | returns the empty Text if not a string
lazyTextFromJSVal :: JSVal -> GHCJSPure TL.Text
lazyTextFromJSVal :: JSVal -> GHCJSPure Text
lazyTextFromJSVal JSVal
j = JSM Text -> GHCJSPure Text
forall a. JSM a -> GHCJSPure a
GHCJSPure (JSM Text -> GHCJSPure Text) -> JSM Text -> GHCJSPure Text
forall a b. (a -> b) -> a -> b
$ JSString -> Text
lazyTextFromJSString (JSString -> Text) -> JSM JSString -> JSM Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal -> JSM JSString
valToJSString JSVal
j
{-# INLINE lazyTextFromJSVal #-}

valToJSString :: JSVal -> JSM JSString
valToJSString :: JSVal -> JSM JSString
valToJSString = JSVal -> JSM JSString
valueToString
{-# INLINE valToJSString #-}