Skip to content

Commit

Permalink
Update doc comment
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephTLyons committed Oct 10, 2024
1 parent df93049 commit 94ce7dc
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/lenient_parse.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import gleam/string

/// Converts a string to a float using a more lenient parsing method than gleam's `float.parse()`. It behaves similarly to Python's `float()` built-in function.
///
/// It attempts to parse the input string in the following order:
/// 1. As a standard float
/// 2. As an integer (which is then converted to a float)
/// 3. As a float with leading or trailing decimal point
///
/// ## Examples
///
/// ```gleam
/// lenient_parse.to_float("3.14") // -> Ok(3.14)
/// lenient_parse.to_float("42") // -> Ok(42.0)
/// lenient_parse.to_float(".5") // -> Ok(0.5)
/// lenient_parse.to_float("2.") // -> Ok(2.0)
/// lenient_parse.to_float("dog") // -> Error(Nil)
/// lenient_parse.to_float("1.001") // -> Ok(1.001)
/// lenient_parse.to_float("1") // -> Ok(1.0)
/// lenient_parse.to_float("1.") // -> Ok(1.0)
/// lenient_parse.to_float("1.0") // -> Ok(1.0)
/// lenient_parse.to_float(".1") // -> Ok(0.1)
/// lenient_parse.to_float("0.1") // -> Ok(0.1)
/// lenient_parse.to_float("+123.321") // -> Ok(123.321)
/// lenient_parse.to_float("-123.321") // -> Ok(-123.321)
/// lenient_parse.to_float(" 1.0 ") // -> Ok(1.0)
/// lenient_parse.to_float(" ") // -> Error(Nil)
/// lenient_parse.to_float("") // -> Error(Nil)
/// lenient_parse.to_float("abc") // -> Error(Nil)
/// ```
pub fn to_float(text: String) -> Result(Float, Nil) {
let text = text |> string.trim
Expand All @@ -33,7 +35,6 @@ pub fn to_float(text: String) -> Result(Float, Nil) {
_, _ -> Error(Nil)
}
}
// TODO: Update doc comment with rules and examples
// TODO: README
// TODO: GitHub repo description
// TODO: gleam.toml
Expand Down

0 comments on commit 94ce7dc

Please sign in to comment.