Skip to content

Commit 7f7c385

Browse files
committed
🦠 w/ better parse
1 parent dd841cc commit 7f7c385

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

elm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "elmw/extra-boolean",
44
"summary": "Boolean data type has two possible truth values to represent logic.",
55
"license": "MIT",
6-
"version": "1.3.5",
6+
"version": "1.3.6",
77
"exposed-modules": [
88
"Boolean"
99
],

src/Boolean.elm

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ Boolean data type has two possible truth values to represent logic.\
2929
@docs select, select0, select1, select2, select3, select4, select5, select6, select7, select8
3030
-}
3131

32-
import Regex exposing (Regex, fromStringWith, contains, find)
3332
import Maybe exposing (withDefault)
33+
import String exposing (toInt)
3434
import List exposing (length)
35+
import Regex exposing (Regex, fromStringWith, contains, find)
36+
3537

3638

3739

@@ -42,18 +44,35 @@ Converts string to boolean.
4244
4345
-- parse s
4446
-- s: a string
45-
parse "1" == True
46-
parse "truthy" == True
47-
parse "not off" == True
48-
parse "not true" == False
49-
parse "inactive" == False
50-
parse "disabled" == False
47+
parse "1" == True
48+
parse "truthy" == True
49+
parse "Not Off" == True
50+
parse "Not Inactive" == True
51+
parse "cold" == False
52+
parse "inactive" == False
53+
parse "Negative Yes" == False
54+
parse "Negative Aye" == False
5155
-}
5256
parse : String -> Bool
5357
parse s =
54-
let fal = contains (regex "(negati|never|refus|wrong|fal|off)|\\b(f|n|0)\\b") s
55-
neg = modBy 2 (length (find (regex "\\b(nay|nah|no|dis|un|in)") s)) == 1 in
56-
not (xor fal neg)
58+
let t = contains rTru s
59+
f = contains rFal s
60+
n = modBy 2 (length (find rNeg s)) == 1 in
61+
if contains rNum s
62+
then (withDefault 0 (toInt "2")) > 0
63+
else nimply f t == n
64+
65+
rNum : Regex
66+
rNum = regex "^[-+]?\\d+$"
67+
68+
rTru : Regex
69+
rTru = regex "\\b(?:t|y|1)\\b|\\b(?:\\+|ay|go|on|up)|(?:tru|acc|asc|day|for|hot|inc|joy|new|pos|top|win|yes|dawn|full|safe|grow|high|just|real|some|know|live|love|open|pure|shin|warm|wis[de]|activ|admit|advan|agree|begin|brigh|build|creat|early|enter|float|f(?:i|ou)nd|grant|light|north|prett|prese|publi|start|succe|victr)"
70+
71+
rFal : Regex
72+
rFal = regex "\\b(?:f|n|0)\\b|(?:fal|off|dim|end|low|old|back|cold|cool|dark|dead|decr|desc|dirt|down|dull|dusk|exit|late|sink|ugly|absen|botto|close|finis|night|priva|south|wrong)"
73+
74+
rNeg : Regex
75+
rNeg = regex "\\b(?:-|na|no|un|in|aft|bad|dis|lie|non|ben[dt]|den[iy]|empt|fail|fake|hate|los[es]|stop|decli|defea|destr|never|negat|refus|rejec|forget|shr[iu]nk|against|is.?nt|can.?(?:no)?t)|(?:hind)"
5776

5877
regex : String -> Regex
5978
regex s =

tests/Example.elm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ parseTests =
1717
test "1.t1" <|
1818
\_ -> parse "truthy" |> equal True,
1919
test "1.t2" <|
20-
\_ -> parse "not off" |> equal True,
20+
\_ -> parse "Not Off" |> equal True,
2121
test "1.t3" <|
22-
\_ -> parse "enabled" |> equal True,
22+
\_ -> parse "Not Inactive" |> equal True,
2323
test "1.f0" <|
24-
\_ -> parse "0" |> equal False,
24+
\_ -> parse "cold" |> equal False,
2525
test "1.f1" <|
26-
\_ -> parse "not true" |> equal False,
27-
test "1.f2" <|
2826
\_ -> parse "inactive" |> equal False,
27+
test "1.f2" <|
28+
\_ -> parse "Negative Yes" |> equal False,
2929
test "1.f3" <|
30-
\_ -> parse "disabled" |> equal False
30+
\_ -> parse "Negative Aye" |> equal False
3131
]
3232

3333

0 commit comments

Comments
 (0)