What's the easiest way to do something similar to Node's `querystring.stringify` in ReasonML?

reasonreact

#1

I couldn’t find an answer online so I wrote my own simplified version (below). Curious to know if there’s a simpler way to perform this task in ReasonML.

let stringifyParams = params => {
	List.fold_left((str, param) => {
		switch(param) {
			| (label, Some(value)) => {
				let strParam = label ++ "=" ++ value;

				if (str === "") {
					str ++ strParam
				} else {
					str ++ "&" ++ strParam
				};
			}
			| (_, None) => str
		}
	}, "", params);
}

#2

Which platform are you targeting? Browser/Node with JavaScript? In that case you can bind to querystring.stringify directly thanks to BuckleScript’s FFI, it should be really easy.

[EDIT: I want to add that it’s not a good idea to roll your own, because URL-encoded strings need to be percent-escaped :slight_smile: . If you need to do this targeting OCaml native, try https://docs.mirage.io/uri/Uri/index.html#val-encoded_of_query .]