-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
49 lines (46 loc) · 1.52 KB
/
test.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
var jsonfile = require('jsonfile')
const target_path = './data.json';
jsonfile.readFile(target_path, (err, file) => {
// var file = JSON.parse(file);
function start(parentObj, obj) {
var keyArr = Object.keys(obj);
keyArr.forEach(key => {
switch (typeof(obj[key])) {
case 'string':
if (Array.isArray(parentObj)) {
parentObj[0][key] = "String"
} else {
parentObj[key] = "String"
}
break;
case 'number':
if (Array.isArray(parentObj)) {
parentObj[0][key] = "Number"
} else {
parentObj[key] = "Number"
}
break;
case 'object':
if (Array.isArray(obj[key])) {
parentObj[key] = [{}]
parentObj[key] = start(parentObj[key], obj[key][0])
} else {
parentObj[key] = {}
parentObj[key] = start(parentObj[key], obj[key])
}
break;
default:
break;
}
})
return parentObj;
}
const exportFile = start({}, file);
jsonfile.writeFile('./typelize-result.json', exportFile, {
spaces: 2
}, function (err) {
if (err) {
console.log(err)
}
})
})