/
normalizations.js
112 lines (112 loc) · 3.56 KB
/
normalizations.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const INPUT_EXTENSIONS = Object.freeze({
ast: "json",
json: "json",
msc: "mscgen",
mscgen: "mscgen",
mscin: "mscgen",
msgenny: "msgenny",
seq: "mscgen",
xu: "xu",
});
const OUTPUT_EXTENSIONS = Object.freeze({
ast: "json",
dot: "dot",
doxygen: "doxygen",
jpeg: "jpeg",
jpg: "jpeg",
json: "json",
msc: "mscgen",
mscgen: "mscgen",
mscin: "mscgen",
msgenny: "msgenny",
png: "png",
seq: "mscgen",
svg: "svg",
xu: "xu",
});
/**
* Given a filename in pString, returns what language is probably
* contained in that file, judging from the extension (the last dot
* in the string to end-of-string)
*
* When in doubt returns pDefault
*
* @param {string} pString - filename
* @param {object} pExtensionMap - a dictionary with
* extension : classification pairs
* @param {string} pDefault - the default to return when the extension
* does not occur in the extension map
* @return {string} - language. Possible values: LHS of the passed
* extension map.
*/
function classifyExtension(pString, pExtensionMap, pDefault) {
if (!pString) {
return pDefault;
}
const lPos = pString.lastIndexOf(".");
if (lPos > -1) {
const lExt = pString.slice(lPos + 1);
if (pExtensionMap[lExt]) {
return pExtensionMap[lExt];
}
}
return pDefault;
}
function deriveOutputFromInput(pInputFrom, pOutputType) {
if (!pInputFrom || "-" === pInputFrom) {
return;
}
return path
.join(path.dirname(pInputFrom), path.basename(pInputFrom, path.extname(pInputFrom)))
.concat(".")
.concat(pOutputType);
}
function determineOutputTo(pOutputTo, pInputFrom, pOutputType) {
return Boolean(pOutputTo)
? pOutputTo
: deriveOutputFromInput(pInputFrom, pOutputType);
}
function determineInputType(pInputType, pInputFrom) {
if (pInputType) {
return pInputType === "ast" ? "json" : pInputType;
}
return classifyExtension(pInputFrom, INPUT_EXTENSIONS, "mscgen");
}
function determineOutputType(pOutputType, pOutputTo, pParserOutput) {
if (Boolean(pParserOutput)) {
return "json";
}
if (Boolean(pOutputType)) {
return pOutputType === "ast" ? "json" : pOutputType;
}
if (Boolean(pOutputTo)) {
return classifyExtension(pOutputTo, OUTPUT_EXTENSIONS, "svg");
}
return "svg";
}
/**
* transforms the given argument and options to a uniform format
*
* - guesses the input type when not given
* - guesses the output type when not given
* - gueses the filename to output to when not given
* - translates parserOutput to a regular output type
*
* @param {string} pArgument an argument (containing the filename to parse)
* @param {object} pOptions a commander options object
* @return {object} a commander options object with options 'normalized'
*/
function normalize(pArgument, pOptions) {
const lRetval = JSON.parse(JSON.stringify(pOptions));
lRetval.inputFrom = Boolean(pArgument) ? pArgument : pOptions.inputFrom;
lRetval.inputType = determineInputType(pOptions.inputType, lRetval.inputFrom);
lRetval.outputType = determineOutputType(pOptions.outputType, pOptions.outputTo, pOptions.parserOutput);
lRetval.outputTo = determineOutputTo(pOptions.outputTo, lRetval.inputFrom, lRetval.outputType);
lRetval.regularArcTextVerticalAlignment =
pOptions.verticalAlignment || "middle";
return lRetval;
}
exports.normalize = normalize;