-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetList.js
More file actions
125 lines (113 loc) · 2.57 KB
/
getList.js
File metadata and controls
125 lines (113 loc) · 2.57 KB
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
/* eslint-disable no-underscore-dangle */
module.exports = getList;
const defaultOptions = {
/**
* @param {Object} opts
* @param {string} opts.title
* @param {string} opts.address
* @param {number} opts.index
* @param {number} opts.level
* @return {string}
**/
item(opts) {
const { title, address } = opts;
return `<li><a href="${address}">${title}</a></li>`;
},
/**
* @param {Object} opts
* @param {string} opts.body
* @param {string} opts.typeSlug type(dirname)
* @param {string} opts.typeName mapped type slug
* @param {number} opts.index
* @param {number} opts.level
* @return {string}
**/
group(opts) {
const { body, typeSlug, typeName, index, level } = opts;
return `<li><p id="${level}-${index}" data-type="${typeSlug}">${typeName}</p><ul>${body}</ul></li>`;
},
/**
* @param {Object} opts
* @param {string} opts.body
* @return {string}
**/
tree(opts) {
const { body } = opts;
return `<ul>${body}</ul>`;
},
};
/**
* @typedef {Object} TreeItem
* @property {string} title
* @property {string} address
*/
/**
* @typedef {Object} TreeBase
* @property {TreeItem[]} _docs
* @property {TreeBase} x
*/
/**
* @typedef {Object} Tree
* @property {TreeItem[]} _docs
* @property {TreeBase} x
*/
/**
* @param {Function} typeMap
* @param {Tree} infoTree
* @param {Object} opts
* @param {Function} [opts.tree]
* @param {Function} [opts.group]
* @param {Function} [opts.item]
* @param {boolean} [set=false]
* @param {string} [typeSlug=.]
* @param {number} [level=0]
* @param {number} [index=-1]
* @return {string}
**/
function getList(
typeMap,
infoTree,
opts,
set = false,
typeSlug = '.',
level = 0,
index = -1
) {
if (!infoTree) return '';
let options;
if (!set) {
options = Object.assign({}, defaultOptions, opts);
} else {
options = opts;
}
const { item, group, tree } = options;
let body = Object.keys(infoTree)
.sort()
.map((newTypeSlug, newIndex) => {
if (newTypeSlug === '_docs') return '';
return getList(
typeMap,
infoTree[newTypeSlug],
options,
true,
newTypeSlug,
level + 1,
newIndex
);
})
.join('');
if (Array.isArray(infoTree._docs)) {
body += infoTree._docs
.map(({ title, address }, _index) =>
item({
title,
address,
level,
index: _index,
})
)
.join('');
}
if (level === 0) return tree({ body });
return group({ body, typeSlug, index, level, typeName: typeMap(typeSlug) });
}