aboutsummaryrefslogtreecommitdiff
path: root/deno/base/lib.ts
blob: 3c69e0aef590f946fa7f2616b07c9c352825cded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function camelCaseToKebabCase(str: string): string {
  return str.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase());
}

function prependNonEmpty<T>(
  object: T,
  prefix: string = " ",
): string {
  if (object == null) return "";
  const string = typeof object === "string" ? object : String(object);
  return string.length === 0 ? "" : prefix + string;
}

export const StringUtils = Object.freeze({
  camelCaseToKebabCase,
  prependNonEmpty,
});

function toFileNameString(date: Date, dateOnly?: boolean): string {
  const str = date.toISOString();
  return dateOnly === true
    ? str.slice(0, str.indexOf("T"))
    : str.replaceAll(/:|\./g, "-");
}

export const DateUtils = Object.freeze(
  {
    toFileNameString,
  } as const,
);