Skip to content

Commit

Permalink
Docs improvements + codec
Browse files Browse the repository at this point in the history
  • Loading branch information
gigobyte committed Mar 27, 2020
1 parent cc8e4da commit 67b93d0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
31 changes: 17 additions & 14 deletions elm.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"type": "package",
"name": "gigobyte/iso8601-duration",
"summary": "Parser for ISO 8601 durations",
"license": "BSD-3-Clause",
"version": "1.0.0",
"exposed-modules": ["Iso8601.Duration"],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"elm/core": "1.0.0 <= v < 2.0.0",
"elm/parser": "1.1.0 <= v < 2.0.0"
},
"test-dependencies": {
"elm-explorations/test": "1.2.2 <= v < 2.0.0"
}
"type": "package",
"name": "gigobyte/iso8601-duration",
"summary": "Parser for ISO 8601 durations",
"license": "BSD-3-Clause",
"version": "1.0.0",
"exposed-modules": [
"Iso8601.Duration"
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"elm/core": "1.0.0 <= v < 2.0.0",
"elm/json": "1.1.3 <= v < 2.0.0",
"elm/parser": "1.1.0 <= v < 2.0.0"
},
"test-dependencies": {
"elm-explorations/test": "1.2.2 <= v < 2.0.0"
}
}
37 changes: 31 additions & 6 deletions src/Iso8601/Duration.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Iso8601.Duration exposing (Duration, fromString, toString)
module Iso8601.Duration exposing (Duration, fromString, toString, decoder, encode)

{-| Convert ISO-8601 duration strings to a Duration value and vice versa.
{-| Convert between ISO-8601 durations strings and `Duration` values.
@docs Duration, fromString, toString
@docs Duration, fromString, toString, decoder, encode
-}

import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Parser exposing ((|.), (|=), Parser, Step(..), end, float, loop, oneOf, problem, succeed, symbol)


Expand Down Expand Up @@ -40,7 +42,7 @@ type Element
| Seconds Float


{-| Convert ISO-8601 duration strings to a Duration value.
{-| Convert from an ISO-8601 duration string to a `Duration` value.
In case a week duration is given only the `days` property will be populated with the number of weeks \* 7.
Expand Down Expand Up @@ -89,9 +91,9 @@ fromString duration =
Nothing


{-| Convert a Duration value to a ISO-8601 duration string.
{-| Convert a `Duration` value to an ISO-8601 duration string.
Week durations are not supported, even values with only days will still be serialized as `PnD`.
Week durations are not supported, even values with `days` only will still be serialized as `PnD`.
-}
toString : Duration -> String
Expand Down Expand Up @@ -123,6 +125,29 @@ toString duration =
)


{-| Decode an ISO-8601 duration string to a `Duration` value using [`fromString`](#fromString).
-}
decoder : Decoder Duration
decoder =
Decode.string
|> Decode.andThen
(\str ->
case fromString str of
Just duration ->
Decode.succeed duration

Nothing ->
Decode.fail "Invalid duration string"
)


{-| Encode a `Duration` value as an ISO-8601 duration string using [`toString`](#toString).
-}
encode : Duration -> Encode.Value
encode =
toString >> Encode.string


parseElements : Parser (List Element) -> String -> Maybe (List Element)
parseElements elementParser date =
Result.toMaybe (Parser.run elementParser date)
Expand Down

0 comments on commit 67b93d0

Please sign in to comment.