1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | 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; };
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | exports.default = function (data) {
|
14 | var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
15 |
|
16 | var sorted = Boolean(opts.sorted),
|
17 | skipIndex = Boolean(opts.skipIndex),
|
18 | ignorenull = Boolean(opts.ignorenull),
|
19 | encode = function encode(value) {
|
20 | return String(value).replace(/(?:[\0-\x1F"-&\+-\}\x7F-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/g, encodeURIComponent).replace(/ /g, '+').replace(/[!'()~\*]/g, function (ch) {
|
21 | return '%' + ch.charCodeAt().toString(16).slice(-2).toUpperCase();
|
22 | });
|
23 | },
|
24 | keys = function keys(obj) {
|
25 | var keyarr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Object.keys(obj);
|
26 | return sorted ? keyarr.sort() : keyarr;
|
27 | },
|
28 | filterjoin = function filterjoin(arr) {
|
29 | return arr.filter(function (e) {
|
30 | return e;
|
31 | }).join('&');
|
32 | },
|
33 | objnest = function objnest(name, obj) {
|
34 | return filterjoin(keys(obj).map(function (key) {
|
35 | return nest(name + '[' + key + ']', obj[key]);
|
36 | }));
|
37 | },
|
38 | arrnest = function arrnest(name, arr) {
|
39 | return arr.length ? filterjoin(arr.map(function (elem, index) {
|
40 | return skipIndex ? nest(name + '[]', elem) : nest(name + '[' + index + ']', elem);
|
41 | })) : encode(name + '[]');
|
42 | },
|
43 | nest = function nest(name, value) {
|
44 | var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : typeof value === 'undefined' ? 'undefined' : _typeof(value);
|
45 | var f = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
|
46 |
|
47 | if (value === f) f = ignorenull ? f : encode(name) + '=' + f;else if (/string|number|boolean/.test(type)) f = encode(name) + '=' + encode(value);else if (Array.isArray(value)) f = arrnest(name, value);else if (type === 'object') f = objnest(name, value);
|
48 |
|
49 | return f;
|
50 | };
|
51 |
|
52 | return data && filterjoin(keys(data).map(function (key) {
|
53 | return nest(key, data[key]);
|
54 | }));
|
55 | }; |
\ | No newline at end of file |