diff --git a/README.md b/README.md index e278816..49583cb 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/Data/Formatter/DateTime.purs b/src/Data/Formatter/DateTime.purs index 06070a0..d0d6b14 100644 --- a/src/Data/Formatter/DateTime.purs +++ b/src/Data/Formatter/DateTime.purs @@ -70,6 +70,7 @@ data FormatterCommand | MillisecondsShort | MillisecondsTwoDigits | Placeholder String + | Escaped String derive instance eqFormatterCommand ∷ Eq FormatterCommand derive instance ordFormatterCommand ∷ Ord FormatterCommand @@ -104,6 +105,7 @@ printFormatterCommand = case _ of MillisecondsTwoDigits → "SS" Milliseconds → "SSS" Placeholder s → s + Escaped s → "[" <> s <> "]" printFormatter ∷ Formatter → String printFormatter = foldMap printFormatterCommand @@ -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 @@ -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 @@ -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` diff --git a/test/src/DateTime.purs b/test/src/DateTime.purs index 42d3a6a..6d03668 100644 --- a/test/src/DateTime.purs +++ b/test/src/DateTime.purs @@ -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)