您的位置:首页 > 移动开发 > Objective-C

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

2017-01-20 16:50 459 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: