-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
54 lines (41 loc) · 1.21 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
47
48
49
50
51
52
53
54
"use strict";
function json2react(create, mapper, schema) {
if (typeof schema === "undefined") {
schema = mapper;
mapper = null;
}
if (schema === null) {
return null;
}
if (typeof schema === "string") {
return schema;
}
if (!isPlainObject(schema)) {
throw new Error("schema must be a string or a plain object");
}
var hasNonEmptySchemaType = (
schema.type &&
typeof schema.type === "string" &&
schema.type.trim() !== ""
);
if (! hasNonEmptySchemaType) {
throw new Error("schema.type must be a non-empty string");
}
schema.type = schema.type.trim();
if (schema.props !== undefined && !isPlainObject(schema.props)) {
throw new Error("schema.props must be a plain object");
}
var type = schema.type;
var props = schema.props || null;
var children = schema.children && [].concat(schema.children).map(json2react.bind(null, create, mapper));
mapper && (type = mapper(type, props));
return create.apply(create, [].concat([type, props]).concat(children));
}
function isPlainObject(maybe) {
return (
maybe !== null &&
typeof maybe === "object" &&
Object.prototype.toString.call(maybe) == "[object Object]"
);
}
module.exports = json2react;