Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix erroneous empty key value pair #6

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions examples/strings/priv/static/strings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,22 @@ function do_take(loop$list, loop$n, loop$acc) {
function take(list, n) {
return do_take(list, n, toList([]));
}
function fold(loop$list, loop$initial, loop$fun) {
while (true) {
let list = loop$list;
let initial = loop$initial;
let fun = loop$fun;
if (list.hasLength(0)) {
return initial;
} else {
let x = list.head;
let rest$1 = list.tail;
loop$list = rest$1;
loop$initial = fun(initial, x);
loop$fun = fun;
}
}
}

// build/dev/javascript/gleam_stdlib/gleam/result.mjs
function map2(result, fun) {
Expand Down Expand Up @@ -1328,23 +1344,6 @@ function get(from3, get2) {
function insert(dict, key, value3) {
return map_insert(key, value3, dict);
}
function fold_list_of_pair(loop$list, loop$initial) {
while (true) {
let list = loop$list;
let initial = loop$initial;
if (list.hasLength(0)) {
return initial;
} else {
let x = list.head;
let rest = list.tail;
loop$list = rest;
loop$initial = insert(initial, x[0], x[1]);
}
}
}
function from_list(list) {
return fold_list_of_pair(list, new$());
}
function update(dict, key, fun) {
let _pipe = dict;
let _pipe$1 = get(_pipe, key);
Expand All @@ -1371,7 +1370,7 @@ function do_fold(loop$list, loop$initial, loop$fun) {
}
}
}
function fold(dict, initial, fun) {
function fold2(dict, initial, fun) {
let _pipe = dict;
let _pipe$1 = map_to_list(_pipe);
return do_fold(_pipe$1, initial, fun);
Expand All @@ -1381,7 +1380,7 @@ function do_map_values(f, dict) {
return insert(dict2, k, f(k, v));
};
let _pipe = dict;
return fold(_pipe, new$(), f$1);
return fold2(_pipe, new$(), f$1);
}
function map_values(dict, fun) {
return do_map_values(fun, dict);
Expand Down Expand Up @@ -2056,31 +2055,32 @@ var listen = (dispatch) => {
// build/dev/javascript/lustre_hash_state/lustre_hash_state.mjs
function parse_hash(query) {
let _pipe = split3(query, "&");
let _pipe$1 = map(
return fold(
_pipe,
(part) => {
let $ = split3(part, "=");
new$(),
(accumulator, element2) => {
let $ = split3(element2, "=");
if ($.hasLength(2)) {
let key = $.head;
let value3 = $.tail.head;
return [
return update(
accumulator,
key,
(() => {
let _pipe$12 = value3;
let _pipe$2 = percent_decode2(_pipe$12);
(_) => {
let _pipe$1 = value3;
let _pipe$2 = percent_decode2(_pipe$1);
return unwrap(_pipe$2, value3);
})()
];
}
);
} else if ($.hasLength(0)) {
return ["", ""];
return accumulator;
} else if ($.hasLength(1)) {
return ["", ""];
return accumulator;
} else {
return ["", ""];
return accumulator;
}
}
);
return from_list(_pipe$1);
}
function stringify_hash(dct) {
let _pipe = map_to_list(dct);
Expand Down
15 changes: 7 additions & 8 deletions src/lustre_hash_state.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ pub fn update(key: String, value: String) -> effect.Effect(msg) {
/// Parses a query string into a dict
pub fn parse_hash(query: String) -> dict.Dict(String, String) {
string.split(query, "&")
|> list.map(fn(part) {
case string.split(part, "=") {
[key, value] -> #(
key,
value |> uri.percent_decode |> result.unwrap(value),
)
[] | [_] | _ -> #("", "")
|> list.fold(dict.new(), fn(accumulator, element) {
case string.split(element, "=") {
[key, value] ->
dict.update(accumulator, key, fn(_) {
value |> uri.percent_decode |> result.unwrap(value)
})
[] | [_] | _ -> accumulator
}
})
|> dict.from_list
}

/// Converts a dict to a query string
Expand Down
5 changes: 5 additions & 0 deletions test/lustre_hash_state_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pub fn encoded_parse_hash_test() {
|> should.equal(dict.from_list([#("key", "value with spaces")]))
}

pub fn parse_empty_hash_test() {
lustre_hash_state.parse_hash("")
|> should.equal(dict.from_list([]))
}

pub fn stringify_hash_test() {
dict.from_list([#("one", "1"), #("two", "2")])
|> lustre_hash_state.stringify_hash
Expand Down