@@ -8,9 +8,9 @@ and `int()` functions for parsing strings into float and integer values. Use
88` lenient_parse ` when ` int.parse ` and ` float.parse ` from the Gleam
99[ stdlib] ( https://github.com/gleam-lang/stdlib ) are too strict.
1010
11- - ` float("3.14") ` -> ` "3.14" |> lenient_parse.to_float `
12- - ` int("42") ` -> ` "42" |> lenient_parse.to_int`
13- - ` int("1010", base=2) ` -> ` "1010" |> lenient_parse.to_int_with_base(2)`
11+ - ` float("3.14") ` -> ` lenient_parse.to_float( "3.14") `
12+ - ` int("42") ` -> ` lenient_parse.to_int("42") `
13+ - ` int("1010", base=2) ` -> ` lenient_parse.to_int_with_base("1010", 2) `
1414
1515## Installation
1616
@@ -23,50 +23,65 @@ gleam add lenient_parse
2323``` gleam
2424import gleam/float
2525import gleam/int
26- import gleam/io
2726import gleam/list
2827import lenient_parse
2928
3029pub fn main() {
3130 // Parse a string containing an integer value into a float
3231
33- "1" |> lenient_parse.to_float |> io.debug // Ok(1.0)
34- "1" |> float.parse |> io.debug // Error(Nil)
32+ let _ = echo lenient_parse.to_float("1")
33+ // Ok(1.0)
34+ let _ = echo float.parse("1")
35+ // Error(Nil)
3536
3637 // Parse a string containing a negative float
3738
38- "-5.001" |> lenient_parse.to_float |> io.debug // Ok(-5.001)
39- "-5.001" |> float.parse |> io.debug // Ok(-5.001)
39+ let _ = echo lenient_parse.to_float("-5.001")
40+ // Ok(-5.001)
41+ let _ = echo float.parse("-5.001")
42+ // Ok(-5.001)
4043
4144 // Parse a string containing a complex float with scientific notation
4245
43- "-1_234.567_8e-2" |> lenient_parse.to_float |> io.debug // Ok(-12.345678)
44- "-1_234.567_8e-2" |> float.parse |> io.debug // Error(Nil)
46+ let _ = echo lenient_parse.to_float("-1_234.567_8e-2")
47+ // Ok(-12.345678)
48+ let _ = echo float.parse("-1_234.567_8e-2")
49+ // Error(Nil)
4550
4651 // Parse a string containing an integer
4752
48- "123" |> lenient_parse.to_int |> io.debug // Ok(123)
49- "123" |> int.parse |> io.debug // Ok(123)
53+ let _ = echo lenient_parse.to_int("123")
54+ // Ok(123)
55+ let _ = echo int.parse("123")
56+ // Ok(123)
5057
5158 // Parse a string containing a negative integer with surrounding whitespace
5259
53- " -123 " |> lenient_parse.to_int |> io.debug // Ok(-123)
54- " -123 " |> int.parse |> io.debug // Error(Nil)
60+ let _ = echo lenient_parse.to_int(" -123 ")
61+ // Ok(-123)
62+ let _ = echo int.parse(" -123 ")
63+ // Error(Nil)
5564
5665 // Parse a string containing an integer with underscores
5766
58- "1_000_000" |> lenient_parse.to_int |> io.debug // Ok(1000000)
59- "1_000_000" |> int.parse |> io.debug // Error(Nil)
67+ let _ = echo lenient_parse.to_int("1_000_000")
68+ // Ok(1000000)
69+ let _ = echo int.parse("1_000_000")
70+ // Error(Nil)
6071
6172 // Parse a string containing a binary number with underscores
6273
63- "1000_0000" |> lenient_parse.to_int_with_base(2) |> io.debug // Ok(128)
64- "1000_0000" |> int.base_parse(2) |> io.debug // Error(Nil)
74+ let _ = echo lenient_parse.to_int_with_base("1000_0000", 2)
75+ // Ok(128)
76+ let _ = echo int.base_parse("1000_0000", 2)
77+ // Error(Nil)
6578
6679 // Parse a string containing a hexadecimal number with underscores
6780
68- "DEAD_BEEF" |> lenient_parse.to_int_with_base(16) |> io.debug // Ok(3735928559)
69- "DEAD_BEEF" |> int.base_parse(16) |> io.debug // Error(Nil)
81+ let _ = echo lenient_parse.to_int_with_base("DEAD_BEEF", 16)
82+ // Ok(3735928559)
83+ let _ = echo int.base_parse("DEAD_BEEF", 16)
84+ // Error(Nil)
7085
7186 // Use base 0 to automatically detect the base when parsing strings with prefix indicators
7287
@@ -75,20 +90,18 @@ pub fn main() {
7590 "0O3507", "0X7a9", "0b11011111011",
7691 ]
7792
78- dates
79- |> list.map(lenient_parse.to_int_with_base(_, 0))
80- |> io.debug()
93+ let _ = echo list.map(dates, lenient_parse.to_int_with_base(_, 0))
8194 // [Ok(1776), Ok(1941), Ok(1865), Ok(1980), Ok(1969), Ok(1929), Ok(1863), Ok(1961), Ok(1787)]
8295
83- dates
84- |> list.map(int.base_parse(_, 0))
85- |> io.debug()
96+ let _ = echo list.map(dates, int.base_parse(_, 0))
8697 // [Error(Nil), Error(Nil), Error(Nil), Error(Nil), Error(Nil), Error(Nil), Error(Nil), Error(Nil), Error(Nil)]
8798
8899 // Nice errors
89100
90- "12.3e_3" |> lenient_parse.to_float |> io.debug // Error(InvalidUnderscorePosition(5))
91- "12.3e_3" |> float.parse |> io.debug // Error(Nil)
101+ let _ = echo lenient_parse.to_float("12.3e_3")
102+ // Error(InvalidUnderscorePosition(5))
103+ let _ = echo float.parse("12.3e_3")
104+ // Error(Nil)
92105}
93106```
94107
0 commit comments