0.1.1 • Published 5 years ago
bs-query-string v0.1.1
bs-query-string
Simple reasonml solution for dealing with query strings.
Inspired by bs-json, BsQueryString.Parse works similarly to Json.Decode and BsQueryString.Stringify similarly to Json.Encode.
Installation
npm i --save bs-query-stringUpdate your bsconfig.json:
"bs-dependencies": [
...
+ "bs-query-string",
]Parse query string
Example of parsing a query string into a record:
type productSearch = {
query: string,
sizes: array(string),
};
let parseQs = (qs: string): t => {
let obj = BsQueryString.Parse.toQueryObj(qs);
BsQueryString.Parse.{
query: obj |> item("q", string),
sizes: obj |> item("color", array),
};
};item will throw if the key is not found, or if multiple values were received for a single string value. You can use optional to return None or withDefault to assign default value:
type productSearch = {
query: option(string),
sizes: array(string),
};
let parseQs = (qs: string): t => {
let obj = BsQueryString.Parse.toQueryObj(qs);
BsQueryString.Parse.{
query: obj |> optional(item("q", string)),
sizes: obj |> withDefault([||], item("color", array)),
};
};This will produce following results:
// "q=jeans&size=S&size=XL"
let search = { q: Some("jeans"), sizes: [|"S", "XL"|] };
// "size=S&size=XL"
let search = { q: None, sizes: [|"S", "XL"|] };
// ""
let search = { q: None, sizes: [||] };Stringify
Example of stringifying a record into a query string:
type productSearch = {
query: option(string),
sizes: array(string),
};
let stringifySearch = (search: productSearch) => {
Stringify.(
toQs([|
("query", optional(search.query, string)),
("array", item(search.sizes, array)),
|])
);
};optional will not include the key if the corresponding value is None.