-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
108 lines (104 loc) · 3.9 KB
/
demo.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Unit Conversions</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/sandstone/bootstrap.min.css" crossorigin="anonymous">
<style>
#help {
border: 1px solid #ddd;
}
main > section.row {
margin-bottom: 1em;
}
form + section { margin-top: 1em; }
ul.list-group { width: 100%; }
#help { max-height: 92vh; overflow-y: scroll; }
</style>
</head>
<body>
<main>
<div class="container">
<section class="row">
<h1>Unit Conversions</h1>
</section>
<section class="row">
<div class="col-4">
<h2>Input</h2>
<div class="position-static">
<form class="convert-form form">
<div class="form-group">
<div class="input-group mb-3">
<span class="input-group-text">Convert:</span>
<input id="convert-input" class="form-control" type="text">
</div>
<div class="input-group mb-3">
<input id="parse" value="sqrt(3)" type="text" class="form-control" placeholder="parse">
<button class="btn btn-secondary" type="button" id="do-parse">Parse</button>
<span id="parse-result" style="width:5em" class="input-group-text"></span>
</div>
</div>
</form>
<section id="convert-results"></section>
</div>
</div>
<div class="col-8">
<h2>Help</h2>
<section id="help" class="row"></section>
</div>
</section>
</div>
</main>
<script type="module">
import {
nlConvert,
unitsOfMeasure,
Unit,
nlParser,
tokenScanner, Interpreter
} from "./nlconvert.js";
const selector = '#convert-input';
const inputEl = document.querySelector(selector);
inputEl.focus();
inputEl.value = "50 f";
const interpreter = new Interpreter({val: 50});
document.querySelector('#do-parse').addEventListener('click', e => {
const input = document.querySelector('#parse').value;
if(input) {
const tokens = tokenScanner(input);
console.log(tokens);
if(tokens) {
let result = interpreter.evaluate(tokens);
console.log(result);
if(Math.floor(result) !== result) {
result = result.toPrecision(6)
}
else {
result = result.toString();
}
document.querySelector('#parse-result').textContent = result;
}
}
});
// extend our table of measurements
const fooUnit = unitsOfMeasure.addUnit('foobar^2', 's', 'foo')
unitsOfMeasure.addConverter('acre', fooUnit, 42);
nlConvert(selector, '#convert-results', {'helpElement': '#help'});
console.log("Sample parsing:");
console.table([
"1 gr butter",
"12 F",
"3 4/5 oz",
"45 56/67",
"1/2 m",
"23/4",
"-2/3",
"+2/3"
].map(i => {
const val = nlParser.parse(i);
return [i, val ? val.value + ', ' + val.label : null];
}));
</script>
</body>
</html>