1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-25 11:28:37 +01:00
js-bigchaindb-driver/dist/node/stringify_as_query_param.js
2017-05-10 09:36:07 +02:00

70 lines
2.9 KiB
JavaScript

'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
exports.default = stringifyAsQueryParam;
var _entries = require('core-js/library/fn/object/entries');
var _entries2 = _interopRequireDefault(_entries);
var _decamelize = require('decamelize');
var _decamelize2 = _interopRequireDefault(_decamelize);
var _queryString = require('query-string');
var _queryString2 = _interopRequireDefault(_queryString);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* imported from https://github.com/bigchaindb/js-utility-belt/
*
* Takes a key-value dictionary (ie. object) and converts it to a query-parameter string that you
* can directly append into a URL.
*
* Extends queryString.stringify by allowing you to specify a `transform` function that will be
* invoked on each of the dictionary's keys before being stringified into the query-parameter
* string.
*
* By default `transform` is `decamelize`, so a dictionary of the form:
*
* {
* page: 1,
* pageSize: 10
* }
*
* will be converted to a string like:
*
* ?page=1&page_size=10
*
* @param {object} obj Query params dictionary
* @param {function} [transform=decamelize] Transform function for each of the param keys
* @return {string} Query param string
*/
function stringifyAsQueryParam(obj) {
var transform = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _decamelize2.default;
if (!obj || (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object' || !Object.keys(obj).length) {
return '';
}
var transformedKeysObj = (0, _entries2.default)(obj).reduce(function (paramsObj, _ref) {
var _ref2 = _slicedToArray(_ref, 2),
key = _ref2[0],
value = _ref2[1];
paramsObj[transform(key)] = value;
return paramsObj;
}, {});
return '?' + _queryString2.default.stringify(transformedKeysObj);
}
module.exports = exports['default'];