[Ramda] Convert a QueryString to an Object using Function Composition in Ramda

时间:2017-01-20 17:39:54   收藏:0   阅读:182

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)

 

原文:http://www.cnblogs.com/Answer1215/p/6323273.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!