formatDistance.mjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const formatDistanceLocale = {
  2. lessThanXSeconds: {
  3. one: "1초 미만",
  4. other: "{{count}}초 미만",
  5. },
  6. xSeconds: {
  7. one: "1초",
  8. other: "{{count}}초",
  9. },
  10. halfAMinute: "30초",
  11. lessThanXMinutes: {
  12. one: "1분 미만",
  13. other: "{{count}}분 미만",
  14. },
  15. xMinutes: {
  16. one: "1분",
  17. other: "{{count}}분",
  18. },
  19. aboutXHours: {
  20. one: "약 1시간",
  21. other: "약 {{count}}시간",
  22. },
  23. xHours: {
  24. one: "1시간",
  25. other: "{{count}}시간",
  26. },
  27. xDays: {
  28. one: "1일",
  29. other: "{{count}}일",
  30. },
  31. aboutXWeeks: {
  32. one: "약 1주",
  33. other: "약 {{count}}주",
  34. },
  35. xWeeks: {
  36. one: "1주",
  37. other: "{{count}}주",
  38. },
  39. aboutXMonths: {
  40. one: "약 1개월",
  41. other: "약 {{count}}개월",
  42. },
  43. xMonths: {
  44. one: "1개월",
  45. other: "{{count}}개월",
  46. },
  47. aboutXYears: {
  48. one: "약 1년",
  49. other: "약 {{count}}년",
  50. },
  51. xYears: {
  52. one: "1년",
  53. other: "{{count}}년",
  54. },
  55. overXYears: {
  56. one: "1년 이상",
  57. other: "{{count}}년 이상",
  58. },
  59. almostXYears: {
  60. one: "거의 1년",
  61. other: "거의 {{count}}년",
  62. },
  63. };
  64. export const formatDistance = (token, count, options) => {
  65. let result;
  66. const tokenValue = formatDistanceLocale[token];
  67. if (typeof tokenValue === "string") {
  68. result = tokenValue;
  69. } else if (count === 1) {
  70. result = tokenValue.one;
  71. } else {
  72. result = tokenValue.other.replace("{{count}}", count.toString());
  73. }
  74. if (options?.addSuffix) {
  75. if (options.comparison && options.comparison > 0) {
  76. return result + " 후";
  77. } else {
  78. return result + " 전";
  79. }
  80. }
  81. return result;
  82. };