UNPKG

5.87 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getSegments = getSegments;
7exports.getSegment = getSegment;
8exports.flattenParamObject = flattenParamObject;
9exports.buildParamString = buildParamString;
10exports.buildUrl = buildUrl;
11exports.objectifyParams = exports.ENCODED_NULL_BYTE = void 0;
12
13function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
14
15function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
16
17function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
18
19function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
20
21function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); }
22
23function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
24
25function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
26
27function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
28
29function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } 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"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
30
31function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
32
33var keys = Object.keys,
34 entries = Object.entries;
35var ENCODED_NULL_BYTE = '%00';
36/**
37 * Get the path segments from an absolute path with an optional query.
38 *
39 * @param {string} path
40 * @return {Array}
41 */
42
43exports.ENCODED_NULL_BYTE = ENCODED_NULL_BYTE;
44
45function getSegments(path) {
46 var _path$split = path.split('?'),
47 _path$split2 = _slicedToArray(_path$split, 1),
48 pathComponent = _path$split2[0];
49
50 var _pathComponent$split = pathComponent.split('/'),
51 _pathComponent$split2 = _toArray(_pathComponent$split),
52 segments = _pathComponent$split2.slice(1);
53
54 return segments;
55}
56/**
57 * Get the second path segment from an absolute path with an optional query.
58 *
59 * @param {string} path
60 * @return {string}
61 */
62
63
64function getSegment(path) {
65 var _getSegments = getSegments(path),
66 _getSegments2 = _slicedToArray(_getSegments, 2),
67 segment = _getSegments2[1];
68
69 return segment;
70}
71
72function flattenParamObject(object, paramName) {
73 return Array.isArray(object) ? _defineProperty({}, paramName, object) : keys(object).reduce(function (acc, key) {
74 return _objectSpread({}, acc, _defineProperty({}, "".concat(paramName, "[").concat(key, "]"), object[key]));
75 }, {});
76}
77/**
78 * Build a paramString from an the keyValues of an object
79 *
80 * @param {Object} params
81 * @return {string}
82 */
83
84
85function buildParamString(params) {
86 var flattenedParams = keys(params).filter(function (param) {
87 return params[param];
88 }).reduce(function (acc, param) {
89 return _typeof(params[param]) === 'object' ? _objectSpread({}, acc, {}, flattenParamObject(params[param], param)) : _objectSpread({}, acc, _defineProperty({}, param, params[param]));
90 }, {});
91
92 var encodeValue = function encodeValue(value) {
93 if (value === ENCODED_NULL_BYTE) {
94 return value;
95 }
96
97 return Array.isArray(value) ? value.map(encodeURIComponent).join(',') : encodeURIComponent(value);
98 };
99
100 var paramsAsString = entries(flattenedParams).map(function (_ref2) {
101 var _ref3 = _slicedToArray(_ref2, 2),
102 param = _ref3[0],
103 value = _ref3[1];
104
105 return "".concat(param, "=").concat(encodeValue(value));
106 }).join('&');
107 var preParam = paramsAsString.length ? '?' : '';
108 return "".concat(preParam).concat(paramsAsString);
109}
110/**
111 * Build a URL from the base url and given params
112 *
113 * @param {string} url
114 * @param {Object} params
115 * @return {string}
116 */
117
118
119function buildUrl(url, params) {
120 return "".concat(url).concat(buildParamString(params));
121}
122/**
123 * @param {string} params
124 * @return {Object}
125 */
126
127
128var objectifyParams = function objectifyParams(params) {
129 if (!params) {
130 return {};
131 }
132
133 return params.split('&').reduce(function (acc, param) {
134 var _param$split = param.split('='),
135 _param$split2 = _slicedToArray(_param$split, 2),
136 name = _param$split2[0],
137 value = _param$split2[1];
138
139 acc[name] = decodeURIComponent(value);
140 return acc;
141 }, {});
142};
143
144exports.objectifyParams = objectifyParams;
\No newline at end of file