buildMatchFn.d.cts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import type { Quarter, Era, Day, Month } from "../../types.js";
  2. import type {
  3. LocaleUnitValue,
  4. LocaleWidth,
  5. LocaleDayPeriod,
  6. MatchFn,
  7. MatchValueCallback,
  8. } from "../types.js";
  9. export interface BuildMatchFnArgs<
  10. Result extends LocaleUnitValue,
  11. DefaultMatchWidth extends LocaleWidth,
  12. DefaultParseWidth extends LocaleWidth,
  13. > {
  14. matchPatterns: BuildMatchFnMatchPatterns<DefaultMatchWidth>;
  15. defaultMatchWidth: DefaultMatchWidth;
  16. parsePatterns: BuildMatchFnParsePatterns<Result, DefaultParseWidth>;
  17. defaultParseWidth: DefaultParseWidth;
  18. valueCallback?: MatchValueCallback<
  19. Result extends LocaleDayPeriod ? string : number,
  20. Result
  21. >;
  22. }
  23. export type BuildMatchFnMatchPatterns<DefaultWidth extends LocaleWidth> = {
  24. [Width in LocaleWidth]?: RegExp;
  25. } & {
  26. [Width in DefaultWidth]: RegExp;
  27. };
  28. export type BuildMatchFnParsePatterns<
  29. Value extends LocaleUnitValue,
  30. DefaultWidth extends LocaleWidth,
  31. > = {
  32. [Width in LocaleWidth]?: ParsePattern<Value>;
  33. } & {
  34. [Width in DefaultWidth]: ParsePattern<Value>;
  35. };
  36. export type ParsePattern<Value extends LocaleUnitValue> =
  37. Value extends LocaleDayPeriod
  38. ? Record<LocaleDayPeriod, RegExp>
  39. : Value extends Quarter
  40. ? readonly [RegExp, RegExp, RegExp, RegExp]
  41. : Value extends Era
  42. ? readonly [RegExp, RegExp]
  43. : Value extends Day
  44. ? readonly [RegExp, RegExp, RegExp, RegExp, RegExp, RegExp, RegExp]
  45. : Value extends Month
  46. ? readonly [
  47. RegExp,
  48. RegExp,
  49. RegExp,
  50. RegExp,
  51. RegExp,
  52. RegExp,
  53. RegExp,
  54. RegExp,
  55. RegExp,
  56. RegExp,
  57. RegExp,
  58. RegExp,
  59. ]
  60. : never;
  61. export declare function buildMatchFn<
  62. Value extends LocaleUnitValue,
  63. DefaultMatchWidth extends LocaleWidth,
  64. DefaultParseWidth extends LocaleWidth,
  65. >(
  66. args: BuildMatchFnArgs<Value, DefaultMatchWidth, DefaultParseWidth>,
  67. ): MatchFn<Value>;