-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
139 lines (122 loc) · 3.89 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
let json = {}
let str = ''
function escapeStr (str) {
if (str !== undefined) {
return str.replace(/(([\w-]+)?(_|\+|\*)([\w-]+)?)/g, '`$1`')
} else {
return ''
}
}
function strX (str, json, spaces) {
let x = {}
Object.assign(x, json.properties, json.patternProperties)
let req = json.required
for (let prop in x) {
if (x.hasOwnProperty(prop)) {
let type = x[prop].type
if (Array.isArray(type)) {
type = type[0]
}
let description = escapeStr(x[prop].description)
switch (type) {
case 'integer':
case 'number':
type = 'number'
if (x[prop].hasOwnProperty('minimum')) {
description += ' - Mininum: **' + x[prop].minimum + '**'
}
if (x[prop].hasOwnProperty('maximum')) {
description += ' - Maximum: **' + x[prop].maximum + '**'
}
if (x[prop].hasOwnProperty('multipleOf')) {
description += ' - Max precision: **' + x[prop].multipleOf + '**'
}
break
case 'string':
if (x[prop].hasOwnProperty('enum')) {
type = 'enum'
} else {
if (x[prop].hasOwnProperty('minLength')) {
description += ' - Min length: **' + x[prop].minLength + '**'
}
if (x[prop].hasOwnProperty('maxLength')) {
description += ' - Max length: **' + x[prop].maxLength + '**'
}
if (x[prop].hasOwnProperty('format')) {
description += ' - Format: **' + x[prop].format + '**'
} else if (x[prop].hasOwnProperty('pattern')) {
description += ' - RegEx pattern: **`' + x[prop].pattern + '`**'
}
}
break
case 'array':
if (x[prop].hasOwnProperty('minItems')) {
description += ' - Min elements: **' + x[prop].minItems + '**'
}
if (x[prop].hasOwnProperty('maxItems')) {
description += ' - Max elements: **' + x[prop].maxItems + '**'
}
break
case 'object':
if (x[prop].hasOwnProperty('minProperties')) {
description += ' - Min properties: **' + x[prop].minProperties + '**'
}
if (x[prop].hasOwnProperty('maxProperties')) {
description += ' - Max properties: **' + x[prop].maxProperties + '**'
}
break
}
str += spaces + '+ `' + prop + '`'
if (x[prop].hasOwnProperty('default')) {
str += ': ' + x[prop].default
}
str += ' (' + type
if (Array.isArray(req)) {
for (let i = 0; i < req.length; i++) {
if (prop === req[i]) {
str += ', required'
break
}
}
}
str += ') - ' + description + '\n'
switch (type) {
case 'enum':
for (let i = 0; i < x[prop].enum.length; i++) {
str += spaces + ' + `' + x[prop].enum[i] + '` (string)\n'
}
break
case 'object':
str = strX(str, x[prop], spaces + ' ')
break
case 'array':
let items = x[prop].items
switch (items.type) {
case 'integer':
items.type = 'number'
break
case 'string':
if (items.hasOwnProperty('enum')) {
items.type = 'enum'
}
break
}
str += spaces + ' + (' + items.type + ') - ' + escapeStr(items.description) + '\n'
switch (items.type) {
case 'enum':
for (let i = 0; i < items.enum.length; i++) {
str += spaces + ' + `' + items.enum[i] + '` (string)\n'
}
break
case 'object':
str = strX(str, items, spaces + ' ')
break
}
break
}
}
}
return str
}
str = strX(str, json, '')
// console.log(str)