-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathutils.js
67 lines (57 loc) · 2.31 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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"); } }; }();
exports.obj2str = obj2str;
exports.str2obj = str2obj;
exports.getType = getType;
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; }
/**
* Serialize an object to query string:
*
* obj2str({ a: 1, b: 2 }) => 'a=1&b=2'
*
* @param {object} obj
* @returns {string}
*/
function obj2str(obj) {
return Object.getOwnPropertyNames(obj).map(function (key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]);
}).join('&');
}
/**
* Deserialize a query string to an clean object:
*
* str2obj('a=1&b=2') => { a: 1, b: 2 }
*
* @param {string} str
* @returns {object}
*/
function str2obj(str) {
if (!str) {
return {};
}
return str.split('&').reduce(function (acc, cur) {
var _cur$split = cur.split('='),
_cur$split2 = _slicedToArray(_cur$split, 2),
k = _cur$split2[0],
v = _cur$split2[1];
k = decodeURIComponent(k);
v = decodeURIComponent(v);
return _extends({}, acc, _defineProperty({}, k, v));
}, {});
}
/**
* Get the value type:
*
* getType({}) => 'Object'
* getType([]) => 'Array'
*
* @param {*} obj
* @returns {string}
*/
function getType(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
}