This library is to write type safe formatter.
A formatter is function that formats a thing into an output. Eg if I have a record, and I want to convert it to a string, for printing.
The output can be other than string, eg I want to print to "html", say each show someone's first name as bold, and last name as grayed out.
import Formatting as F exposing ((<>))
type alias User =
{ first : String
, last : String
}
userFormat : F.Format User String
userFormat =
(.first >> F.string) <> F.s " " <> (.last >> F.string)
user : User -> String
user =
F.print userFormatThis library is heavily influenced by krisajenkins/formatting.
If you want to combine two formatted strings, use ++ or equivalent.
user : Format User String
date : Format Date String
user_date : User -> Date -> String
user_date u d =
print user u ++ print date dThese help us create type safe formatters. The formatters are of type
Format a String, if you want a formatter of other kind, say Format a b, you
will can use map function to convert. So the path is a -> String -> b.
You can ignore these methods and directly write Format a b if you so want.
Creates a formatter for String.
Creates a formatter for Int.
Creates a formatter for Bool.
Creates a formatter for Float.
Creates a formatter for Int or Float.
<> lets you compose formatters together, best demonstrated via an example:
first : Format User String
first =
.first >> string
last : Format User String
last =
.last >> string
name : Format User String
name =
first <> s " " <> last
print name { first = "Amit", last = "Upadhyay" }
--> "Amit Upadhyay"map changes the type of output from one format to another. It can also be used for general post processing of the output.
name : Format User String
name =
(.name >> string)
|> map String.toUpperPrint applies a formatter on a value, to produce the output.
These are some helpers that change the output after the conversion of
a -> String, they are simple applications of map.
Wrap puts a token around the output.
name : Format User String
name =
(.name >> string)
|> wrap "'"
print name { name = "Amit" }
--> 'Amit' (puts "'", before and after).Pad a string on both sides until it has a given length.
Pad a string on the left until it has a given length.
Pad a string on the right until it has a given length.
Rounds a float to given number of decimal points.
Format is a the core type on which this library works on. It is nothing fancy, for a Format a String, it converts a to list of String.
The reason why we format it into a list of string, is so we can compose more
than one formatter together. Each formatter can return a list of final output
type, r, into a list, and our composition function <> concatenates the
lists together.
If Elm has support for monoids, we may not have needed the list.
Creates a formatter that returns the same string for any value.