-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
128 lines (117 loc) · 4.97 KB
/
script.js
File metadata and controls
128 lines (117 loc) · 4.97 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
126
127
128
const outputTypeOptions = document.querySelector('.options');
const outputTypeParameters = document.querySelector('.parameters');
const outputTypeArray = document.querySelector('.array');
const inputTypeValues = document.querySelector('.inputTypeValues');
const inputTypeNames = document.querySelector('.inputTypeNames');
const inputTypeNamesAndTypes = document.querySelector('.inputTypeNamesAndTypes');
const separatorComma = document.querySelector('.separatorComma'); // only 2 radio buttons, one of them is always 'checked'
const valueFirst = document.querySelector('.valueFirst') // only 2 radio buttons, one of them is always 'checked'
const inputOptions = document.querySelector('.inputOptions');
const btnSubmit = document.querySelector('.btnSubmit');
const resultContent = document.querySelector('.resultContent');
// convert strings with _ or camelCase to proper labels
function nameToLabel(str) {
if (typeof str !== "string") return str;
const abbreviations = ["Id", "Url","Html"];
const name = str.replaceAll('_', ' ')
const nameArr = name.split(/(?<=[a-z\d])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|(?<=[a-z])(?=[\d])|\s/);
return nameArr.map(word => {
let capWord = word.replace(word.charAt(0), word.charAt(0).toUpperCase()); // Capitalize the first character
if (abbreviations.includes(capWord)) return capWord.toUpperCase(); // abbreviation all upper case
return capWord;
}).join(' ');
};
// from a list of values create array with {value, label} objects (options for Select dropdown list)
function parseOptionsFromValues(input, separator) {
const separatedBy = separator.checked ? ',' : '\n'
const output = input.split(separatedBy)
.map(element => ({
"value": element,
"label": nameToLabel(element)
}));
return JSON.stringify(output, null, 2)
};
// from a list of value,name or name,value create array with {value, label} objects
function parseOptionsFromValuesAndNames(input, order) {
const valueOrder = order.checked ? 0 : 1;
const nameOrder = !order.checked ? 0 : 1;
const output = input.split('\n')
.map(element => ({
"value": element.split(',')[valueOrder],
"label": element.split(',')[nameOrder]
}));
return JSON.stringify(output, null, 2);
};
// from a list of values create array of parameter objects
function parseParametersFromValues(input, separator) {
const separatedBy = separator.checked ? ',' : '\n'
const output = input.split(separatedBy)
.map(element => ({
"name": element,
"label": nameToLabel(element),
"type": "text"
}));
return JSON.stringify(output, null, 2)
};
// from a list of value,name or name,value create array with {value, label} objects
function parseParametersFromValuesAndNames(input, order) {
const valueOrder = order.checked ? 0 : 1;
const nameOrder = !order.checked ? 0 : 1;
const output = input.split('\n')
.map(element => ({
"name": element.split(',')[valueOrder],
"label": element.split(',')[nameOrder],
"type": "text"
}));
return JSON.stringify(output, null, 2);
};
// Custom typeMap
const typeMap = {
"String": "text",
"Datetime": "date",
"Date":"string",
"Int":"number",
"Decimal":"number",
"Short":"number",
"Byte":"number",
"string": "text",
"datetime": "date",
"date":"string",
"int":"number",
"decimal":"number",
"short":"number",
"byte":"number"
};
// from a list of name,type create array with {name, label, type} objects for mappable parameters/interface
function parseParametersFromValuesAndTypes(input, order) {
const output = input.split('\n')
.map(element => ({
"name": element.split(',')[0],
"label": nameToLabel(element.split(',')[0]),
"type": typeMap[element.split(',')[1]] || element.split(',')[1],
}));
return JSON.stringify(output, null, 2);
};
// from a list of value,name or name,value create array with {value, label} objects
function parseArrayFromValues(input, separator) {
const separatedBy = separator.checked ? ',' : '\n'
const output = input.split(separatedBy)
return JSON.stringify(output)
};
// calls the function and displays the results after 'submit' button is clicked
btnSubmit.addEventListener('click', function (e) {
e.preventDefault();
if (!inputOptions.value) alert('Insert values');
if (outputTypeOptions.checked) {
if (inputTypeValues.checked) resultContent.textContent = parseOptionsFromValues(inputOptions.value, separatorComma);
if (!inputTypeValues.checked) resultContent.textContent = parseOptionsFromValuesAndNames(inputOptions.value, valueFirst);
};
if (outputTypeParameters.checked) {
if (inputTypeValues.checked) resultContent.textContent = parseParametersFromValues(inputOptions.value, separatorComma);
if (inputTypeNames.checked) resultContent.textContent = parseParametersFromValuesAndNames(inputOptions.value, valueFirst);
if (inputTypeNamesAndTypes.checked) resultContent.textContent = parseParametersFromValuesAndTypes(inputOptions.value, valueFirst);
};
if (outputTypeArray.checked) {
if (inputTypeValues.checked) resultContent.textContent = parseArrayFromValues(inputOptions.value, separatorComma);
}
});