Gets the name of a number, even if it's stupidly big. Supersedes TheoryOfNekomata/number-name.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

78 líneas
2.1 KiB

  1. import {BLANK_DIGIT} from '../../../utils/numeric';
  2. const config = {
  3. onesNames: ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'],
  4. onesOrdinalNames: ['zeroth', 'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth'],
  5. teensNames: [
  6. 'ten',
  7. 'eleven',
  8. 'twelve',
  9. 'thirteen',
  10. 'fourteen',
  11. 'fifteen',
  12. 'sixteen',
  13. 'seventeen',
  14. 'eighteen',
  15. 'nineteen',
  16. ],
  17. teensOrdinalNames: [
  18. 'tenth',
  19. 'eleventh',
  20. 'twelfth',
  21. 'thirteenth',
  22. 'fourteenth',
  23. 'fifteenth',
  24. 'sixteenth',
  25. 'seventeenth',
  26. 'eighteenth',
  27. 'nineteenth',
  28. ],
  29. tensNames: ['zero', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'],
  30. tensOrdinalNames: [
  31. 'zeroth',
  32. 'tenth',
  33. 'twentieth',
  34. 'thirtieth',
  35. 'fortieth',
  36. 'fiftieth',
  37. 'sixtieth',
  38. 'seventieth',
  39. 'eightieth',
  40. 'ninetieth',
  41. ],
  42. hundredName: 'hundred',
  43. hundredOrdinalName: 'hundredth',
  44. grouping: 3,
  45. };
  46. export const getGroupDigitsName = (digitsRaw: string, ordinal: boolean) => {
  47. const digits = digitsRaw.padStart(config.grouping, BLANK_DIGIT);
  48. const [hundreds, tens, ones] = digits.split('').map(s => Number(s));
  49. const names = [];
  50. if (hundreds !== 0) {
  51. names.push(config.onesNames[hundreds]);
  52. if (!ordinal || (tens > 0 || ones > 0)) {
  53. names.push(config.hundredName);
  54. }
  55. }
  56. if (tens === 1) {
  57. names.push(ordinal ? config.teensOrdinalNames[ones] : config.teensNames[ones]);
  58. } else if (tens > 1) {
  59. if (ones > 0) {
  60. names.push(config.tensNames[tens]);
  61. names.push(ordinal ? config.onesOrdinalNames[ones] : config.onesNames[ones]);
  62. } else {
  63. names.push(ordinal ? config.tensOrdinalNames[tens] : config.tensNames[tens]);
  64. }
  65. } else {
  66. if (hundreds === 0) {
  67. names.push(ordinal ? config.onesOrdinalNames[ones] : config.onesNames[ones]);
  68. } else if (ones > 0) {
  69. names.push(ordinal ? config.onesOrdinalNames[ones] : config.onesNames[ones]);
  70. } else if (ordinal) {
  71. names.push(config.hundredOrdinalName);
  72. }
  73. }
  74. return names.join(' ');
  75. };