-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.html
233 lines (226 loc) · 7.04 KB
/
main.html
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Format Quantity Test Page</title>
<style>
body {
margin: 1rem;
font-family: arial, sans-serif;
}
h2 {
margin-top: 0;
}
h4 {
margin-bottom: 0.8rem;
}
pre {
border: 1px solid darkgray;
padding: 0.75rem;
border-radius: 5px;
background-color: lightgray;
}
.header {
display: flex;
align-items: flex-start;
justify-content: space-between;
}
.header h2 {
margin: 0;
padding: 0;
}
.links {
display: flex;
column-gap: 1rem;
}
.main {
display: grid;
grid-template-columns: 1fr 1fr;
}
.option-grid {
display: grid;
grid-template-columns: 10rem 14rem;
column-gap: 0.5rem;
row-gap: 0.25rem;
}
#tests {
font-family: 'Courier New', Courier, monospace;
font-size: small;
display: grid;
grid-template-columns: 0.1fr 1fr 0.2fr 0.2fr;
gap: 0.5rem;
}
#tests > .desc {
font-weight: bold;
grid-column-start: 1;
grid-column-end: 5;
padding-top: 1rem;
}
#tests > .th {
border-bottom: 1px solid #cccccc;
}
#tests > .th,
#tests > .pass {
text-align: center;
}
</style>
</head>
<body>
<div class="header">
<h2>Format Quantity Test Page</h2>
<div class="links">
<a href="https://jakeboone02.github.io/format-quantity/">docs</a>
<a href="https://github.com/jakeboone02/format-quantity">github</a>
<a href="https://www.npmjs.com/package/format-quantity">npm</a>
</div>
</div>
<div class="main">
<div>
<h4>Input</h4>
<input id="input" type="number" value="1.5" style="width: auto" />
<h4>Options</h4>
<div class="option-grid">
<div>Vulgar fractions</div>
<div>
<input type="checkbox" id="vulgarFractions" />
</div>
<div>Fraction slash</div>
<div>
<input type="checkbox" id="fractionSlash" />
</div>
<div>Tolerance</div>
<div>
<input
type="number"
id="tolerance"
value="0.0075"
style="width: auto"
/>
</div>
<div>Roman numerals</div>
<div>
<input type="checkbox" id="romanNumerals" />
</div>
<div>
<button type="button" id="default">Reset options</button>
</div>
</div>
</div>
<div>
<h4>Call</h4>
<pre id="call">Loading...</pre>
<h4>Result</h4>
<pre id="result">Loading...</pre>
</div>
</div>
<hr />
<h4>Tests</h4>
<p>
<em><small>Tests use the default tolerance.</small></em>
</p>
<div id="tests">Loading...</div>
<script type="module" src="/src/dev.ts"></script>
<script>
const getInput = () => {
/** @type HTMLInputElement */
const inputBox = document.getElementById('input');
return inputBox.value;
};
const getOptions = () => {
/** @type HTMLInputElement */
const vulgarFractionsEl = document.getElementById('vulgarFractions');
/** @type HTMLInputElement */
const toleranceEl = document.getElementById('tolerance');
/** @type HTMLInputElement */
const fractionSlashEl = document.getElementById('fractionSlash');
/** @type HTMLInputElement */
const romanNumeralsEl = document.getElementById('romanNumerals');
const vulgarFractions = vulgarFractionsEl.checked;
const tolerance = parseFloat(toleranceEl.value);
const fractionSlash = fractionSlashEl.checked;
const romanNumerals = romanNumeralsEl.checked;
return { vulgarFractions, tolerance, fractionSlash, romanNumerals };
};
const refresh = () => {
const input = getInput();
const optionsArr = [];
const options = getOptions();
if (options.vulgarFractions) {
optionsArr.push('vulgarFractions: true');
}
if (options.tolerance !== defaultTolerance) {
optionsArr.push(`tolerance: ${parseFloat(options.tolerance)}`);
}
if (options.fractionSlash) {
optionsArr.push('fractionSlash: true');
}
if (options.romanNumerals) {
optionsArr.push('romanNumerals: true');
}
let optionsStr = '';
if (optionsArr.length > 0) {
optionsStr = `, { ${optionsArr.join(', ')} }`;
}
document.getElementById('call').innerText =
`formatQuantity(${input}${optionsStr})`;
document.getElementById('result').innerText = JSON.stringify(
formatQuantity(input, options)
);
};
// Tests
const testsEl = document.getElementById('tests');
const headerRow =
'<div class="th">pass?</div><div class="th">call</div><div class="th">expect</div><div class="th">actual</div>';
const runTests = () => {
let testResultHTML = '';
for (const [description, tests] of Object.entries(
formatQuantityTests
)) {
testResultHTML += `<div class="desc">${description}</div>`;
testResultHTML += headerRow;
for (const [quantity, result, options] of tests) {
const actual = formatQuantity(quantity, options);
const opts = typeof options === 'undefined' ? '' : options;
const optionString =
opts === ''
? ''
: `, ${JSON.stringify(opts).replace(/([,:])/g, '$1 ')}`;
testResultHTML += `<div class="pass">${
actual === result ? '✅' : '❌'
}</div><div>formatQuantity(${JSON.stringify(
quantity
)}${optionString})</div><div>${JSON.stringify(
result
)}</div><div>${JSON.stringify(actual)}</div>`;
}
}
testsEl.innerHTML = testResultHTML;
};
const inputIDs = ['input', 'tolerance'];
const booleanOptionIDs = [
'vulgarFractions',
'fractionSlash',
'romanNumerals',
];
for (const id of [...inputIDs, ...booleanOptionIDs]) {
document.getElementById(id).addEventListener('change', refresh);
}
for (const id of inputIDs) {
document.getElementById(id).addEventListener('keyup', refresh);
}
document.getElementById('default').addEventListener('click', () => {
for (const id of booleanOptionIDs) {
document.getElementById(id).checked = false;
}
document.getElementById('tolerance').value = defaultTolerance;
refresh();
});
setTimeout(() => {
document.getElementById('tolerance').value = defaultTolerance;
refresh();
runTests();
}, 250);
</script>
</body>
</html>