git.iko.soy/purescript-indexed-DB
on master help sign in
add Show and Ord instances to IDBKeys
629979dKtorZ
src/Database/IndexedDB/IDBKey/Internal.purs+32−1
@@ -18,7 +18,7 @@ import Data.Function.Uncurried (Fn2, Fn4, Fn7)
18import Data.List.NonEmpty (NonEmptyList(..))
18import Data.List.NonEmpty (NonEmptyList(..))
19import Data.List.Types (List(..))
19import Data.List.Types (List(..))
20import Data.NonEmpty (NonEmpty(..))
20import Data.NonEmpty (NonEmpty(..))
21−import Data.Either (Either(..), isRight)
21+import Data.Either (Either(..), either, isRight)
22import Data.Identity (Identity(..))
22import Data.Identity (Identity(..))
23import Data.Nullable (Nullable, toNullable)
23import Data.Nullable (Nullable, toNullable)
24import Data.Time as Time
24import Data.Time as Time
@@ -48,6 +48,8 @@ class IDBKey a where
48instance eqKey :: Eq Key where
48instance eqKey :: Eq Key where
49 eq a b = (runExceptT >>> runIdentity >>> isRight) $
49 eq a b = (runExceptT >>> runIdentity >>> isRight) $
50 eq <$> ((fromKey a) :: F Int) <*> fromKey b
50 eq <$> ((fromKey a) :: F Int) <*> fromKey b
51+ <|>
52+ eq <$> ((fromKey a) :: F Number) <*> fromKey b
51 <|>
53 <|>
52 eq <$> ((fromKey a) :: F String) <*> fromKey b
54 eq <$> ((fromKey a) :: F String) <*> fromKey b
53 <|>
55 <|>
@@ -57,6 +59,35 @@ instance eqKey :: Eq Key where
57 runIdentity (Identity x) = x
59 runIdentity (Identity x) = x
58
60
59
61
62+instance ordKey :: Ord Key where
63+ compare a b = (runExceptT >>> runIdentity >>> either (const LT) id) $
64+ compare <$> ((fromKey a) :: F Int) <*> fromKey b
65+ <|>
66+ compare <$> ((fromKey a) :: F Number) <*> fromKey b
67+ <|>
68+ compare <$> ((fromKey a) :: F String) <*> fromKey b
69+ <|>
70+ compare <$> ((fromKey a) :: F DateTime) <*> fromKey b
71+ where
72+ runIdentity :: forall a. Identity a -> a
73+ runIdentity (Identity x) = x
74+
75+
76+instance showKey :: Show Key where
77+ show a = (runExceptT >>> format) $
78+ (show <$> (fromKey a :: F Int))
79+ <|>
80+ (show <$> (fromKey a :: F Number))
81+ <|>
82+ (show <$> (fromKey a :: F String))
83+ <|>
84+ (show <$> (fromKey a :: F DateTime))
85+ where
86+ format :: forall a. Identity (Either a String) -> String
87+ format (Identity x) =
88+ either (const "(Key)") (\s -> "(Key " <> s <> ")") x
89+
90+
60instance idbKeyInt :: IDBKey Int where
91instance idbKeyInt :: IDBKey Int where
61 toKey = Foreign.toForeign >>> Key
92 toKey = Foreign.toForeign >>> Key
62 fromKey (Key f) = Foreign.readInt f
93 fromKey (Key f) = Foreign.readInt f