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.
 
 

204 lines
5.1 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. shortenMillia: false,
  120. addTensDashes: true,
  121. },
  122. finalizeOptions: {
  123. oneGroupPerLine: true,
  124. },
  125. },
  126. parse: {
  127. system: systems.enUS.shortCount,
  128. type: 'bigint',
  129. },
  130. };
  131. const createNamePlaceholder = (theOptions) => {
  132. return stringify(1234, theOptions.stringify);
  133. };
  134. numberInput.addEventListener('input', (e) => {
  135. e.currentTarget.setCustomValidity('');
  136. nameInput.value = '';
  137. if (e.currentTarget.value.trim() === '') {
  138. return;
  139. }
  140. try {
  141. nameInput.value = stringify(e.currentTarget.value, options.stringify);
  142. } catch (err) {
  143. console.error(err);
  144. e.currentTarget.setCustomValidity('Invalid number.');
  145. }
  146. });
  147. nameInput.addEventListener('input', (e) => {
  148. e.currentTarget.setCustomValidity('');
  149. numberInput.value = '';
  150. if (e.currentTarget.value.trim() === '') {
  151. return;
  152. }
  153. try {
  154. numberInput.value = parse(e.currentTarget.value, options.parse)
  155. .toString();
  156. // TODO group digits function from system.
  157. } catch (err) {
  158. console.error(err);
  159. e.currentTarget.setCustomValidity('Invalid name.');
  160. }
  161. });
  162. addTensDashesCheckbox.addEventListener('change', (e) => {
  163. options.stringify.makeGroupOptions.addTensDashes = e.currentTarget.checked;
  164. numberInput.dispatchEvent(new Event('input'));
  165. nameInput.placeholder = createNamePlaceholder(options);
  166. });
  167. shortenMilliaCheckbox.addEventListener('change', (e) => {
  168. options.stringify.makeGroupOptions.shortenMillia = e.currentTarget.checked;
  169. numberInput.dispatchEvent(new Event('input'));
  170. nameInput.placeholder = createNamePlaceholder(options);
  171. });
  172. oneGroupPerLineCheckbox.addEventListener('change', (e) => {
  173. options.stringify.finalizeOptions.oneGroupPerLine = e.currentTarget.checked;
  174. numberInput.dispatchEvent(new Event('input'));
  175. nameInput.placeholder = createNamePlaceholder(options);
  176. });
  177. nameInput.placeholder = createNamePlaceholder(options);
  178. }
  179. const mainForm = window.document.getElementById('mainForm');
  180. new MainForm(mainForm);
  181. </script>
  182. </body>
  183. </html>