• Stars
    star
    114
  • Rank 308,031 (Top 7 %)
  • Language
    Elm
  • License
    BSD 3-Clause "New...
  • Created over 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Parse URLs into nicely structured data

URL Parser

This library helps you turn URLs into nicely structured data.

It is designed to be used with elm-lang/navigation to help folks create single-page applications (SPAs) where you manage browser navigation yourself.

Note: This library is meant to serve as a baseline for future URL parsers. For example, it does not handle query parameters and hashes right now. It is more to (1) get folks started using URL parsers and (2) help us gather data on exactly which scenarios people face.

Examples

Here is a simplified REPL session showing a parser in action:

> import UrlParser exposing ((</>), s, int, string, parseHash)

> parseHash (s "blog" </> int) { ... , hash = "#blog/42" }
Just 42

> parseHash (s "blog" </> int) { ... , hash = "#/blog/13" }
Just 13

> parseHash (s "blog" </> int) { ... , hash = "#/blog/hello" }
Nothing

> parseHash (s "search" </> string) { ... , hash = "#search/dogs" }
Just "dogs"

> parseHash (s "search" </> string) { ... , hash = "#/search/13" }
Just "13"

> parseHash (s "search" </> string) { ... , hash = "#/search" }
Nothing

Normally you have to put many of these parsers to handle all possible pages though! The following parser works on URLs like /blog/42 and /search/badger:

import UrlParser exposing (Parser, (</>), s, int, string, map, oneOf, parseHash)

type Route = Blog Int | Search String

route : Parser (Route -> a) a
route =
  oneOf
    [ map Blog (s "blog" </> int)
    , map Search (s "search" </> string)
    ]

-- parseHash route { ... , hash = "#/blog/58" }    == Just (Blog 58)
-- parseHash route { ... , hash = "#/search/cat" } == Just (Search "cat")
-- parseHash route { ... , hash = "#/search/31" }  == Just (Search "31")
-- parseHash route { ... , hash = "#/blog/cat" }   == Nothing
-- parseHash route { ... , hash = "#/blog" }       == Nothing

Notice that we are turning URLs into nice union types, so we can use case expressions to work with them in a nice way.

Check out the examples/ directory of this repo to see this in use with elm-lang/navigation.

Testing

npm install
npm test

Background

I first saw this general idea in Chris Done’s formatting library. Based on that, Noah and I outlined the API you see in this library. Noah then found Rudi Grinberg’s post about type safe routing in OCaml. It was exactly what we were going for. We had even used the names s and (</>) in our draft API! In the end, we ended up using the “final encoding” of the EDSL that had been left as an exercise for the reader. Very fun to work through!

More Repositories

1

elm-architecture-tutorial

How to create modular Elm code that scales nicely with your app
Elm
4,171
star
2

elm-todomvc

The TodoMVC app written in Elm, nice example for beginners.
Elm
1,220
star
3

start-app

DEPRECATED. Moved to elm-lang/html
Elm
392
star
4

elm-html

DEPRECATED. Moved to elm-lang/html
Elm
336
star
5

guide.elm-lang.org

My book introducing you to Elm!
Elm
318
star
6

elm-sortable-table

Sortable tables for whatever data you want to display
Elm
284
star
7

functional-programming-in-elm

DRAFT outlining some techniques of functional programming
Shell
230
star
8

airplane-mode

Airplanes are programming heaven. Airplane Mode turns off the bad internet. Yes docs, no facebook!
Python
122
star
9

elm-playground

Create pictures, animations, and games with Elm!
Elm
107
star
10

elm-html-and-js

Example of how to integrate Elm with HTML and JS
HTML
93
star
11

react-angular-ember-elm-performance-comparison

Comparing performance of Elm, React, Angular, and Ember
JavaScript
91
star
12

elm-http

DEPRECATED. This library is now called elm-lang/http
Elm
89
star
13

elm-markdown

Markdown parsing within Elm
Elm
88
star
14

first-person-elm

First-person navigation in a simple 3D world, written in Elm
Elm
85
star
15

focus

A library for getting and setting values in deeply nested data structures.
Elm
80
star
16

TodoFRP

Basic Todo list example, written with FRP in Elm
Elm
75
star
17

elm-graphics

The graphical building blocks that inspired Elm in the first place
Elm
55
star
18

elm-effects

DEPRECATED. Moved to elm-lang/core
Elm
46
star
19

automaton

experimental library for Arrowized FRP in Elm
Elm
41
star
20

elm-svg

DEPRECATED. Moved to elm-lang/svg
Elm
30
star
21

elm-syntax-highlighting

Syntax Highlighting for Elm in Sublime Text
30
star
22

time-zone-proposal

A proposal for accurately computing local time in JavaScript
29
star
23

elm-hack-night

Elm
28
star
24

virtual-dom

DEPRECATED. Full rewrite in elm-lang/virtual-dom
JavaScript
25
star
25

elm-format-on-save

Sublime Text plugin to run elm-format on save
Python
23
star
26

local-channel

Helps you write self-contained components in Elm
Elm
16
star
27

elm-project-survey

How do build times and asset sizes change as Elm projects grow larger?
Haskell
13
star
28

task-tutorial

Friendly functions for getting started with tasks
JavaScript
11
star
29

elm-at-strangeloop

6
star
30

elm-at-mloc-js

Slides and resources from the mloc.js conference in Budapest! Graciously hosted by Prezi :)
5
star
31

elm-at-pldi-2013

A presentation written entirely in Elm, made for a talk at PLDI 2013.
Elm
4
star
32

codemesh-tutorial

self-contained exercises that emphasize fundamentals and the Elm Architecture
JavaScript
4
star
33

cufp-tutorial

Practice problems for the Elm tutorial at CUFP 2014
Elm
2
star