string_utils.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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) => {
  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. return rtf.format(-time.time, time.unitOfTime);
  25. };
  26. export const timeFrom = (
  27. time: string | number,
  28. secondTime?: string | number
  29. ) => {
  30. // Get timestamps
  31. let unixTime = new Date(time).getTime();
  32. if (!unixTime) return;
  33. let now = new Date().getTime();
  34. if (secondTime) {
  35. now = new Date(secondTime).getTime();
  36. }
  37. // Calculate difference
  38. let difference = unixTime / 1000 - now / 1000;
  39. // Setup return object
  40. let tfn: any = {};
  41. // Check if time is in the past, present, or future
  42. tfn.when = "now";
  43. if (difference > 0) {
  44. tfn.when = "future";
  45. } else if (difference < -1) {
  46. tfn.when = "past";
  47. }
  48. // Convert difference to absolute
  49. difference = Math.abs(difference);
  50. // Calculate time unit
  51. if (difference / (60 * 60 * 24 * 365) > 1) {
  52. // Years
  53. tfn.unitOfTime = "years";
  54. tfn.time = Math.floor(difference / (60 * 60 * 24 * 365));
  55. } else if (difference / (60 * 60 * 24 * 45) > 1) {
  56. // Months
  57. tfn.unitOfTime = "months";
  58. tfn.time = Math.floor(difference / (60 * 60 * 24 * 45));
  59. } else if (difference / (60 * 60 * 24) > 1) {
  60. // Days
  61. tfn.unitOfTime = "days";
  62. tfn.time = Math.floor(difference / (60 * 60 * 24));
  63. } else if (difference / (60 * 60) > 1) {
  64. // Hours
  65. tfn.unitOfTime = "hours";
  66. tfn.time = Math.floor(difference / (60 * 60));
  67. } else if (difference / 60 > 1) {
  68. // Minutes
  69. tfn.unitOfTime = "minutes";
  70. tfn.time = Math.floor(difference / 60);
  71. } else {
  72. // Seconds
  73. tfn.unitOfTime = "seconds";
  74. tfn.time = Math.floor(difference);
  75. }
  76. // Return time from now data
  77. return tfn;
  78. };
  79. export const capitalize = (s: string) => {
  80. if (!s) {
  81. return "";
  82. } else if (s.length == 0) {
  83. return s;
  84. } else if (s.length == 1) {
  85. return s.charAt(0).toUpperCase();
  86. }
  87. return s.charAt(0).toUpperCase() + s.substring(1).toLowerCase();
  88. };
  89. const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/gm;
  90. export const dotenv_parse = (src: string): Record<string, string> => {
  91. // Parser src into an Object
  92. const obj = {} as Record<string, string>;
  93. // Convert buffer to string
  94. let lines = src.toString();
  95. // Convert line breaks to same format
  96. lines = lines.replace(/\r\n?/gm, "\n");
  97. let match;
  98. while ((match = LINE.exec(lines)) != null) {
  99. const key = match[1];
  100. // Default undefined or null to empty string
  101. let value = match[2] || "";
  102. // Remove whitespace
  103. value = value.trim();
  104. // Check if double quoted
  105. const maybeQuote = value[0];
  106. // Remove surrounding quotes
  107. value = value.replace(/^(['"`])([\s\S]*)\1$/gm, "$2");
  108. // Expand newlines if double quoted
  109. if (maybeQuote === '"') {
  110. value = value.replace(/\\n/g, "\n");
  111. value = value.replace(/\\r/g, "\r");
  112. }
  113. // Add to object
  114. obj[key] = value;
  115. }
  116. return obj;
  117. };