aboutsummaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/src/Dom.js (renamed from js/Dom.js)0
-rw-r--r--js/src/Dom.purs (renamed from js/Dom.purs)0
-rw-r--r--js/src/EditableNumber.purs (renamed from js/EditableNumber.purs)20
-rw-r--r--js/src/Main.purs (renamed from js/Main.purs)7
-rw-r--r--js/src/Number.purs22
-rw-r--r--js/src/Parser.purs (renamed from js/Parser.purs)0
-rw-r--r--js/test/Main.purs61
7 files changed, 89 insertions, 21 deletions
diff --git a/js/Dom.js b/js/src/Dom.js
index 6835c1f..6835c1f 100644
--- a/js/Dom.js
+++ b/js/src/Dom.js
diff --git a/js/Dom.purs b/js/src/Dom.purs
index a71cda7..a71cda7 100644
--- a/js/Dom.purs
+++ b/js/src/Dom.purs
diff --git a/js/EditableNumber.purs b/js/src/EditableNumber.purs
index eb5ddb0..6a6e3a8 100644
--- a/js/EditableNumber.purs
+++ b/js/src/EditableNumber.purs
@@ -1,14 +1,10 @@
module EditableNumber
( NumberElem
, set
- , formatNumber
) where
import Control.Monad.Eff (Eff)
-import Data.Int (round, toNumber, pow) as Int
import Data.Maybe (Maybe(..))
-import Data.String (Pattern(..), Replacement(..))
-import Data.String (replace) as String
import DOM (DOM)
import DOM.HTML (window) as DOM
import DOM.HTML.Types (htmlDocumentToDocument) as DOM
@@ -18,10 +14,10 @@ import DOM.Node.Element (setClassName, setAttribute) as DOM
import DOM.Node.Node (textContent) as DOM
import DOM.Node.Types (Element, Node)
import DOM.Node.Types (elementToNode, textToNode) as DOM
-import Math (round) as Math
import Prelude
import Dom (replaceElement, appendNodes) as Dom
+import Number (format) as Number
import Parser (TextWithNumber)
import Parser (textWithNumber) as Parser
@@ -56,17 +52,5 @@ numberElem number = do
document <- DOM.htmlDocumentToDocument <$> (DOM.window >>= DOM.document)
container <- DOM.createElement "input" document
DOM.setClassName "number" container
- DOM.setAttribute "value" (formatNumber number) container
+ DOM.setAttribute "value" (Number.format number) container
pure container
-
-formatNumber :: Number -> String
-formatNumber number =
- if Math.round number == number then
- show (Int.round number)
- else
- String.replace (Pattern ".") (Replacement ",") (show (roundAt 1 number))
-
-roundAt :: Int -> Number -> Number
-roundAt at n =
- let exp = Int.toNumber (Int.pow 10 at)
- in Math.round (n * exp) / exp
diff --git a/js/Main.purs b/js/src/Main.purs
index 062eb1d..42db131 100644
--- a/js/Main.purs
+++ b/js/src/Main.purs
@@ -6,13 +6,14 @@ import Data.Maybe (Maybe(..))
import Data.Traversable (sequence, sequence_) as Traversable
import DOM (DOM)
import DOM.Node.ParentNode (QuerySelector(..))
-import DOM.Node.Types (Node)
import DOM.Node.Types (elementToNode) as DOM
+import DOM.Node.Types (Node)
import Prelude
import Dom (selectElement, selectElementsFrom, onInput, setValue, selectElementFrom) as Dom
import EditableNumber (NumberElem)
-import EditableNumber (set, formatNumber) as EditableNumber
+import EditableNumber (set) as EditableNumber
+import Number (format) as Number
import Parser (number) as Parser
main :: forall e. Eff (dom :: DOM | e) Unit
@@ -43,7 +44,7 @@ onInput numberElems { elem, number } = do
# map (\ne -> do
inputNode <- Dom.selectElementFrom ne.elem (QuerySelector "input")
case inputNode of
- Just node -> Dom.setValue (EditableNumber.formatNumber (ne.number * mul)) node
+ Just node -> Dom.setValue (Number.format (ne.number * mul)) node
_ -> pure unit
)
# Traversable.sequence_
diff --git a/js/src/Number.purs b/js/src/Number.purs
new file mode 100644
index 0000000..0403f19
--- /dev/null
+++ b/js/src/Number.purs
@@ -0,0 +1,22 @@
+module Number
+ ( format
+ , roundAt
+ ) where
+
+import Data.Int (round, toNumber, pow) as Int
+import Data.String (Pattern(..), Replacement(..))
+import Data.String (replace) as String
+import Math (round) as Math
+import Prelude
+
+format :: Number -> String
+format number =
+ if Math.round number == number then
+ show (Int.round number)
+ else
+ String.replace (Pattern ".") (Replacement ",") (show (roundAt 1 number))
+
+roundAt :: Int -> Number -> Number
+roundAt at n =
+ let exp = Int.toNumber (Int.pow 10 at)
+ in Math.round (n * exp) / exp
diff --git a/js/Parser.purs b/js/src/Parser.purs
index cad9f1b..cad9f1b 100644
--- a/js/Parser.purs
+++ b/js/src/Parser.purs
diff --git a/js/test/Main.purs b/js/test/Main.purs
new file mode 100644
index 0000000..e23f9e2
--- /dev/null
+++ b/js/test/Main.purs
@@ -0,0 +1,61 @@
+module Test.Main
+ ( main
+ ) where
+
+import Control.Monad.Eff (Eff)
+import Data.Maybe (Maybe(Just, Nothing))
+import Prelude
+import Test.Spec (describe, it)
+import Test.Spec.Assertions (shouldEqual)
+import Test.Spec.Reporter.Console (consoleReporter)
+import Test.Spec.Runner (RunnerEffects, run)
+
+import Number (roundAt, format) as Number
+import Parser (TextWithNumber)
+import Parser (textWithNumber, number) as Parser
+
+main :: Eff (RunnerEffects ()) Unit
+main = run [consoleReporter] do
+
+ describe "Number" do
+ it "rounds numbers" $ do
+ (Number.roundAt 0 0.0) `shouldEqual` (0.0)
+ (Number.roundAt 5 0.0) `shouldEqual` (0.0)
+ (Number.roundAt 0 12.3456) `shouldEqual` (12.0)
+ (Number.roundAt 1 12.3456) `shouldEqual` (12.3)
+ (Number.roundAt 2 12.3456) `shouldEqual` (12.35)
+ (Number.roundAt 3 12.3456) `shouldEqual` (12.346)
+ (Number.roundAt 4 12.3456) `shouldEqual` (12.3456)
+ (Number.roundAt 5 12.3456) `shouldEqual` (12.3456)
+
+ it "formats numbers" $ do
+ (Number.format 0.0) `shouldEqual` "0"
+ (Number.format 0.1) `shouldEqual` "0,1"
+ (Number.format 12.3456) `shouldEqual` "12,3"
+ (Number.format 12.9) `shouldEqual` "12,9"
+ (Number.format 12.99) `shouldEqual` "13,0"
+ (Number.format 123456.0) `shouldEqual` "123456"
+ (Number.format 1234.5678) `shouldEqual` "1234,6"
+
+ describe "Parser" do
+ it "parses number" $ do
+ (Parser.number "") `shouldEqual` Nothing
+ (Parser.number "auie") `shouldEqual` Nothing
+ (Parser.number "13.8auie") `shouldEqual` Nothing
+ (Parser.number "13.") `shouldEqual` Nothing
+ (Parser.number ".8") `shouldEqual` Nothing
+ (Parser.number "13") `shouldEqual` (Just 13.0)
+ (Parser.number "13,8") `shouldEqual` (Just 13.8)
+ (Parser.number "13.8") `shouldEqual` (Just 13.8)
+ (Parser.number "123 456") `shouldEqual` Nothing
+
+ it "parses textWithNumber" $ do
+ map showTwn (Parser.textWithNumber "") `shouldEqual` Nothing
+ map showTwn (Parser.textWithNumber "0") `shouldEqual` Just (showTwn { begin: "", number: 0.0, end: "" })
+ map showTwn (Parser.textWithNumber "13,8") `shouldEqual` Just (showTwn { begin: "", number: 13.8, end: "" })
+ map showTwn (Parser.textWithNumber "begin 0 end") `shouldEqual` Just (showTwn { begin: "begin ", number: 0.0, end: " end" })
+ map showTwn (Parser.textWithNumber "14end") `shouldEqual` Just (showTwn { begin: "", number: 14.0, end: "end" })
+ map showTwn (Parser.textWithNumber "begin14") `shouldEqual` Just (showTwn { begin: "begin", number: 14.0, end: "" })
+
+showTwn :: TextWithNumber -> String
+showTwn { begin, number, end } = begin <> show number <> end