buildLocalizeFn.d.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import type { Day, Era, Month, Quarter } from "../../types.js";
  2. import type {
  3. LocaleDayPeriod,
  4. LocaleUnitValue,
  5. LocaleWidth,
  6. LocalizeFn,
  7. } from "../types.js";
  8. export type BuildLocalizeFnArgs<
  9. Value extends LocaleUnitValue,
  10. ArgCallback extends LocalizeFnArgCallback<Value> | undefined,
  11. > = {
  12. values: LocalizePeriodValuesMap<Value>;
  13. defaultWidth: LocaleWidth;
  14. formattingValues?: LocalizePeriodValuesMap<Value>;
  15. defaultFormattingWidth?: LocaleWidth;
  16. } & (ArgCallback extends undefined
  17. ? {
  18. argumentCallback?: undefined;
  19. }
  20. : {
  21. argumentCallback: LocalizeFnArgCallback<Value>;
  22. });
  23. /**
  24. * The localize function argument callback which allows to convert raw value to
  25. * the actual type.
  26. *
  27. * @param value - The value to convert
  28. *
  29. * @returns The converted value
  30. */
  31. export type LocalizeFnArgCallback<Value extends LocaleUnitValue | number> = (
  32. value: Value,
  33. ) => LocalizeUnitIndex<Value>;
  34. /**
  35. * The map of localized values for each width.
  36. */
  37. export type LocalizePeriodValuesMap<Value extends LocaleUnitValue> = {
  38. [Pattern in LocaleWidth]?: LocalizeValues<Value>;
  39. };
  40. /**
  41. * The index type of the locale unit value. It types conversion of units of
  42. * values that don't start at 0 (i.e. quarters).
  43. */
  44. export type LocalizeUnitIndex<Value extends LocaleUnitValue | number> =
  45. Value extends LocaleUnitValue ? keyof LocalizeValues<Value> : number;
  46. /**
  47. * Converts the unit value to the tuple of values.
  48. */
  49. export type LocalizeValues<Value extends LocaleUnitValue> =
  50. Value extends LocaleDayPeriod
  51. ? Record<LocaleDayPeriod, string>
  52. : Value extends Era
  53. ? LocalizeEraValues
  54. : Value extends Quarter
  55. ? LocalizeQuarterValues
  56. : Value extends Day
  57. ? LocalizeDayValues
  58. : Value extends Month
  59. ? LocalizeMonthValues
  60. : never;
  61. /**
  62. * The tuple of localized era values. The first element represents BC,
  63. * the second element represents AD.
  64. */
  65. export type LocalizeEraValues = readonly [string, string];
  66. /**
  67. * The tuple of localized quarter values. The first element represents Q1.
  68. */
  69. export type LocalizeQuarterValues = readonly [string, string, string, string];
  70. /**
  71. * The tuple of localized day values. The first element represents Sunday.
  72. */
  73. export type LocalizeDayValues = readonly [
  74. string,
  75. string,
  76. string,
  77. string,
  78. string,
  79. string,
  80. string,
  81. ];
  82. /**
  83. * The tuple of localized month values. The first element represents January.
  84. */
  85. export type LocalizeMonthValues = readonly [
  86. string,
  87. string,
  88. string,
  89. string,
  90. string,
  91. string,
  92. string,
  93. string,
  94. string,
  95. string,
  96. string,
  97. string,
  98. ];
  99. export declare function buildLocalizeFn<
  100. Value extends LocaleUnitValue,
  101. ArgCallback extends LocalizeFnArgCallback<Value> | undefined,
  102. >(args: BuildLocalizeFnArgs<Value, ArgCallback>): LocalizeFn<Value>;