buildLocalizeFn.cjs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. exports.buildLocalizeFn = buildLocalizeFn;
  3. /**
  4. * The localize function argument callback which allows to convert raw value to
  5. * the actual type.
  6. *
  7. * @param value - The value to convert
  8. *
  9. * @returns The converted value
  10. */
  11. /**
  12. * The map of localized values for each width.
  13. */
  14. /**
  15. * The index type of the locale unit value. It types conversion of units of
  16. * values that don't start at 0 (i.e. quarters).
  17. */
  18. /**
  19. * Converts the unit value to the tuple of values.
  20. */
  21. /**
  22. * The tuple of localized era values. The first element represents BC,
  23. * the second element represents AD.
  24. */
  25. /**
  26. * The tuple of localized quarter values. The first element represents Q1.
  27. */
  28. /**
  29. * The tuple of localized day values. The first element represents Sunday.
  30. */
  31. /**
  32. * The tuple of localized month values. The first element represents January.
  33. */
  34. function buildLocalizeFn(args) {
  35. return (value, options) => {
  36. const context = options?.context ? String(options.context) : "standalone";
  37. let valuesArray;
  38. if (context === "formatting" && args.formattingValues) {
  39. const defaultWidth = args.defaultFormattingWidth || args.defaultWidth;
  40. const width = options?.width ? String(options.width) : defaultWidth;
  41. valuesArray =
  42. args.formattingValues[width] || args.formattingValues[defaultWidth];
  43. } else {
  44. const defaultWidth = args.defaultWidth;
  45. const width = options?.width ? String(options.width) : args.defaultWidth;
  46. valuesArray = args.values[width] || args.values[defaultWidth];
  47. }
  48. const index = args.argumentCallback ? args.argumentCallback(value) : value;
  49. // @ts-expect-error - For some reason TypeScript just don't want to match it, no matter how hard we try. I challenge you to try to remove it!
  50. return valuesArray[index];
  51. };
  52. }