qs

Types

pub type OneOrMany {
  One(String)
  Many(List(String))
}

Constructors

  • One(String)
  • Many(List(String))
pub type Query =
  Map(String, OneOrMany)

Functions

pub fn delete(query: Map(String, OneOrMany), key: String) -> Map(
  String,
  OneOrMany,
)

Delete a key from the query

pub fn empty() -> Map(String, OneOrMany)

Make an empty Query

pub fn get(query: Map(String, OneOrMany), key: String) -> Result(
  OneOrMany,
  String,
)

Get values from the query

pub fn get_as_bool(query: Map(String, OneOrMany), key: String) -> Result(
  Bool,
  String,
)

Attempt to get one value as a Bool If the value is a list this will fail

pub fn get_as_float(query: Map(String, OneOrMany), key: String) -> Result(
  Float,
  String,
)

Attempt to get one value as an Float If the value is a list this will fail

pub fn get_as_int(query: Map(String, OneOrMany), key: String) -> Result(
  Int,
  String,
)

Attempt to get one value as an Int If the value is a list this will fail

pub fn get_as_list(query: Map(String, OneOrMany), key: String) -> List(
  String,
)

Get values from the query as a list of strings (regardless if one or many). If keys are not present this defaults to an empty list

pub fn get_as_list_of_bool(query: Map(String, OneOrMany), key: String) -> Result(
  List(Bool),
  String,
)

Attempt to get values as a list of Bool

pub fn get_as_list_of_float(query: Map(String, OneOrMany), key: String) -> Result(
  List(Float),
  String,
)

Attempt to get values as a list of Float

pub fn get_as_list_of_int(query: Map(String, OneOrMany), key: String) -> Result(
  List(Int),
  String,
)

Attempt to get values as a list of Int

pub fn get_as_string(query: Map(String, OneOrMany), key: String) -> Result(
  String,
  String,
)

Attempt to get one value as a string If the value is a list this will fail

pub fn has_key(query: Map(String, OneOrMany), key: String) -> Bool

Tell if the query has the given key

pub fn insert(query: Map(String, OneOrMany), key: String, value: OneOrMany) -> Map(
  String,
  OneOrMany,
)

Insert a value in the query

pub fn insert_list(query: Map(String, OneOrMany), key: String, values: List(
    String,
  )) -> Map(String, OneOrMany)

Set a list of values in the query

pub fn insert_one(query: Map(String, OneOrMany), key: String, value: String) -> Map(
  String,
  OneOrMany,
)

Set a unique value in the query

pub fn maybe_get_as_list(query: Map(String, OneOrMany), key: String) -> Result(
  List(String),
  String,
)
pub fn merge(a: Map(String, OneOrMany), b: Map(String, OneOrMany)) -> Map(
  String,
  OneOrMany,
)

Merge two Querys. If there are entries with the same keys in both maps the entry from the second query takes precedence.

pub fn parse(qs: String) -> Result(Map(String, OneOrMany), String)

Parse a query string

Example

"?color=red&tags[]=large&tags[]=wool"
|> qs.parse

> Ok([ #("color", qs.One("red")), #("tags", qs.Many(["large", "wool"])) ] |> map.from_list)
pub fn parse_key_value(segment: String) -> Result(
  #(String, String, Bool),
  String,
)
pub fn push(query: Map(String, OneOrMany), key: String, value: String) -> Map(
  String,
  OneOrMany,
)

Adds one value to a list If the key is not a list then it will be promoted to a list If the key doesn’t exist then it will be added as a list of one item

pub fn serialize(query: Map(String, OneOrMany)) -> String

Serialize a query

Example

[ #("color", qs.One("red")), #("tags", qs.Many(["large", "wool"])) ] |> qs.serialize
> "?color=red&tags[]=large&tags[]=wool"
Search Document