Skip to content

Moment.js-style escaping of literal text in date/time formats #39

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ This is just subset of format/parse string from moment.js library. Currently sup
+ `SS`
+ `S`

Literal text can be escaped by enclosing it with square brackets: `[Today is] MMM D`

## Documentation

Module documentation is published on Pursuit: [http://pursuit.purescript.org/packages/purescript-formatters](http://pursuit.purescript.org/packages/purescript-formatters)
13 changes: 12 additions & 1 deletion src/Data/Formatter/DateTime.purs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ data FormatterCommand
| MillisecondsShort
| MillisecondsTwoDigits
| Placeholder String
| Escaped String

derive instance eqFormatterCommand ∷ Eq FormatterCommand
derive instance ordFormatterCommand ∷ Ord FormatterCommand
Expand Down Expand Up @@ -104,6 +105,7 @@ printFormatterCommand = case _ of
MillisecondsTwoDigits → "SS"
Milliseconds → "SSS"
Placeholder s → s
Escaped s → "[" <> s <> "]"

printFormatter ∷ Formatter → String
printFormatter = foldMap printFormatterCommand
Expand All @@ -118,8 +120,15 @@ placeholderContent =
# Array.some
<#> Str.fromCharArray

escapedContent :: P.Parser String String
escapedContent = PC.try $ PC.between (PS.string "[") (PS.string "]") escapedString
where
escapedString :: P.Parser String String
escapedString = (Array.many $ PS.satisfy (_ /= ']')) <#> Str.fromCharArray

formatterCommandParser ∷ P.Parser String FormatterCommand
formatterCommandParser = (PC.try <<< PS.string) `oneOfAs`
formatterCommandParser = (Escaped <$> escapedContent) <|>
(PC.try <<< PS.string) `oneOfAs`
[ Tuple "YYYY" YearFull
, Tuple "YY" YearTwoDigits
, Tuple "Y" YearAbsolute
Expand Down Expand Up @@ -186,6 +195,7 @@ formatCommand dt@(DT.DateTime d t) = case _ of
MillisecondsShort → show $ (_ / 100) $ fromEnum $ T.millisecond t
MillisecondsTwoDigits → padSingleDigit $ (_ / 10) $ fromEnum $ T.millisecond t
Placeholder s → s
Escaped s → s

--TODO we need leftpad here

Expand Down Expand Up @@ -384,6 +394,7 @@ unformatCommandParser = case _ of
Milliseconds → _{millisecond = _} `modifyWithParser`
(parseInt 3 exactLength "Incorrect millisecond")
Placeholder s → void $ PS.string s
Escaped s → void $ PS.string s
MillisecondsShort → _{millisecond = _} `modifyWithParser`
(parseInt 1 exactLength "Incorrect 1-digit millisecond" <#> (_ * 100))
MillisecondsTwoDigits → _{millisecond = _} `modifyWithParser`
Expand Down
1 change: 1 addition & 0 deletions test/src/DateTime.purs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ datetimeTest = describe "Data.Formatter.DateTime" do
, { format: "hhmmssS", dateStr: "1112301", date: makeDateTime 2017 4 10 11 12 30 123 }
, { format: "HHmmssSSS", dateStr: "134530123", date: makeDateTime 2017 4 10 13 45 30 123 }
, { format: "HHmm", dateStr: "1345", date: makeDateTime 2017 4 10 13 45 30 123 }
, { format: "[It's] MMMM D[st]", dateStr: "It's April 1st" , date: makeDateTime 2017 4 1 0 0 0 0}
]
(\({ format, dateStr, date }) → do
(format `FDT.formatDateTime` date) `shouldEqual` (Right dateStr)
Expand Down