-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathspecfix.js
60 lines (52 loc) · 2.19 KB
/
specfix.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
const util = require('util');
const fs = require('fs');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const keywordsMapping = {};
(async function start() {
const specContent = await readFile('./spec.json');
const specJson = JSON.parse(specContent);
function process(section) {
const negativeKeywords = [];
const conditionalKeywords = [];
// eslint-disable-next-line no-restricted-syntax
for (const obj of section) {
let prevNegative = false;
let prevCondition = false;
// eslint-disable-next-line no-restricted-syntax
for (const element of obj.elements) {
if (typeof element === 'string') {
const canContinue = /,/.test(element);
const ifThenCondition = /:/.test(element);
const hasOrAnd = /(\b(and|or)\b)/.test(element);
prevNegative = /(\b([Nn]o|[Nn]ot)\b(?! (more than one)))/.test(element) || (prevNegative && (hasOrAnd || canContinue));
prevCondition = /(\b([Ii]f|[Uu]nless)\b)/.test(element) || (prevCondition && (hasOrAnd || canContinue || ifThenCondition));
} else {
keywordsMapping[element.hashText] = element;
if (prevNegative) {
negativeKeywords.push(element.hashText);
}
if (prevCondition) {
conditionalKeywords.push(element.hashText);
}
}
}
}
return {
negativeKeywords: [...new Set(negativeKeywords)].map((item) => item.toLowerCase()),
conditionalKeywords: [...new Set(conditionalKeywords)].map((item) => item.toLowerCase()),
};
}
const processed = specJson.result.map((tag) => {
// eslint-disable-next-line no-param-reassign
tag.props.sections = tag.props.sections || {};
let sectionProps = process(tag.props.Categories);
// eslint-disable-next-line no-param-reassign
tag.props.sections.Categories = sectionProps;
sectionProps = process(tag.props.ContentModel);
// eslint-disable-next-line no-param-reassign
tag.props.sections.ContentModel = sectionProps;
return tag;
});
await writeFile('./spec_fixed.json', JSON.stringify({ version: specJson.version, keywordsMapping, result: processed }, ' ', 2));
}());