This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unserialize.js
80 lines (63 loc) · 1.9 KB
/
unserialize.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
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
exports.unserializeValue = unserializeValue;
exports.default = unserialize;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Lazily type casts string values into possible actual type values.
*
* @param mixed unresolvedValue
*
* @return mixed
*/
function unserializeValue(unresolvedValue) {
if (unresolvedValue === 'true') return true;
if (unresolvedValue === 'false') return false;
if (isNaN(unresolvedValue) === false) {
return parseFloat(unresolvedValue);
}
return unresolvedValue;
}
/**
* Reverse of {@see serialize.js}, parses a serialized string into object.
*
* @param string unresolvedString
*
* @return string
*/
/* @type-dependencies */
function unserialize(unresolvedString) {
var unserializedObject = {};
var urlSearchParams = new URLSearchParams(unresolvedString);
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = urlSearchParams.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _step$value = (0, _slicedToArray3.default)(_step.value, 2),
key = _step$value[0],
value = _step$value[1];
var decodedKey = decodeURIComponent(key);
var decodedValue = unserializeValue(decodeURIComponent(value));
unserializedObject[decodedKey] = decodedValue;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return unserializedObject;
}