-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
42 lines (39 loc) · 1.24 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
'use strict';
const capitalize = require('capitalize');
const RE_OBJECTOF = /(?:React\.)?(?:PropTypes\.)?objectOf\((?:React\.)?(?:PropTypes\.)?(\w+)\)/;
function getTypeStr(type) {
switch (type.name.toLowerCase()) {
case 'instanceof':
return `Class(${type.value})`;
case 'enum':
return type.value.map(v => `${v.value}`).join(' │ ');
case 'union':
return type.value.map(t => `${getTypeStr(t)}`).join(' │ ');
case 'arrayof':
return `Array(${getTypeStr(type.value)})`;
case 'custom':
if (type.raw.indexOf('function') !== -1 || type.raw.indexOf('=>') !== -1)
return 'Custom(Function)';
else if (type.raw.toLowerCase().indexOf('objectof') !== -1) {
let m = type.raw.match(RE_OBJECTOF);
if (m && m[1])
return `ObjectOf(${capitalize(m[1])})`;
return 'ObjectOf';
}
return 'Custom';
case 'bool':
return 'Boolean';
case 'func':
return 'Function';
case 'shape':
let shape = type.value;
let rst = {};
Object.keys(shape).forEach(key => {
rst[key] = getTypeStr(shape[key]);
});
return JSON.stringify(rst, null, 2);
default:
return capitalize(type.name);
}
}
module.exports = getTypeStr;