-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi2html.js
executable file
·53 lines (52 loc) · 1.48 KB
/
api2html.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
let cache = [];
let uniqueId = new Date().getTime();
let invalidKey = [];
function circleApi(api, aprentMap) {
if (!api || (typeof api !== 'object' && typeof api !== 'function')) {
return;
}
let map = {};
try {
for (const key of Reflect.ownKeys(api)) {
if (invalidKey.indexOf(key) === -1) {
map[key] = api[key];
}
else {
console.log('invalid key: ', key);
}
}
for (let key in map) {
let fn = '';
if (typeof map[key] === 'function' && map[key].toString) {
fn = map[key].toString();
}
let currentMap = {
name: `${aprentMap.name}.${key}`,
value: map[key],
fn: fn,
children: [],
uniqueId: uniqueId++
};
aprentMap.children.push(currentMap);
if (typeof map[key] !== 'string' && key !== 'constructor' && cache.indexOf(map[key]) === -1) {
cache.push(map[key]);
circleApi(map[key], currentMap);
}
}
}
catch (e) {
console.log('[API 遍历出错]', e);
}
}
module.exports = function (api, parent, option) {
let apiMap = {
children: [],
name: parent || "root",
value: api
};
invalidKey = option && option.invalidKey || invalidKey;
if (api) {
circleApi(api, apiMap);
}
return apiMap;
};