Gets the name of a number, even if it's stupidly big. Supersedes TheoryOfNekomata/number-name.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

244 lines
7.0 KiB

  1. <!DOCTYPE html>
  2. <html lang="en-PH">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>@modal-sh/numerica-core</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <style>
  8. .main-form {
  9. position: fixed;
  10. top: 0;
  11. left: 0;
  12. width: 100%;
  13. height: 100%;
  14. display: flex;
  15. flex-direction: column;
  16. align-items: stretch;
  17. }
  18. .main-form > fieldset {
  19. display: contents;
  20. }
  21. .main-form > fieldset > div {
  22. padding: 1rem;
  23. box-sizing: border-box;
  24. display: flex;
  25. gap: 1rem;
  26. }
  27. .main-form > fieldset > legend {
  28. position: absolute;
  29. left: -999999px;
  30. }
  31. .main-form > div {
  32. flex-direction: column;
  33. display: flex;
  34. flex: auto;
  35. }
  36. .main-form > div > textarea {
  37. flex: auto;
  38. width: 100%;
  39. height: 0;
  40. font-size: 1.5rem;
  41. padding: 1rem;
  42. border: 0;
  43. outline: 0;
  44. resize: none;
  45. box-sizing: border-box;
  46. }
  47. .main-form > div > textarea:invalid {
  48. color: red;
  49. }
  50. .select {
  51. font: inherit;
  52. padding: 0 1rem;
  53. }
  54. .checkbox > input {
  55. position: absolute;
  56. left: -999999px;
  57. }
  58. .checkbox > input + span {
  59. display: inline-flex;
  60. align-items: center;
  61. justify-content: center;
  62. height: 3rem;
  63. padding: 0 1rem;
  64. border: 1px solid #ccc;
  65. border-radius: 0.25rem;
  66. cursor: pointer;
  67. }
  68. .checkbox > input:checked + span {
  69. border-color: #000;
  70. background-color: #000;
  71. color: #fff;
  72. }
  73. @media (min-width: 1080px) {
  74. .main-form > div {
  75. flex-direction: row;
  76. }
  77. .main-form > div > textarea {
  78. width: 0;
  79. height: 100%;
  80. }
  81. }
  82. </style>
  83. </head>
  84. <body>
  85. <form id="mainForm" class="main-form">
  86. <fieldset>
  87. <legend>
  88. Options
  89. </legend>
  90. <div>
  91. <select class="select" aria-label="Locale" name="system.locale">
  92. <option value="enUS">
  93. English (US)
  94. </option>
  95. </select>
  96. <select class="select" aria-label="Variant" name="system.count">
  97. <option value="shortCount">
  98. Short Count
  99. </option>
  100. <option value="longCount">
  101. Long Count
  102. </option>
  103. </select>
  104. <label class="checkbox">
  105. <input type="checkbox" name="shortenMillia">
  106. <span>Shorten millia</span>
  107. </label><label class="checkbox">
  108. <input type="checkbox" name="addTensDashes" checked>
  109. <span>Add dashes to tens</span>
  110. </label><label class="checkbox">
  111. <input type="checkbox" name="oneGroupPerLine" checked>
  112. <span>One group per line</span>
  113. </label><!--<label class="checkbox">
  114. <input type="groupDigits" name="groupDigits" checked>
  115. <span>Group digits</span>
  116. </label>-->
  117. </div>
  118. </fieldset>
  119. <div>
  120. <textarea aria-label="Number" name="number" placeholder="1234"></textarea>
  121. <textarea aria-label="Name" name="name" placeholder="one thousand&#x0A;two hundred thirty-four"></textarea>
  122. </div>
  123. </form>
  124. <script type="module">
  125. import { stringify, parse, systems } from './numerica.js';
  126. function MainForm(el) {
  127. const numberInput = el.querySelector('[name="number"]');
  128. const nameInput = el.querySelector('[name="name"]');
  129. const addTensDashesCheckbox = el.querySelector('[name="addTensDashes"]');
  130. const shortenMilliaCheckbox = el.querySelector('[name="shortenMillia"]');
  131. const oneGroupPerLineCheckbox = el.querySelector('[name="oneGroupPerLine"]');
  132. const localeSelect = el.querySelector('[name="system.locale"]');
  133. const variantSelect = el.querySelector('[name="system.count"]');
  134. const options = {
  135. stringify: {
  136. makeGroupOptions: {},
  137. finalizeOptions: {},
  138. },
  139. parse: {
  140. type: 'bigint',
  141. },
  142. };
  143. const createNamePlaceholder = (theOptions) => {
  144. return stringify(1234, theOptions.stringify);
  145. };
  146. numberInput.addEventListener('input', (e) => {
  147. e.currentTarget.setCustomValidity('');
  148. nameInput.value = '';
  149. if (e.currentTarget.value.trim() === '') {
  150. return;
  151. }
  152. try {
  153. nameInput.value = stringify(e.currentTarget.value, options.stringify);
  154. } catch (err) {
  155. console.error(err);
  156. e.currentTarget.setCustomValidity('Invalid number.');
  157. }
  158. });
  159. nameInput.addEventListener('input', (e) => {
  160. e.currentTarget.setCustomValidity('');
  161. numberInput.value = '';
  162. if (e.currentTarget.value.trim() === '') {
  163. return;
  164. }
  165. try {
  166. numberInput.value = parse(e.currentTarget.value, options.parse)
  167. .toString();
  168. // TODO group digits function from system.
  169. } catch (err) {
  170. console.error(err);
  171. e.currentTarget.setCustomValidity('Invalid name.');
  172. }
  173. });
  174. addTensDashesCheckbox.addEventListener('change', (e) => {
  175. options.stringify.makeGroupOptions.addTensDashes = e.currentTarget.checked;
  176. numberInput.dispatchEvent(new Event('input'));
  177. nameInput.placeholder = createNamePlaceholder(options);
  178. });
  179. shortenMilliaCheckbox.addEventListener('change', (e) => {
  180. options.stringify.makeGroupOptions.shortenMillia = e.currentTarget.checked;
  181. numberInput.dispatchEvent(new Event('input'));
  182. nameInput.placeholder = createNamePlaceholder(options);
  183. });
  184. oneGroupPerLineCheckbox.addEventListener('change', (e) => {
  185. options.stringify.finalizeOptions.oneGroupPerLine = e.currentTarget.checked;
  186. numberInput.dispatchEvent(new Event('input'));
  187. nameInput.placeholder = createNamePlaceholder(options);
  188. });
  189. localeSelect.addEventListener('change', (e) => {
  190. const variantOptions = Array.from(variantSelect.options);
  191. variantOptions.forEach((option) => {
  192. option.disabled = !systems[e.currentTarget.value][option.value];
  193. });
  194. variantSelect.value = variantOptions.find((option) => !option.disabled).value;
  195. options.stringify.system = systems[e.currentTarget.value][variantSelect.value];
  196. options.parse.system = systems[e.currentTarget.value][variantSelect.value];
  197. numberInput.dispatchEvent(new Event('input'));
  198. nameInput.placeholder = createNamePlaceholder(options);
  199. });
  200. variantSelect.addEventListener('change', (e) => {
  201. options.stringify.system = systems[localeSelect.value][e.currentTarget.value];
  202. options.parse.system = systems[localeSelect.value][e.currentTarget.value];
  203. numberInput.dispatchEvent(new Event('input'));
  204. nameInput.placeholder = createNamePlaceholder(options);
  205. });
  206. options.stringify.makeGroupOptions.addTensDashes = addTensDashesCheckbox.checked;
  207. options.stringify.makeGroupOptions.shortenMillia = shortenMilliaCheckbox.checked;
  208. options.stringify.finalizeOptions.oneGroupPerLine = oneGroupPerLineCheckbox.checked;
  209. options.stringify.system = systems[localeSelect.value][variantSelect.value];
  210. options.parse.system = systems[localeSelect.value][variantSelect.value];
  211. Array.from(variantSelect.options).forEach((option) => {
  212. option.disabled = !systems[localeSelect.value][option.value];
  213. });
  214. nameInput.placeholder = createNamePlaceholder(options);
  215. }
  216. const mainForm = window.document.getElementById('mainForm');
  217. new MainForm(mainForm);
  218. </script>
  219. </body>
  220. </html>