-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubset.js
61 lines (53 loc) · 1.29 KB
/
subset.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
// @ts-check
/**
* Report subset object.
* @typedef {Object} subset
* @property {function():string} getId
* @property {function():string} getName
* @property {function():string} getTitle
* @property {function():string} getSubTitle
*/
/**
* Factory function for creating Subset object.
* @param {Object} data initializes report.
* @param {string} data._id - identity for persistence.
* @param {string} data.name
* @param {Object[]} data.sections
* @returns {subset} Subset object
*/
function createSubset(data) {
const { _id } = data;
const { name } = data;
const { sections } = data;
function getId() {
return _id;
}
function getName() {
return name;
}
function getTitle() {
const section = sections.find((elem) => elem.style === "th1");
if (!section) {
throw new TypeError("No header section found.");
}
return section.value;
}
function getSubTitle() {
const section = sections.find((elem) => elem.style === "th2");
return section ? section.value : "";
}
return Object.freeze({
getId,
getName,
getTitle,
getSubTitle,
});
}
/**
* Factory function for creating array of Subset objects.
* @param {Object[]} data
* @returns {subset[]}
*/
export default function (data) {
return data.map((elem) => createSubset(elem));
}