1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getSegments = getSegments;
|
7 | exports.getSegment = getSegment;
|
8 | exports.flattenParamObject = flattenParamObject;
|
9 | exports.buildParamString = buildParamString;
|
10 | exports.buildUrl = buildUrl;
|
11 | exports.objectifyParams = exports.ENCODED_NULL_BYTE = void 0;
|
12 |
|
13 | function _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 |
|
15 | function 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 |
|
17 | function _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 |
|
19 | function _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 |
|
21 | function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); }
|
22 |
|
23 | function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
|
24 |
|
25 | function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
|
26 |
|
27 | function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
|
28 |
|
29 | function _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 |
|
31 | function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
32 |
|
33 | var keys = Object.keys,
|
34 | entries = Object.entries;
|
35 | var ENCODED_NULL_BYTE = '%00';
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | exports.ENCODED_NULL_BYTE = ENCODED_NULL_BYTE;
|
44 |
|
45 | function 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 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | function getSegment(path) {
|
65 | var _getSegments = getSegments(path),
|
66 | _getSegments2 = _slicedToArray(_getSegments, 2),
|
67 | segment = _getSegments2[1];
|
68 |
|
69 | return segment;
|
70 | }
|
71 |
|
72 | function 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 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function 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 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function buildUrl(url, params) {
|
120 | return "".concat(url).concat(buildParamString(params));
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | var 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 |
|
144 | exports.objectifyParams = objectifyParams; |
\ | No newline at end of file |