Add instances for NonEmptyString (#13)

This commit is contained in:
Mark Eibes 2022-10-25 14:53:53 +02:00 committed by GitHub
parent e7ffcc2d5e
commit 0a674d1da5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 2 deletions

View File

@ -4,7 +4,6 @@
, dependencies =
[ "arrays"
, "bifunctors"
, "js-bigints"
, "control"
, "datetime"
, "effect"
@ -15,6 +14,7 @@
, "foreign-object"
, "identity"
, "integers"
, "js-bigints"
, "js-date"
, "lists"
, "maybe"
@ -25,6 +25,7 @@
, "partial"
, "prelude"
, "record"
, "strings"
, "transformers"
, "tuples"
, "typelevel-prelude"

View File

@ -55,6 +55,8 @@ import Data.Maybe (Maybe(..), fromMaybe, fromMaybe', maybe)
import Data.Newtype (class Newtype)
import Data.Nullable (Nullable, toMaybe, toNullable)
import Data.Number as Number
import Data.String.NonEmpty.Internal (NonEmptyString)
import Data.String.NonEmpty.Internal as NonEmptyString
import Data.Symbol (class IsSymbol, reflectSymbol)
import Data.Traversable (traverse)
import Data.Tuple (Tuple(..))
@ -212,6 +214,12 @@ instance ReadForeign BigInt where
instance ReadForeign String where
readImpl = readString
instance ReadForeign NonEmptyString where
readImpl = readString >=>
NonEmptyString.fromString
>>> note (pure $ ForeignError "String must not be empty")
>>> except
instance ReadForeign Boolean where
readImpl = readBoolean
@ -402,6 +410,9 @@ instance WriteForeign Foreign where
instance WriteForeign String where
writeImpl = unsafeToForeign
instance WriteForeign NonEmptyString where
writeImpl = unsafeToForeign
instance WriteForeign Int where
writeImpl = unsafeToForeign

View File

@ -12,11 +12,13 @@ import Data.Map as Map
import Data.Maybe (Maybe(..), fromJust, fromMaybe')
import Data.Newtype (class Newtype, un)
import Data.Nullable as Nullable
import Data.String.NonEmpty (NonEmptyString, nes)
import Data.Tuple (Tuple(..))
import Data.Tuple.Nested ((/\))
import Data.Variant (Variant, inj)
import Effect.Class (liftEffect)
import Effect.Now (nowDateTime)
import Foreign (ForeignError(..))
import Foreign.Object as Object
import Js.BigInt.BigInt (BigInt)
import Js.BigInt.BigInt as BigInt
@ -133,13 +135,20 @@ spec = describe "En- and decoding" $ do
roundtrips (UntaggedVariant (erwin "e") ∷ UntaggedVariant (Erwin ()))
let bareVariant = erwin "e"
let res = writeJSON (UntaggedVariant bareVariant ∷ UntaggedVariant ExampleVariant)
let expected = """"e""""
let expected = "\"e\""
res `shouldEqual` expected
let
parsed ∷ _ (UntaggedVariant ExampleVariant)
parsed = readJSON expected
(un UntaggedVariant <$> parsed) `shouldEqual` Right bareVariant
describe "works on non empty strings" do
it "roundtrips NonEmptyString" do
roundtrips (nes (Proxy :: Proxy "Non-Empty"))
it "fails to decode empty strings" do
let (result :: (Either _ NonEmptyString)) = readJSON (show "")
result `shouldEqual` Left (pure $ ForeignError "String must not be empty")
type ExampleVariant = ("erwin" ∷ String, "jackie" ∷ Int)
type ExampleTaggedVariant t v = TaggedVariant t v ExampleVariant