zl程序教程

您现在的位置是:首页 >  其它

当前栏目

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

to in object Using an Function convert Ramda
2023-09-14 08:59:18 时间

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)