首页 > 其他 > 详细

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

时间:2017-01-20 17:39:54      阅读:181      评论:0      收藏:0      [点我收藏+]

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)

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!