aboutsummaryrefslogtreecommitdiff
path: root/src/Order.purs
diff options
context:
space:
mode:
authorJoris2017-04-21 17:31:50 +0200
committerJoris2017-04-21 17:31:50 +0200
commitd359e1d6203728783c97ed1313e62088dd44f386 (patch)
tree1841f31f6c97cdf3ae453c803915083dab37460d /src/Order.purs
parentb7a00a27d50353f53bd5e74a0c67f1a238518cb7 (diff)
Sort table by clicking on the header
Diffstat (limited to 'src/Order.purs')
-rw-r--r--src/Order.purs54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/Order.purs b/src/Order.purs
new file mode 100644
index 0000000..33c2be9
--- /dev/null
+++ b/src/Order.purs
@@ -0,0 +1,54 @@
+module Order
+ ( Order
+ , OrderKind(..)
+ , OrderDirection(..)
+ , init
+ , select
+ ) where
+
+import Prelude (class Eq, (==))
+import Data.Generic (class Generic, gEq)
+
+type Order =
+ { kind :: OrderKind
+ , direction :: OrderDirection
+ }
+
+data OrderKind =
+ Name
+ | GlycemicIndex
+ | Carbohydrates
+ | GlycemicLoad
+
+derive instance genericOrderKind :: Generic OrderKind
+
+instance eqOrderKind :: Eq OrderKind where
+ eq = gEq
+
+data OrderDirection =
+ Ascending
+ | Descending
+
+derive instance genericOrderDirection :: Generic OrderDirection
+
+instance eqOrderDirection :: Eq OrderDirection where
+ eq = gEq
+
+init :: Order
+init =
+ { kind: Name
+ , direction: initDirection
+ }
+
+select :: OrderKind -> Order -> Order
+select kind order =
+ if order.kind == kind
+ then order { direction = otherDirection order.direction }
+ else order { kind = kind, direction = initDirection }
+
+otherDirection :: OrderDirection -> OrderDirection
+otherDirection Ascending = Descending
+otherDirection Descending = Ascending
+
+initDirection :: OrderDirection
+initDirection = Ascending