Gets the name of a number, even if it's stupidly big. Supersedes TheoryOfNekomata/number-name.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

99 Zeilen
2.0 KiB

  1. /**
  2. * Group digits.
  3. */
  4. type GroupDigits = string;
  5. /**
  6. * Group place.
  7. */
  8. export type GroupPlace = bigint;
  9. /**
  10. * Group of digits and its place.
  11. *
  12. * The place refers to the order which the digits are grouped, e.g. for a number like
  13. *
  14. * 1,234,567
  15. *
  16. * The groups would be:
  17. *
  18. * ['001', 2]
  19. * ['234', 1]
  20. * ['567', 0]
  21. *
  22. * Note that groups do not necessarily have the same length of digits, such in the case of
  23. * South Asian numbering system:
  24. *
  25. * 1,00,00,000
  26. *
  27. * The groups would be:
  28. *
  29. * ['01', 3]
  30. * ['00', 2]
  31. * ['00', 1]
  32. * ['000', 0]
  33. */
  34. export type Group = [GroupDigits, GroupPlace];
  35. /**
  36. * Index of the group digits in a {@link Group|group}.
  37. */
  38. export const GROUP_DIGITS_INDEX = 0 as const;
  39. /**
  40. * Index of the group place in a {@link Group|group}.
  41. */
  42. export const GROUP_PLACE_INDEX = 1 as const;
  43. /**
  44. * System for stringifying and parsing numbers.
  45. */
  46. export interface StringifySystem {
  47. /**
  48. * Creates a negative string.
  49. * @param s - The string to make negative.
  50. */
  51. makeNegative: (s: string) => string;
  52. /**
  53. * Creates a group string.
  54. * @param group - The group digits.
  55. * @param place - The group place.
  56. * @param options - Options to use when creating the group.
  57. */
  58. makeGroups: <T extends object>(groups: Group[], options?: T) => string[];
  59. /**
  60. * Groups a string.
  61. * @param value - The string to group.
  62. */
  63. group: (value: string) => Group[];
  64. /**
  65. * Finalizes a string.
  66. * @param tokens - The tokens to finalize.
  67. */
  68. finalize: (tokens: string[]) => string;
  69. /**
  70. * Tokenizes a string.
  71. * @param value - The string to tokenize.
  72. */
  73. tokenize: (value: string) => string[];
  74. /**
  75. * Parses groups from a string.
  76. * @param value - The string to parse groups from.
  77. */
  78. parseGroups: (value: string[]) => Group[];
  79. /**
  80. * Combines groups into a string.
  81. * @param value - The groups to combine.
  82. */
  83. combineGroups: (value: Group[]) => string;
  84. }
  85. /**
  86. * Error thrown when an invalid token is encountered.
  87. */
  88. export class InvalidTokenError extends Error {
  89. constructor(token: string) {
  90. super(`Invalid token: ${token}`);
  91. }
  92. }