Skip to content

Commit

Permalink
Use "parse" over "coerce" (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephTLyons committed Nov 4, 2024
1 parent 5e5d60d commit 9a3c3f6
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 250 deletions.
20 changes: 14 additions & 6 deletions src/lenient_parse.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import lenient_parse/internal/coerce.{parse_float, parse_int}

import gleam/float
import gleam/int
import gleam/result
import lenient_parse/internal/parse
import parse_error.{type ParseError}
import parse_error.{type ParseError, GleamFloatParseError, GleamIntParseError}

/// 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.
///
Expand All @@ -23,8 +24,15 @@ import parse_error.{type ParseError}
/// lenient_parse.to_float("abc") // -> Error(InvalidCharacter("a", 0))
/// ```
pub fn to_float(text: String) -> Result(Float, ParseError) {
let text = text |> parse.parse_float
use text <- result.try(text)
let res = text |> float.parse |> result.replace_error(GleamFloatParseError)
use <- result.lazy_or(res)

text
|> parse.to_float(parse_float)
|> int.parse
|> result.map(int.to_float)
|> result.replace_error(GleamIntParseError)
}

/// Converts a string to an integer using a more lenient parsing method than gleam's `int.parse()`.
Expand All @@ -44,6 +52,6 @@ pub fn to_float(text: String) -> Result(Float, ParseError) {
/// lenient_parse.to_int("abc") // -> Error(InvalidCharacter("a", 0))
/// ```
pub fn to_int(text: String) -> Result(Int, ParseError) {
text
|> parse.to_int(parse_int)
use text <- result.try(text |> parse.parse_int)
text |> int.parse |> result.replace_error(GleamIntParseError)
}
205 changes: 0 additions & 205 deletions src/lenient_parse/internal/coerce.gleam

This file was deleted.

Loading

0 comments on commit 9a3c3f6

Please sign in to comment.