[Ramda] Convert a QueryString to an Object using Function Composition in Ramda
In this lesson we'll use a handful of Ramda's utility functions to take a queryString full of name/value pairs and covert it into a JavaScript object so we can access those properties in a more useful way. Along the way, we'll build up a composition and look at the tail
, split
, map
and fromPairs
functions, along with the crucial compose
function.
const {compose, fromPairs, map, split, tail} = R const queryString = '?page=2&pageSize=10&total=203' const parseQs = compose( fromPairs, // {"page":"2","pageSize":"10","total":"203"} map(split('=')), // [["page","2"],["pageSize","10"],["total","203"]] split('&'), // ["page=2","pageSize=10","total=203"] tail // "page=2&pageSize=10&total=203" ) const result = parseQs(queryString) console.log(result)