fmglee

fmglee

A string formatting library for gleam.

Valid format specifiers are

Examples

import fmglee
import gleeunit/should

fmglee.new("Number %d, float %f, string %s")
|> fmglee.d(99)
|> fmglee.f(12.9)
|> fmglee.s("Hello!")
|> fmglee.build
|> should.equal("Number 99, float 12.9, string Hello!")
fmglee.sprintf("Number %d, float %f, string %s", with: [fmglee.D(99), fmglee.F(12.9), fmglee.S("Hello!")])
|> should.equal("Number 99, float 12.9, string Hello!")
fmglee.sprintf("%,.2f", [fmglee.F(1234.554)])
|> should.equal("1,234.55")

Types

pub type Fmt {
  S(String)
  D(Int)
  F(Float)
}

Constructors

  • S(String)

    Wrap a String for use with sprintf(_, using: [..])

  • D(Int)

    Wrap an Int for use with sprintf(_, using: [..])

  • F(Float)

    Wrap a Float for use with fmt(_, using: [..])

Errors returned by fmtglee

pub type FmtError {
  IncorrectValueType(expected: String, got: Fmt)
  TooManyValues
  NotEnoughValues
  InvalidFloat(String)
  InvalidInt(String)
  InvalidFloatFormatSpecifier(String)
}

Constructors

  • IncorrectValueType(expected: String, got: Fmt)

    The current value does not match the current placeholder. Returned when the order of Fmts is not the same as the order of placeholders.

  • TooManyValues

    Returned when there are more Fmts than placeholders in the string.

  • NotEnoughValues

    Returned when there are less Fmts than placeholders in the string.

  • InvalidFloat(String)

    Returned when a number has no floating point.

  • InvalidInt(String)

    Returned when an invalid Integer value is provided to a Float formatter.

  • InvalidFloatFormatSpecifier(String)

    Returned when a float format specifier cannot be parsed. Can also occur when a Float value is provided alongside a String or Int placeholder.

pub type Formatter {
  Formatter(s: String, v: List(Fmt))
}

Constructors

  • Formatter(s: String, v: List(Fmt))

    Used to compile a format string using the pipeable methods.

Functions

pub fn build(formatter: Formatter) -> String

Compile a Formatter into a string. Panics where try_build(formatter) returns an FmtError.

pub fn d(formatter: Formatter, d: Int) -> Formatter

Add an Int value to a Formatter.

pub fn f(formatter: Formatter, f: Float) -> Formatter

Add a Float value to a Formatter.

pub fn fmt(s: String, using v: List(Fmt)) -> String

Deprecated: Use sprintf instead

pub fn new(s: String) -> Formatter

Create a new Formatter.

Examples

new("Hello, %s. You are %d years old.")
|> s(name)
|> d(age)
|> build

new("Hello, %s. You are %d years old.")
|> s(name)
|> d(age)
|> printlnf
pub fn print(s: String, with v: List(Fmt)) -> Nil

Compile the format string and print the output. Panics if sprintf(s, v) would panic with the provided arguments.

pub fn printf(formatter: Formatter) -> Nil

Print the output of a compiled Formatter. Panics if the Formatter is invalid.

pub fn println(s: String, with v: List(Fmt)) -> Nil

Compile the format string and print the output with a newline. Panics if sprintf(s, v) would panic with the provided arguments.

pub fn printlnf(formatter: Formatter) -> Nil

Print the output of a compiled Formatter with a newline. Panics if the Formatter is invalid.

pub fn s(formatter: Formatter, s: String) -> Formatter

Add a String value to a Formatter.

pub fn sprintf(s: String, with v: List(Fmt)) -> String

Format a string and a list of Fmt values. Will panic where try_sprintf(s, v) returns an FmtError.

pub fn try_build(
  formatter: Formatter,
) -> Result(String, FmtError)

Compile a Formatter into a string. Errors when the number of provided values does not match the number of placeholders, or if an invalid type was provided for a placeholder.

pub fn try_fmt(
  s: String,
  with v: List(Fmt),
) -> Result(String, FmtError)

Deprecated: Use try_sprintf instead

pub fn try_sprintf(
  s: String,
  with v: List(Fmt),
) -> Result(String, FmtError)

Format a string and a list of Fmt values. Substitutes placeholders from left to right with values in the list of Fmt. Errors if there is a type missmatch between the placeholder and the value, or if the number of placeholders does not match the number of values given.

pub fn write(
  s: String,
  with v: List(Fmt),
  using writer: fn(String) -> Nil,
) -> Nil

Write a string and list of Fmt to the provided Writer.

pub fn writef(
  formatter: Formatter,
  using writer: fn(String) -> Nil,
) -> Nil

Write a Formatter to the provided writer. The Formatter will be built using build which will panic if the formatter is invalid.

Search Document