This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0 2.0.0
eDSL for typesafe URL parsing
version 1.0.1
license MIT
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 1.0.1
Committed At 2018-04-28 08:37:36 UTC
elm-lang/core 5.1.1 <= v < 6.0.0 5.1.1
elm-community/elm-test 4.0.0 <= v < 5.0.0 4.2.0

Modules

README

Pathfinder

Build Status

An embedded DSL for a typesafe parsing of URL. It is designed for processing URLs of any complexity.

The library can parse single value, multiple values with the strict or arbitrary order and queries. It also allows writing typesafe parser for a query.

The library contains two main parts:

  • A DSL for building parsing tree
  • Function which performs parsing of a string in according to provided parsing tree

Examples

Parsing of a string form a simple path.

parseName: String -> Maybe String
parseName url =
    case parse (p "someUrl" </> str) url of
        Str name ->
            Just name
        
        _ ->
            Nothing


parseName "someUrl/userName"  -- Just "userName"

Parsing of an integer from a path which contains a query.

parseQuery: String -> Maybe Int
parseQuery url =
    case parse (p "someUrl" <?> (p "age" <=> int)) url of
        Integer age ->
            Just age
        
        _ ->
            Nothing


parseQuery "someUrl?age=10"  -- Just 10

Parsing of multiple values.

parseNameAndId: String -> Just ( String, Int )
parseNameAndId url = 
    case parse (p "someUrl" </> str </> int </> any ) url of
        MultiValue [ Str name, Integer id] ->
                    Just (name, id)
                
        _ ->
            Nothing


parseNameAndId "someUrl/userName/1/someRandomStuff"  -- Just ("userName", 1)

Error handling in case of unsuccesful parsing.

getDevider: String -> Result String Float
getDevider url =
    case (parse any </> float) url of
        Floating value->
            Ok value
            
        Failure err ->
            Err err
        
        _ ->
            Err "Unexpected path."


getDevider "10*3.1415"  -- Err "10*3.1415 does not contain /"

Contributing

Feedback and contributions are very welcome.

For installation of local npm dependencies run:

npm install

For installation of local elm dependencies run:

npm run install

Repeat the same actions for tests folder.

Run tests from root folder by:

npm test

Any Elm CLI commands can be called by:

npm run elm [comands]