aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoris2017-04-05 17:04:33 +0200
committerJoris2017-04-05 17:04:33 +0200
commit9a95a674fbbf1e64d3ad07922d569c3a1c751cf2 (patch)
tree2f99889040b0af406df1c403b9d930fe77e706cf
parentf6a73e5bd6a5e2d7d4eb9c8a14bdf1a0c8a4ac4c (diff)
Show aliments, glycemic index, carbohydrates and glycemic charge
-rw-r--r--.tmuxinator.yml4
-rw-r--r--README.md4
-rw-r--r--bower.json5
-rw-r--r--package.json6
-rw-r--r--public/icon.pngbin0 -> 399 bytes
-rw-r--r--public/index.html4
-rw-r--r--public/main.css26
-rw-r--r--src/Aliment.purs13
-rw-r--r--src/Food.purs25
-rw-r--r--src/Format.purs29
-rw-r--r--src/Page.purs45
-rwxr-xr-xstop2
-rw-r--r--test/Main.purs27
13 files changed, 162 insertions, 28 deletions
diff --git a/.tmuxinator.yml b/.tmuxinator.yml
index be3eac8..914bf4f 100644
--- a/.tmuxinator.yml
+++ b/.tmuxinator.yml
@@ -1,4 +1,4 @@
-name: sucre
+name: glycemie
windows:
- main:
@@ -7,4 +7,4 @@ windows:
- # Empty
- build:
- webfsd -r public -f index.html -p 9000
- - npm install pulp && bower install && ./watch
+ - npm install && bower install && ./watch
diff --git a/README.md b/README.md
index 3039512..ff01331 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,5 @@
-Sucre
-=====
+Glycémie
+========
Getting started
---------------
diff --git a/bower.json b/bower.json
index 6ec6ec0..0293f4a 100644
--- a/bower.json
+++ b/bower.json
@@ -1,5 +1,5 @@
{
- "name": "sucre",
+ "name": "glycemie",
"private": true,
"ignore": [
"**/.*",
@@ -8,6 +8,7 @@
"output"
],
"dependencies": {
- "purescript-halogen": "*"
+ "purescript-halogen": "*",
+ "purescript-spec": "*"
}
}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..a3d0b92
--- /dev/null
+++ b/package.json
@@ -0,0 +1,6 @@
+{
+ "private": true,
+ "devDependencies": {
+ "pulp": "^10.0.4"
+ }
+}
diff --git a/public/icon.png b/public/icon.png
new file mode 100644
index 0000000..48210e7
--- /dev/null
+++ b/public/icon.png
Binary files differ
diff --git a/public/index.html b/public/index.html
index f1bb196..a322388 100644
--- a/public/index.html
+++ b/public/index.html
@@ -1,8 +1,10 @@
<!doctype html>
<html>
<head>
- <title>Sucre</title>
+ <title>Glycémie</title>
+ <meta charset="UTF-8">
<link rel="stylesheet" href="main.css">
+ <link rel="icon" href="icon.png">
</head>
<body>
<script src="main.js"></script>
diff --git a/public/main.css b/public/main.css
index 6388837..86455a6 100644
--- a/public/main.css
+++ b/public/main.css
@@ -4,3 +4,29 @@ h1 {
font-size: 46px;
color: #33AA00;
}
+
+ul.aliments {
+ display: table;
+ margin: 0 auto;
+}
+
+ul.aliments > li {
+ display: table-row;
+}
+
+ul.aliments > li.title {
+ font-weight: bold;
+}
+
+ul.aliments > li > div {
+ display: table-cell;
+ padding: 15px;
+}
+
+ul.aliments > li > .number {
+ text-align: right;
+}
+
+ul.aliments > li:nth-child(2n+3) {
+ background-color: #EEEEEE;
+}
diff --git a/src/Aliment.purs b/src/Aliment.purs
deleted file mode 100644
index e7c4fc8..0000000
--- a/src/Aliment.purs
+++ /dev/null
@@ -1,13 +0,0 @@
-module Aliment where
-
-all :: Array Aliment
-all =
- [ { name: "Oignon", gi: 15 }
- , { name: "Olive", gi: 15 }
- , { name: "Haricot rouge", gi: 35 }
- ]
-
-type Aliment =
- { name :: String
- , gi :: Int
- }
diff --git a/src/Food.purs b/src/Food.purs
new file mode 100644
index 0000000..b1e3d29
--- /dev/null
+++ b/src/Food.purs
@@ -0,0 +1,25 @@
+module Food
+ ( Aliment
+ , all
+ , glycemicLoad
+ ) where
+
+import Data.Int (toNumber)
+import Prelude
+
+type Aliment =
+ { name :: String
+ , glycemicIndex :: Int
+ , carbohydrates :: Int -- for 100 grams
+ }
+
+glycemicLoad :: Aliment -> Number
+glycemicLoad aliment = toNumber aliment.glycemicIndex * toNumber aliment.carbohydrates / 100.0
+
+all :: Array Aliment
+all =
+ [ { name: "oignon", glycemicIndex: 15, carbohydrates: 9 }
+ , { name: "olive", glycemicIndex: 15, carbohydrates: 6 }
+ , { name: "haricot rouge", glycemicIndex: 35, carbohydrates: 24 }
+ , { name: "haricot blanc", glycemicIndex: 35, carbohydrates: 13 }
+ ]
diff --git a/src/Format.purs b/src/Format.purs
new file mode 100644
index 0000000..22593e5
--- /dev/null
+++ b/src/Format.purs
@@ -0,0 +1,29 @@
+module Format where
+
+import Data.Int (toNumber, fromNumber)
+import Data.Array (replicate)
+import Data.Maybe (fromMaybe)
+import Data.String (length, joinWith)
+import Math (round, trunc, pow)
+
+import Prelude
+
+number :: Int -> Number -> String
+number decimalLength num = formattedIntegerPart <> formattedDecimalPart
+ where
+ formattedIntegerPart =
+ (if decimalLength > 0 then trunc num else round num)
+ # fromNumber
+ # fromMaybe 0
+ # show
+
+ formattedDecimalPart =
+ if decimalLength > 0 then
+ ((num - trunc num) * pow 10.0 (toNumber decimalLength))
+ # round
+ # fromNumber
+ # fromMaybe 0
+ # show
+ # \str -> "," <> (joinWith "" $ replicate (decimalLength - length str) "0") <> str
+ else
+ ""
diff --git a/src/Page.purs b/src/Page.purs
index 83e4d73..76cdf8d 100644
--- a/src/Page.purs
+++ b/src/Page.purs
@@ -1,12 +1,14 @@
module Page where
-import Prelude
import Data.Maybe (Maybe(..))
import Halogen as H
import Halogen.HTML as HH
+import Halogen.HTML.Properties as HP
+import Prelude
-import Aliment as Aliment
-import Aliment (Aliment)
+import Food (Aliment)
+import Food as Food
+import Format as Format
type State = Unit
@@ -27,19 +29,48 @@ component =
render state =
HH.div
[]
- [ HH.h1 [] [ HH.text "Sucre" ]
+ [ HH.h1 [] [ HH.text "Glycémie" ]
, HH.ul
- []
- (map renderAliment Aliment.all)
+ [ HP.class_ $ HH.ClassName "aliments" ]
+ ([renderTitle] <> (map renderAliment Food.all))
]
eval :: Query ~> H.ComponentDSL State Query Message m
eval = case _ of
NoOp next -> pure next
+renderTitle :: H.ComponentHTML Query
+renderTitle =
+ HH.li
+ [ HP.class_ $ HH.ClassName "title" ]
+ [ HH.div
+ []
+ [ HH.text "Aliment" ]
+ , HH.div
+ []
+ [ HH.text "Index glycémique" ]
+ , HH.div
+ []
+ [ HH.text "Glucides pour 100 g" ]
+ , HH.div
+ []
+ [ HH.text "Charge glycémique" ]
+ ]
+
renderAliment :: Aliment -> H.ComponentHTML Query
renderAliment aliment =
HH.li
[]
- [ HH.text (aliment.name <> ", index glycémique: " <> (show aliment.gi))
+ [ HH.div
+ []
+ [ HH.text aliment.name ]
+ , HH.div
+ [ HP.class_ $ HH.ClassName "number" ]
+ [ HH.text (show aliment.glycemicIndex) ]
+ , HH.div
+ [ HP.class_ $ HH.ClassName "number" ]
+ [ HH.text (show aliment.carbohydrates) ]
+ , HH.div
+ [ HP.class_ $ HH.ClassName "number" ]
+ [ HH.text (Format.number 2 <<< Food.glycemicLoad $ aliment) ]
]
diff --git a/stop b/stop
index 99d5355..651a4cf 100755
--- a/stop
+++ b/stop
@@ -1,3 +1,3 @@
#!/bin/sh
pkill webfsd
-tmux kill-session -t sucre
+tmux kill-session -t glycemie
diff --git a/test/Main.purs b/test/Main.purs
new file mode 100644
index 0000000..5ebbb03
--- /dev/null
+++ b/test/Main.purs
@@ -0,0 +1,27 @@
+module Test.Main where
+
+import Prelude
+import Control.Monad.Aff (later')
+import Control.Monad.Eff (Eff)
+import Test.Spec (describe, it)
+import Test.Spec.Assertions (shouldEqual)
+import Test.Spec.Reporter.Console (consoleReporter)
+import Test.Spec.Runner (RunnerEffects, run)
+
+import Format as Format
+
+main :: Eff (RunnerEffects ()) Unit
+main = run [consoleReporter] do
+
+ describe "Format" do
+ it "format numbers" $ do
+ (Format.number 0 0.0) `shouldEqual` "0"
+ (Format.number 0 (-0.0)) `shouldEqual` "0"
+ (Format.number 0 8.0) `shouldEqual` "8"
+ (Format.number 0 (-4.0)) `shouldEqual` "-4"
+ (Format.number 2 25.0) `shouldEqual` "25,00"
+ (Format.number 5 25.0) `shouldEqual` "25,00000"
+ (Format.number 5 25.13) `shouldEqual` "25,13000"
+ (Format.number 1 25.13) `shouldEqual` "25,1"
+ (Format.number 1 25.15) `shouldEqual` "25,1"
+ (Format.number 1 25.19) `shouldEqual` "25,2"