-
Notifications
You must be signed in to change notification settings - Fork 1
/
deserialize.js
49 lines (41 loc) · 1.01 KB
/
deserialize.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
function deserialize (value, type) {
switch (type) {
case Number:
value = Number(value);
break;
case Boolean:
value = Boolean(value === '' || value === 'true' || value === '1' || value === 'on');
break;
case Object:
try {
value = JSON.parse(value);
} catch (err) {
// allow non-JSON literals like Strings and Numbers
// console.warn('Failed decode json: "' + value + '" to Object');
}
break;
case Array:
try {
value = JSON.parse(value);
} catch (err) {
// .console.warn('Failed decode json: "' + value + '" to Array');
value = null;
}
break;
case Date:
value = new Date(value);
break;
case RegExp:
value = new RegExp(value);
break;
case Function:
value = new Function(value); // eslint-disable-line
break;
// behave like default for now
// case String:
default:
break;
}
return value;
}
export default deserialize;