-
Notifications
You must be signed in to change notification settings - Fork 2
/
builder.js
59 lines (56 loc) · 1.26 KB
/
builder.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
/**
* Build Schema for nimnification of JSON data
* @param {*} jsObj
*/
function buildSchema(jsObj, key) {
if (jsObj === undefined) return null;
var type = typeOf(jsObj);
switch (type) {
case "array":
{
let schema = {
type: "list",
detail: buildSchema(jsObj[0])
};
key && (schema.name = key);
return schema;
}
case "object":
{
let schema = {
type: "map",
detail: []
};
key && (schema.name = key);
let keys = Object.keys(jsObj);
for (var i in keys) {
let key = keys[i];
if (jsObj[key] !== undefined) {
schema.detail.push(buildSchema(jsObj[key], key));
}
}
return schema;
}
case "null":
case "string":
case "number":
case "date":
case "boolean":
{
let schema = {
type: type
};
key && (schema.name = key);
return schema;
}
default:
throw Error("Unacceptable type : " + type);
}
}
function typeOf(obj) {
if (obj === null) return "null";
else if (Array.isArray(obj)) return "array";
else if (obj instanceof Date) return "date";
else return typeof obj;
}
module.exports.build = buildSchema;