Skip to content

Commit 8aa66ac

Browse files
felixSchlpaf31
authored andcommitted
Add various instances for NullOrUndefined (#52)
* Derive more instances for `NullOrUndefined` et. al * Wrap `show` output in parenthesis * Remove Data.Function import
1 parent a761bcf commit 8aa66ac

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/Data/Foreign/Null.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Data.Foreign.Null where
33
import Prelude
44

55
import Data.Maybe (Maybe(..))
6+
import Data.Newtype (class Newtype, unwrap)
67
import Data.Foreign (F, Foreign, isNull)
78

89
-- | A `newtype` wrapper whose `IsForeign` instance correctly handles
@@ -12,6 +13,13 @@ import Data.Foreign (F, Foreign, isNull)
1213
-- | but not `undefined`.
1314
newtype Null a = Null (Maybe a)
1415

16+
derive instance newtypeNull :: Newtype (Null a) _
17+
derive instance eqNull :: (Eq a) => Eq (Null a)
18+
derive instance ordNull :: (Ord a) => Ord (Null a)
19+
20+
instance showNull :: (Show a) => Show (Null a) where
21+
show x = "(Null " <> show (unwrap x) <> ")"
22+
1523
-- | Unwrap a `Null` value
1624
unNull :: forall a. Null a -> Maybe a
1725
unNull (Null m) = m

src/Data/Foreign/NullOrUndefined.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Data.Foreign.NullOrUndefined where
22

33
import Prelude
44

5+
import Data.Newtype (class Newtype, unwrap)
56
import Data.Maybe (Maybe(..))
67
import Data.Foreign (F, Foreign, isUndefined, isNull)
78

@@ -12,6 +13,13 @@ import Data.Foreign (F, Foreign, isUndefined, isNull)
1213
-- | or `undefined`.
1314
newtype NullOrUndefined a = NullOrUndefined (Maybe a)
1415

16+
derive instance newtypeNullOrUndefined :: Newtype (NullOrUndefined a) _
17+
derive instance eqNullOrUndefined :: (Eq a) => Eq (NullOrUndefined a)
18+
derive instance ordNullOrUndefined :: (Ord a) => Ord (NullOrUndefined a)
19+
20+
instance showNullOrUndefined :: (Show a) => Show (NullOrUndefined a) where
21+
show x = "(NullOrUndefined " <> show (unwrap x) <> ")"
22+
1523
-- | Unwrap a `NullOrUndefined` value
1624
unNullOrUndefined :: forall a. NullOrUndefined a -> Maybe a
1725
unNullOrUndefined (NullOrUndefined m) = m

src/Data/Foreign/Undefined.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Data.Foreign.Undefined where
33
import Prelude
44

55
import Data.Maybe (Maybe(..))
6+
import Data.Newtype (class Newtype, unwrap)
67
import Data.Foreign (F, Foreign, isUndefined)
78

89
-- | A `newtype` wrapper whose `IsForeign` instance correctly handles
@@ -12,6 +13,13 @@ import Data.Foreign (F, Foreign, isUndefined)
1213
-- | but not `null`.
1314
newtype Undefined a = Undefined (Maybe a)
1415

16+
derive instance newtypeUndefined :: Newtype (Undefined a) _
17+
derive instance eqUndefined :: (Eq a) => Eq (Undefined a)
18+
derive instance ordUndefined :: (Ord a) => Ord (Undefined a)
19+
20+
instance showUndefined :: (Show a) => Show (Undefined a) where
21+
show x = "(Undefined " <> show (unwrap x) <> ")"
22+
1523
-- | Unwrap an `Undefined` value
1624
unUndefined :: forall a. Undefined a -> Maybe a
1725
unUndefined (Undefined m) = m

0 commit comments

Comments
 (0)