-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (43 loc) · 1.27 KB
/
index.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
function getType(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
}
function formatUNSAFE(target, format) {
let tType = getType(target);
let fType = getType(format);
switch (fType) {
case 'Object':
if (tType !== fType) {
target = {};
}
for (let key in format) {
target[key] = formatUNSAFE(target[key], format[key]);
}
return target;
case 'Array':
if (tType !== fType) {
target = [];
return target;
}
if (format.length === 0) {
return target;
}
target = target.map(function (value, key) {
if (key < format.length) {
value = formatUNSAFE(value, format[key]);
} else {
value = formatUNSAFE(value, format[0]);
}
return value;
});
return target;
case 'String':
case 'Number':
return tType === fType ? target : format;
case 'Undefined':
case 'Null':
return tType === fType ? format : target;
default:
return undefined;
}
}
export default formatUNSAFE;