[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 tailsplitmap and fromPairs functions, along with the crucial composefunction.

 

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)

 

posted @ 2017-01-20 16:50  Zhentiw  阅读(208)  评论(0编辑  收藏  举报