-
Notifications
You must be signed in to change notification settings - Fork 30
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
Docs for most public functions for formatting DateTimes and Numbers #73
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c4e1183
docs for most public functions for formatting/printing DateTimes and …
ntwilson 12afbaa
update the changelog with a link to this PR
ntwilson 8adaf11
update the momentjs link to their detailed formatting docs, and corre…
ntwilson 96d5488
Merge remote-tracking branch 'upstream/main' into update-docs
ntwilson d9dd31b
update the docs for astral plane characters
ntwilson ae6558d
address review comments
ntwilson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,17 @@ import Text.Parsing.Parser.Combinators as PC | |
import Text.Parsing.Parser.String as PS | ||
|
||
|
||
-- | Defines a format for printing/parsing numbers. | ||
-- | | ||
-- | `comma`: use a ',' for a thousands separator | ||
-- | | ||
-- | `before`: the minimum number of characters to print before the decimal point | ||
-- | | ||
-- | `after`: the total number of characters to print after the decimal point | ||
-- | | ||
-- | `abbreviations`: "31600.0" → "32K"; "31600000.0" → "32M" | ||
-- | | ||
-- | `sign`: always print a sign, including a `+` for positive numbers | ||
newtype Formatter = Formatter | ||
{ comma ∷ Boolean | ||
, before ∷ Int | ||
|
@@ -51,6 +62,8 @@ instance showFormatter ∷ Show Formatter where | |
|
||
derive instance eqFormatter ∷ Eq Formatter | ||
|
||
-- | The format string representation of a `Formatter`. | ||
-- | The interpretation of the format string inspired by [numeral.js](http://numeraljs.com/#format) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing applies here as to the comment about moment.js. Also this information is news to me, I didn't know where it came from 😄 (I didn't implement this module). |
||
printFormatter ∷ Formatter → String | ||
printFormatter (Formatter f) = | ||
(if f.sign then "+" else "") | ||
|
@@ -60,6 +73,8 @@ printFormatter (Formatter f) = | |
<> (repeat "0" f.after) | ||
<> (if f.abbreviations then "a" else "") | ||
|
||
-- | Attempt to parse a `String` as a `Formatter`, | ||
-- | using an interpretation inspired by [numeral.js](http://numeraljs.com/#format) | ||
parseFormatString ∷ String → Either String Formatter | ||
parseFormatString = runP formatParser | ||
|
||
|
@@ -87,7 +102,7 @@ formatParser = do | |
-- means of showing an integer potentially larger than +/- 2 billion. | ||
foreign import showNumberAsInt :: Number -> String | ||
|
||
-- | Formats a number according to the format object provided. | ||
-- | Format a `Number` according to the `Formatter` provided. | ||
-- | Due to the nature of floating point numbers, may yield unpredictable results for extremely | ||
-- | large or extremely small numbers, such as numbers whose absolute values are ≥ 1e21 or ≤ 1e-21, | ||
-- | or when formatting with > 20 digits after the decimal place. | ||
|
@@ -155,9 +170,13 @@ format (Formatter f) num = | |
<> (if leftover > 0.0 then leftoverWithZeros else "")) | ||
|
||
|
||
-- | Attempt to parse a `String` as a `Number` according to the format defined in the | ||
-- | given `Formatter`. | ||
unformat ∷ Formatter → String → Either String Number | ||
unformat = runP <<< unformatParser | ||
|
||
-- | A `ParserT` for `String`s that parses a `Number` | ||
-- | according to the format defined in the given `Formatter`. | ||
unformatParser ∷ Formatter → P.Parser String Number | ||
unformatParser (Formatter f) = do | ||
minus ← PC.optionMaybe $ PC.try $ PS.string "-" | ||
|
@@ -232,10 +251,17 @@ unformatParser (Formatter f) = do | |
* sign | ||
* (before + after / Math.pow 10.0 (Int.toNumber f.after)) | ||
|
||
-- | Format a Number according to the format defined in the given format string. | ||
-- | If the format string fails to parse, will return a `Left` value. | ||
-- | The interpretation of the format string is inspired by [numeral.js](http://numeraljs.com/#format) | ||
formatNumber ∷ String → Number → Either String String | ||
formatNumber pattern number = | ||
parseFormatString pattern <#> flip format number | ||
|
||
-- | Attempt to parse a `String` as a `Number` according to the format defined in the | ||
-- | given format string. Returns a `Left` value if the given format string fails to parse, or | ||
-- | if the number string fails to parse according to the format. | ||
-- | The interpretation of the format string is inspired by [numeral.js](http://numeraljs.com/#format) | ||
unformatNumber ∷ String → String → Either String Number | ||
unformatNumber pattern str = | ||
parseFormatString pattern >>= flip unformat str | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could use a spot check on that statement. From what I could tell, the only situation that would return a
Left
value was if the string is empty (fromList.some
on 164), but literally anything else would parse as aFormatter
and just havePlaceholder
for any unrecognized portions of the input string.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm, I ran this:
and got
So any advice what this message should say?