[Ramda] Create a Query String from an Object using Ramda's toPairs function

In this lesson, we'll use Ramda's toPairs function, along with mapjoinconcatand compose to create a reusable function that will convert an object to a querystring.

 

const R = require('ramda');

const {map, join, concat, compose, toPairs} = R;

const obj = {
    page: 2,
    limit: 6,
    type: 'movies'
};

const createQs = compose(
    concat('?'), // ?page=2&limit=6&type=movies
    join('&'), // page=2&limit=6&type=movies
    map(join('=')), // [ 'page=2', 'limit=6', 'type=movies' ]
    toPairs  // [ [ 'page', 2 ], [ 'limit', 6 ], [ 'type', 'movies' ] ]
);
const result = createQs(obj);

console.log(result);

 

posted @ 2017-03-10 17:13  Zhentiw  阅读(240)  评论(0)    收藏  举报