This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
/
conditional-sections.js
96 lines (84 loc) · 2.85 KB
/
conditional-sections.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
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const hash = require('object-hash');
const { setdefault, type, each } = require('ferrum');
function selectstrain(context, { request, logger }) {
const cont = setdefault(context, 'content', { mdast: {} });
const params = request.params || {};
const { strain } = params;
const sections = cont.mdast.children;
if (!strain || !sections) {
return;
}
logger.debug(`Filtering sections not intended for strain ${strain}`);
each(sections, (section) => {
const meta = setdefault(section, 'meta', {});
const strainList = type(meta.strain) === Array ? meta.strain : [meta.strain];
meta.hidden = Boolean(meta.strain && meta.strain !== [] && !strainList.includes(strain));
});
}
function testgroups(sections = []) {
return sections
.filter(({ meta = {} } = {}) => !!meta.test)
.reduce((groups, section) => {
if (groups[section.meta.test]) {
groups[section.meta.test].push(section);
} else {
groups[section.meta.test] = [section];
}
return groups;
}, {});
}
/**
* Generate a stable sort order based on a random seed.
* @param {String} strain random seed
*/
function strainhashsort(strain = 'default') {
return function compare(left, right) {
const lhash = hash({ strain, val: left });
const rhash = hash({ strain, val: right });
return lhash.localeCompare(rhash);
};
}
function pick(groups = {}, strain = 'default') {
return Object.keys(groups)
.reduce((selected, group) => {
const candidates = groups[group];
candidates.sort(strainhashsort(strain));
if (candidates.length) {
[selected[group]] = candidates; // eslint prefers array destructing here
}
return selected;
}, {});
}
function selecttest(context, { request }) {
const cont = setdefault(context, 'content', { mdast: {} });
const params = request.params || {};
const { strain } = params;
const sections = cont.mdast.children;
if (!strain || !sections) {
return;
}
const selected = pick(testgroups(sections), strain);
each(sections, (section) => {
if (!section.meta || !section.meta.test) {
return;
}
section.meta.hidden = !(section === selected[section.meta.test]);
});
}
module.exports = {
selectstrain,
testgroups,
selecttest,
pick,
};