|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+"use strict";
|
|
|
2
|
+
|
|
|
3
|
+var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
|
|
|
4
|
+
|
|
|
5
|
+module.exports = {
|
|
|
6
|
+
|
|
|
7
|
+ _defaults: {
|
|
|
8
|
+ "warn_on_invalid": true
|
|
|
9
|
+ },
|
|
|
10
|
+
|
|
|
11
|
+ convert: function convert(data, options) {
|
|
|
12
|
+ if ((typeof data === "undefined" ? "undefined" : _typeof(data)) === 'object') {
|
|
|
13
|
+ this._merge_options(this._defaults, options);
|
|
|
14
|
+ var result = "?";
|
|
|
15
|
+ Object.keys(data).map(function (query_key) {
|
|
|
16
|
+ var query_data = data[query_key];
|
|
|
17
|
+ var query_data_processed = void 0;
|
|
|
18
|
+ if (query_data === 'null') {
|
|
|
19
|
+ if (options.warn_on_invalid) {
|
|
|
20
|
+ console.warn("Attempted to convert null to query string!");
|
|
|
21
|
+ }
|
|
|
22
|
+ } else if (typeof query_data === 'number') {
|
|
|
23
|
+ query_data_processed = query_data.toString();
|
|
|
24
|
+ } else if (typeof query_data === 'string') {
|
|
|
25
|
+ query_data_processed = query_data;
|
|
|
26
|
+ } else if (typeof query_data === 'boolean') {
|
|
|
27
|
+ query_data_processed = query_data.toString();
|
|
|
28
|
+ } else if ((typeof query_data === "undefined" ? "undefined" : _typeof(query_data)) === 'object') {
|
|
|
29
|
+ query_data_processed = JSON.stringify(query_data);
|
|
|
30
|
+ } else if (typeof query_data === 'undefined') {
|
|
|
31
|
+ if (options.warn_on_invalid) {
|
|
|
32
|
+ console.warn("Attempted to convert undefined to query string!");
|
|
|
33
|
+ }
|
|
|
34
|
+ } else {
|
|
|
35
|
+ if (options.warn_on_invalid) {
|
|
|
36
|
+ console.warn("Attempted to convert function or symbol to query string!");
|
|
|
37
|
+ }
|
|
|
38
|
+ }
|
|
|
39
|
+ if (query_data_processed !== 'undefined') {
|
|
|
40
|
+ var append = query_key + "=" + encodeURIComponent(query_data_processed) + "&";
|
|
|
41
|
+ result += append;
|
|
|
42
|
+ }
|
|
|
43
|
+ });
|
|
|
44
|
+ return result.substring(0, result.length - 1);
|
|
|
45
|
+ } else {
|
|
|
46
|
+ if (options.warn_on_invalid) {
|
|
|
47
|
+ console.warn("Attempted to convert non-object to query string!");
|
|
|
48
|
+ return "";
|
|
|
49
|
+ }
|
|
|
50
|
+ }
|
|
|
51
|
+ },
|
|
|
52
|
+
|
|
|
53
|
+ _merge_options: function _merge_options(obj1, obj2) {
|
|
|
54
|
+ var obj3 = {};
|
|
|
55
|
+ Object.keys(obj1).map(function (attrname) {
|
|
|
56
|
+ obj3[attrname] = obj1[attrname];
|
|
|
57
|
+ });
|
|
|
58
|
+ Object.keys(obj2).map(function (attrname) {
|
|
|
59
|
+ obj3[attrname] = obj2[attrname];
|
|
|
60
|
+ });
|
|
|
61
|
+ return obj3;
|
|
|
62
|
+ }
|
|
|
63
|
+};
|