string_utils.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. export const readableDate = (s: string) => {
  2. const ts = new Date(s);
  3. const date = ts.toLocaleDateString();
  4. const time = ts.toLocaleTimeString([], {
  5. hour: "numeric",
  6. minute: "2-digit",
  7. });
  8. return `${time} on ${date}`;
  9. };
  10. export const relativeDate = (date: string | number, future: boolean) => {
  11. if (!date) {
  12. return "N/A";
  13. }
  14. // @ts-ignore
  15. const rtf = new Intl.RelativeTimeFormat("en", {
  16. localeMatcher: "best fit", // other values: "lookup"
  17. numeric: "auto", // other values: "auto"
  18. style: "long", // other values: "short" or "narrow"
  19. });
  20. const time = timeFrom(date);
  21. if (!time) {
  22. return "N/A";
  23. }
  24. let format;
  25. if (future) {
  26. format = rtf.format(time.time, time.unitOfTime);
  27. } else {
  28. format = rtf.format(-time.time, time.unitOfTime);
  29. }
  30. return format;
  31. };
  32. export const feedDate = (timestamp: string) => {
  33. const localTime = new Date(timestamp).toLocaleString("en-US", {
  34. month: "short",
  35. day: "numeric",
  36. hour: "numeric",
  37. minute: "2-digit",
  38. hour12: true,
  39. });
  40. return localTime;
  41. };
  42. export const timeFrom = (
  43. time: string | number,
  44. secondTime?: string | number
  45. ) => {
  46. // Get timestamps
  47. let unixTime = new Date(time).getTime();
  48. if (!unixTime) return;
  49. let now = new Date().getTime();
  50. if (secondTime) {
  51. now = new Date(secondTime).getTime();
  52. }
  53. // Calculate difference
  54. let difference = unixTime / 1000 - now / 1000;
  55. // Setup return object
  56. let tfn: any = {};
  57. // Check if time is in the past, present, or future
  58. tfn.when = "now";
  59. if (difference > 0) {
  60. tfn.when = "future";
  61. } else if (difference < -1) {
  62. tfn.when = "past";
  63. }
  64. // Convert difference to absolute
  65. difference = Math.abs(difference);
  66. // Calculate time unit
  67. if (difference / (60 * 60 * 24 * 365) > 1) {
  68. // Years
  69. tfn.unitOfTime = "years";
  70. tfn.time = Math.floor(difference / (60 * 60 * 24 * 365));
  71. } else if (difference / (60 * 60 * 24 * 45) > 1) {
  72. // Months
  73. tfn.unitOfTime = "months";
  74. tfn.time = Math.floor(difference / (60 * 60 * 24 * 45));
  75. } else if (difference / (60 * 60 * 24) > 1) {
  76. // Days
  77. tfn.unitOfTime = "days";
  78. tfn.time = Math.floor(difference / (60 * 60 * 24));
  79. } else if (difference / (60 * 60) > 1) {
  80. // Hours
  81. tfn.unitOfTime = "hours";
  82. tfn.time = Math.floor(difference / (60 * 60));
  83. } else if (difference / 60 > 1) {
  84. // Minutes
  85. tfn.unitOfTime = "minutes";
  86. tfn.time = Math.floor(difference / 60);
  87. } else {
  88. // Seconds
  89. tfn.unitOfTime = "seconds";
  90. tfn.time = Math.floor(difference);
  91. }
  92. // Return time from now data
  93. return tfn;
  94. };
  95. export const capitalize = (s: string) => {
  96. if (!s) {
  97. return "";
  98. } else if (s.length == 0) {
  99. return s;
  100. } else if (s.length == 1) {
  101. return s.charAt(0).toUpperCase();
  102. }
  103. return s.charAt(0).toUpperCase() + s.substring(1).toLowerCase();
  104. };
  105. const LINE =
  106. /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/gm;
  107. export const dotenv_parse = (src: string): Record<string, string> => {
  108. // Parser src into an Object
  109. const obj = {} as Record<string, string>;
  110. // Convert buffer to string
  111. let lines = src.toString();
  112. // Convert line breaks to same format
  113. lines = lines.replace(/\r\n?/gm, "\n");
  114. let match;
  115. while ((match = LINE.exec(lines)) != null) {
  116. const key = match[1];
  117. // Default undefined or null to empty string
  118. let value = match[2] || "";
  119. // Remove whitespace
  120. value = value.trim();
  121. // Check if double quoted
  122. const maybeQuote = value[0];
  123. // Remove surrounding quotes
  124. value = value.replace(/^(['"`])([\s\S]*)\1$/gm, "$2");
  125. // Expand newlines if double quoted
  126. if (maybeQuote === '"') {
  127. value = value.replace(/\\n/g, "\n");
  128. value = value.replace(/\\r/g, "\r");
  129. }
  130. // Add to object
  131. obj[key] = value;
  132. }
  133. return obj;
  134. };