-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.svelte
More file actions
320 lines (284 loc) · 9.82 KB
/
App.svelte
File metadata and controls
320 lines (284 loc) · 9.82 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<script lang="ts">
import { Decimal } from "decimal.js";
import { getPlanck, genericConvert } from "./lib/convert";
import {
type ScaledQuantity,
type Prefix,
type Unit,
type Dimensionality,
emptyPrefix,
allUnits,
unitsNotToOutputTo,
unitLookup,
} from "./lib/vocab";
Decimal.set({ precision: 70 });
/**
* Formats a coefficient for display:
* - Scientific notation (ax10^b) for |value| >= 10^7 or |value| < 10^-5
* - 2 decimal places (without trailing zeros) for 1 <= |value| < 100
* - Comma separators for 1000 <= |value| < 10^7
* - No decimals for 100 <= |value| < 10^7
* - Normal notation for 10^-5 <= |value| < 10^7
*/
function formatCoefficient(coeff: Decimal): string {
const absValue = coeff.abs().toNumber();
// Use scientific notation for extreme values
if (absValue >= 1e7 || (absValue < 1e-5 && absValue !== 0)) {
const exponent = Math.floor(Math.log10(absValue));
const mantissa = coeff.div(Decimal(10).pow(exponent));
const mantissaStr = mantissa.toFixed(2).replace(/\.?0+$/, "");
// Simplify "1×10^b" to just "10^b"
if (mantissaStr === "1") {
return `10^${exponent}`;
}
return `${mantissaStr}×10^${exponent}`;
}
// Normal notation with smart decimal handling
if (absValue >= 1000) {
// Add comma separators for values >= 1000
const num = coeff.toFixed(0);
return num.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (absValue >= 100) {
// No decimals for values between 100 and 1000
return coeff.toFixed(0);
} else if (absValue >= 1) {
// 2 decimal places (without trailing zeros) for values between 1 and 100
return coeff.toFixed(2).replace(/\.?0+$/, "");
} else if (absValue === 0) {
return "0";
} else {
// For values between 10^-5 and 1, show 2 significant non-zero decimals
const str = coeff.toFixed(10); // Get enough decimals
const match = str.match(/^-?0\.0*[1-9]\d?/);
const result = match ? match[0] : coeff.toFixed(2);
return result.replace(/\.?0+$/, ""); // Remove trailing zeros
}
}
function displayScaledQuantity(quantity: ScaledQuantity): string {
const coeffString: string = formatCoefficient(quantity.coeff);
const prefixString: string = quantity.prefix.id;
const unitString: string =
quantity.coeff.toNumber() === 1
? quantity.unit.id
: quantity.unit.plural;
return coeffString + " " + prefixString + unitString;
}
function convertAndDisplay(
input: ScaledQuantity,
targetUnit: Unit,
): string {
let prefixedConvertedQuantity: ScaledQuantity;
let unprefixedConvertedQuantity: ScaledQuantity;
try {
prefixedConvertedQuantity = genericConvert(
getPlanck(input),
targetUnit,
true,
);
unprefixedConvertedQuantity = genericConvert(
getPlanck(input),
targetUnit,
false,
);
} catch (e) {
return e instanceof Error ? e.message : "Unknown error";
}
let result = displayScaledQuantity(prefixedConvertedQuantity);
if (prefixedConvertedQuantity.prefix !== emptyPrefix) {
result += ` (${displayScaledQuantity(unprefixedConvertedQuantity)})`;
}
return result;
}
function listCompatibleUnits(inputDim: Dimensionality): Unit[] {
return allUnits.filter(
(unit) =>
unit.planck.dimensionality === inputDim &&
!unitsNotToOutputTo.includes(unit.id),
);
}
function parsePrefixedUnitString(input: string): {
prefix: Prefix;
unit: Unit;
} {
const result = unitLookup.get(input);
if (!result) throw new Error();
return result;
}
function normalizeScientificNotation(input: string): string {
const regex =
/(\d*\.?\d+)\s*(?:[×x*·]\s*10|10)(?:\^|\*\*)?\s*([+-]?\d+)/gi;
return input.replace(regex, (_match, coefficient, exponent) => {
return `${coefficient}e${exponent}`;
});
}
function parseCoeff(input: string): Decimal {
try {
// try to parse it normally
return Decimal(input);
} catch {
// if we get errors, try normalizeScientificNotation()
try {
return Decimal(
normalizeScientificNotation(input.replaceAll(",", "")),
);
} catch {
// if we still get errors, give up and throw an error
throw new Error(
"Failed to parse coefficient; please use standard scientific notation (e.g. 1.6e-4).",
);
}
}
}
function parseInput(input: string): ScaledQuantity {
const splitInput = input.trim().toLowerCase().split(" ");
let coeff: Decimal;
let prefix: Prefix;
let unit: Unit;
switch (splitInput.length) {
case 1: // just prefixed unit
coeff = Decimal(1);
({ prefix, unit } = parsePrefixedUnitString(splitInput[0]));
break;
case 2: // coeff + prefixed unit
coeff = Decimal(parseCoeff(splitInput[0]));
({ prefix, unit } = parsePrefixedUnitString(splitInput[1]));
break;
default: // too many spaces; invalid input
throw new Error();
}
return {
coeff,
prefix,
unit,
};
}
let rawInput: string = $state("");
let output: string[] = $derived.by(() => {
let parsedInput: ScaledQuantity;
try {
parsedInput = parseInput(rawInput);
} catch (e) {
return [e instanceof Error ? e.message : "Couldn't process input"];
}
if (typeof parsedInput === "string") {
return [parsedInput];
}
const compatUnits = listCompatibleUnits(
parsedInput.unit.planck.dimensionality,
);
let convertedUnits: { display: string; coeff: Decimal }[] = [];
compatUnits.forEach((unit) => {
let prefixedConvertedQuantity: ScaledQuantity;
try {
prefixedConvertedQuantity = genericConvert(
getPlanck(parsedInput),
unit,
true,
);
} catch (e) {
return;
}
convertedUnits.push({
display: convertAndDisplay(parsedInput, unit),
coeff: prefixedConvertedQuantity.coeff,
});
});
// Sort by closeness of coefficient to 1
convertedUnits.sort((a, b) => {
const distA = Decimal.log10(a.coeff.abs()).abs();
const distB = Decimal.log10(b.coeff.abs()).abs();
return distA.comparedTo(distB);
});
return convertedUnits.map((item) => item.display);
});
</script>
<main>
<h1>Marks System Converter Tool (msct)</h1>
<p>
<a href="https://github.com/ethmarks/msct">msct</a> is a utility that
converts between the
<a href="https://site-ethmarks.vercel.app/posts/measurement"
>Marks system</a
>, metric system, and customary system.
</p>
<details>
<summary>How to use msct</summary>
<p>
msct inputs come in two parts: a coefficient and a unit. The
coefficient is optional and defaults to 1, but the unit is
mandatory.
</p>
<p>The coefficient can be a simple number or in scientific notation.</p>
<p>
The unit can optionally begin with a prefix (e.g. "kilo") and must
end with a recognized unit of measurement.
</p>
<p>An example input is "1.6e3 decilens".</p>
<p>Recognized units of measurement are:</p>
<ul>
{#each allUnits as unit}
<li>
{unit.id}
{unit.aliases ? `(${unit.aliases.join(", ")})` : ""}
</li>
{/each}
</ul>
<hr />
<p>
msct will process your input by converting it into every compatible
unit.
</p>
<p>
For units that accept prefixes, it will automatically add them but
include the base value in parenthesis.
</p>
<p>
The converted units are sorted based on how close their coefficients
are to 1, indicating how good of a match they are to the input.
</p>
</details>
<hr />
<form id="converter">
<input
id="unifiedInput"
type="text"
placeholder="1 len"
bind:value={rawInput}
/>
<output for="unifiedInput">
{#each output as outputString}
{outputString}<br />
{/each}
</output>
</form>
</main>
<footer>
<p>
Created by <a href="https://github.com/ethmarks">ethmarks</a>
|
<a href="https://github.com/ethmarks/msct">Source Code</a>
</p>
</footer>
<style>
form {
display: flex;
flex-direction: column;
}
summary {
font-weight: 400;
}
input {
width: 100%;
padding: 0.6rem 1rem;
font-weight: 600;
outline: #447ae5 solid 0.1rem;
transition: outline-width 0.1s;
}
input:active,
input:focus {
outline-width: 0.2rem;
}
footer {
text-align: center;
}
</style>