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.
 
 

202 lines
5.3 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. .checkbox > input {
  51. position: absolute;
  52. left: -999999px;
  53. }
  54. .checkbox > input + span {
  55. display: inline-flex;
  56. align-items: center;
  57. justify-content: center;
  58. height: 3rem;
  59. padding: 0 1rem;
  60. border: 1px solid #ccc;
  61. border-radius: 0.25rem;
  62. cursor: pointer;
  63. }
  64. .checkbox > input:checked + span {
  65. border-color: #000;
  66. background-color: #000;
  67. color: #fff;
  68. }
  69. @media (min-width: 1080px) {
  70. .main-form > div {
  71. flex-direction: row;
  72. }
  73. .main-form > div > textarea {
  74. width: 0;
  75. height: 100%;
  76. }
  77. }
  78. </style>
  79. </head>
  80. <body>
  81. <form id="mainForm" class="main-form">
  82. <fieldset>
  83. <legend>
  84. Options
  85. </legend>
  86. <div>
  87. <label class="checkbox">
  88. <input type="checkbox" name="shortenMillia">
  89. <span>Shorten millia</span>
  90. </label><label class="checkbox">
  91. <input type="checkbox" name="addTensDashes" checked>
  92. <span>Add dashes to tens</span>
  93. </label><label class="checkbox">
  94. <input type="checkbox" name="oneGroupPerLine" checked>
  95. <span>One group per line</span>
  96. </label><!--<label class="checkbox">
  97. <input type="groupDigits" name="groupDigits" checked>
  98. <span>Group digits</span>
  99. </label>-->
  100. </div>
  101. </fieldset>
  102. <div>
  103. <textarea aria-label="Number" name="number" placeholder="1234"></textarea>
  104. <textarea aria-label="Name" name="name" placeholder="one thousand&#x0A;two hundred thirty-four"></textarea>
  105. </div>
  106. </form>
  107. <script type="module">
  108. import { stringify, parse, systems } from './numerica.js';
  109. function MainForm(el) {
  110. const numberInput = el.querySelector('[name="number"]');
  111. const nameInput = el.querySelector('[name="name"]');
  112. const addTensDashesCheckbox = el.querySelector('[name="addTensDashes"]');
  113. const shortenMilliaCheckbox = el.querySelector('[name="shortenMillia"]');
  114. const oneGroupPerLineCheckbox = el.querySelector('[name="oneGroupPerLine"]');
  115. const options = {
  116. stringify: {
  117. system: systems.enUS.shortCount,
  118. makeGroupOptions: {},
  119. finalizeOptions: {},
  120. },
  121. parse: {
  122. system: systems.enUS.shortCount,
  123. type: 'bigint',
  124. },
  125. };
  126. const createNamePlaceholder = (theOptions) => {
  127. return stringify(1234, theOptions.stringify);
  128. };
  129. numberInput.addEventListener('input', (e) => {
  130. e.currentTarget.setCustomValidity('');
  131. nameInput.value = '';
  132. if (e.currentTarget.value.trim() === '') {
  133. return;
  134. }
  135. try {
  136. nameInput.value = stringify(e.currentTarget.value, options.stringify);
  137. } catch (err) {
  138. console.error(err);
  139. e.currentTarget.setCustomValidity('Invalid number.');
  140. }
  141. });
  142. nameInput.addEventListener('input', (e) => {
  143. e.currentTarget.setCustomValidity('');
  144. numberInput.value = '';
  145. if (e.currentTarget.value.trim() === '') {
  146. return;
  147. }
  148. try {
  149. numberInput.value = parse(e.currentTarget.value, options.parse)
  150. .toString();
  151. // TODO group digits function from system.
  152. } catch (err) {
  153. console.error(err);
  154. e.currentTarget.setCustomValidity('Invalid name.');
  155. }
  156. });
  157. addTensDashesCheckbox.addEventListener('change', (e) => {
  158. options.stringify.makeGroupOptions.addTensDashes = e.currentTarget.checked;
  159. numberInput.dispatchEvent(new Event('input'));
  160. nameInput.placeholder = createNamePlaceholder(options);
  161. });
  162. shortenMilliaCheckbox.addEventListener('change', (e) => {
  163. options.stringify.makeGroupOptions.shortenMillia = e.currentTarget.checked;
  164. numberInput.dispatchEvent(new Event('input'));
  165. nameInput.placeholder = createNamePlaceholder(options);
  166. });
  167. oneGroupPerLineCheckbox.addEventListener('change', (e) => {
  168. options.stringify.finalizeOptions.oneGroupPerLine = e.currentTarget.checked;
  169. numberInput.dispatchEvent(new Event('input'));
  170. nameInput.placeholder = createNamePlaceholder(options);
  171. });
  172. options.stringify.makeGroupOptions.addTensDashes = addTensDashesCheckbox.checked;
  173. options.stringify.makeGroupOptions.shortenMillia = shortenMilliaCheckbox.checked;
  174. options.stringify.finalizeOptions.oneGroupPerLine = oneGroupPerLineCheckbox.checked;
  175. nameInput.placeholder = createNamePlaceholder(options);
  176. }
  177. const mainForm = window.document.getElementById('mainForm');
  178. new MainForm(mainForm);
  179. </script>
  180. </body>
  181. </html>