/typed-format

Type safe 'sprintf' for Elm

Primary LanguageElmBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Typed Format

This library helps you turn data into nicely formatted strings in a type-safe manner.

The API deliberately mirrors url-parser which is based on the same idea (see references).

Examples

Here is a formatter that, when applied with 'sprintf', will accept a string output the string hello [X]!

hello : Format a (String -> a)
hello =
    s "hello " <++> string <++> s "!"

You can combined formatters using <++> to get more complex formatters. For example:

doubleHello : Format a (String -> String -> a)
doubleHello =
    hello <++> hello

and

myMessage : Format a (Char -> Char -> a)
myMessage =
    s "Elm gets me from " <++> char <++> s " to " <++> char

See the examples for more examples.

Installation

elm package install enetsee/typed-format

References

The library is based on the final representation of the Printer type from Oleg Kiselyov's Formatted Printer Parsers

The url-parser library is based on the corresponding Scanner type.