Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse formatted number string to number #41

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

praveentiru
Copy link
Contributor

This PR fixes #35. It is a draft PR to implement new feature to convert a formatted string to Maybe Float.

There is still some work in docs. Please review the approach and let me know your thoughts.

Copy link
Owner

@cuducos cuducos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once more, many thanks for your extra hand here 🎉

I made some comments to try to get to a more flexible implementation — what do you think?

src/FormatNumber/Parser.elm Outdated Show resolved Hide resolved
src/FormatNumber/Parser.elm Outdated Show resolved Hide resolved
src/FormatNumber/Parser.elm Outdated Show resolved Hide resolved
src/FormatNumber/Parser.elm Outdated Show resolved Hide resolved
src/FormatNumber/Parser.elm Outdated Show resolved Hide resolved
src/FormatNumber/Parser.elm Outdated Show resolved Hide resolved
1. Added new function joinParts to build number from parts
2. Cleaned up code in parseString as per feedback
value based on locale
1. Add new function parse to expose new feature
2. Add new function parseString in Parser.elm to implement parse
3. Add new function joinParts to join the number parts into float
@praveentiru
Copy link
Contributor Author

@cuducos I have never used rebase on command line. I believe I have messed it up. Please squash the commits and merge if all changes are ok.

src/FormatNumber.elm Show resolved Hide resolved
Comment on lines +156 to +157
parse { base | thousandSeparator = ",", negativePrefix = "-" } "-100#000"
--> Just -100000
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the # in the -100#000 example? If # is not a thousand separators, this example seems wrong, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It is wrong. But, this is the problem with current approach of cleaning integer part of non-digits. Thousand separator does not have any value in parsing.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can opt for a strategy to validate every every non-digit is one of the expected tokens (negative prefix or suffix, zero prefix or suffix, separators):

  1. Split the value by the regex \d+
  2. Check if the resulting list contains only expected tokens

src/FormatNumber/Parser.elm Outdated Show resolved Hide resolved
src/FormatNumber/Parser.elm Outdated Show resolved Hide resolved
src/FormatNumber/Parser.elm Show resolved Hide resolved
Comment on lines +447 to +448
{-| Given a `Locale` parses a `String` into a `Maybe Float`
-}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need some examples here, both for documentation and for tests ; )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason I did not add any tests here is because parse function in FormatNumber.elm is just calls this function and does nothing else. I thought it would be repetition.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parse function in FormatNumber.elm is just calls this function

So, what about moving this function to FormatNumber.elm?

src/FormatNumber/Parser.elm Show resolved Hide resolved
onlyDigits =
Regex.replace notDigits (\_ -> "")

splitValue : String -> List String
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think we should use String -> (String, String) here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I change this, then I can move entire logic of joinParts into the parseString. I like the current structure as it cleanly segregates that out of parseString and allow me to test it separately.

Let me know if you still want it changed. I will do it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think making impossible states impossible worth the job : )

src/FormatNumber/Parser.elm Outdated Show resolved Hide resolved
src/FormatNumber/Parser.elm Outdated Show resolved Hide resolved
@praveentiru
Copy link
Contributor Author

@cuducos I believe there is a major flaw that I was not thinking about that is exposed by example: parse { base | thousandSeparator = ",", negativePrefix = "-" } "-100#000"

I have not built any code to validate the string against locale before parsing. I am working on a function to do the same. Will take little time but, I am hoping that would be a better approach. I will create a separate branch for the same so, we are able to evaluate that approach while building on the current code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature request/question: Reversing the formatting of a number?
2 participants