-
Notifications
You must be signed in to change notification settings - Fork 399
/
index.js
91 lines (84 loc) · 3.13 KB
/
index.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
const attachFormat = () => {
const input = document.getElementById('input');
const output = document.getElementById('output');
const error = document.getElementById('error');
const language = document.getElementById('language');
const tabWidth = document.getElementById('tabWidth');
const useTabs = document.getElementById('useTabs');
const keywordCase = document.getElementById('keywordCase');
const dataTypeCase = document.getElementById('dataTypeCase');
const functionCase = document.getElementById('functionCase');
const identifierCase = document.getElementById('identifierCase');
const indentStyle = document.getElementById('indentStyle');
const logicalOperatorNewline = document.getElementById('logicalOperatorNewline');
const expressionWidth = document.getElementById('expressionWidth');
const lineBetweenQueries = document.getElementById('lineBetweenQueries');
const denseOperators = document.getElementById('denseOperators');
const newlineBeforeSemicolon = document.getElementById('newlineBeforeSemicolon');
function showOutput(text) {
output.value = text;
output.style.display = 'block';
error.style.display = 'none';
}
function showError(text) {
error.innerHTML = text;
output.style.display = 'none';
error.style.display = 'block';
}
function format() {
try {
const config = {
language: language.options[language.selectedIndex].value,
tabWidth: tabWidth.value,
useTabs: useTabs.checked,
keywordCase: keywordCase.options[keywordCase.selectedIndex].value,
dataTypeCase: dataTypeCase.options[dataTypeCase.selectedIndex].value,
functionCase: functionCase.options[functionCase.selectedIndex].value,
identifierCase: identifierCase.options[identifierCase.selectedIndex].value,
indentStyle: indentStyle.options[indentStyle.selectedIndex].value,
logicalOperatorNewline:
logicalOperatorNewline.options[logicalOperatorNewline.selectedIndex].value,
expressionWidth: expressionWidth.value,
lineBetweenQueries: lineBetweenQueries.value,
denseOperators: denseOperators.checked,
newlineBeforeSemicolon: newlineBeforeSemicolon.checked,
};
showOutput(sqlFormatter.format(input.value, config));
} catch (e) {
if (e instanceof sqlFormatter.ConfigError) {
showError(`<h2>Configuration error</h2><p>${e.message}</p>`);
} else {
showError(
`
<h2>An Unexpected Error Occurred</h2>
<p><strong>${e.message}</strong></p>
<p>
Please report this at
<a href="https://github.com/sql-formatter-org/sql-formatter/issues">Github issues page.<a>
</p>
<p>Stack Trace:</p>
<pre>${e.stack.toString()}</pre>
`
);
}
}
}
input.addEventListener('input', format);
[
language,
tabWidth,
useTabs,
keywordCase,
dataTypeCase,
functionCase,
identifierCase,
indentStyle,
logicalOperatorNewline,
expressionWidth,
lineBetweenQueries,
denseOperators,
newlineBeforeSemicolon,
].forEach(option => option.addEventListener('change', format));
format();
};
document.addEventListener('DOMContentLoaded', attachFormat);