File size: 1,507 Bytes
1244914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import tmp from "tmp";
import { parse as parseCsv } from "csv-parse";

/**
 * Formats a date with local timezone information
 */
export function formatTimestamp(date: Date): string {
  const offset = -date.getTimezoneOffset();
  const sign = offset >= 0 ? "+" : "-";
  const hours = Math.floor(Math.abs(offset) / 60)
    .toString()
    .padStart(2, "0");
  const minutes = (Math.abs(offset) % 60).toString().padStart(2, "0");
  const timezone = `${sign}${hours}:${minutes}`;

  return `${date.toISOString().replace("Z", "")}${timezone}`;
}

/**
 * Escapes special regex characters in a string
 */
export function escapeRegex(str: string): string {
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

/**
 * Creates a temporary directory asynchronously
 */
export async function createTempDir(
  prefix: string,
): Promise<{ name: string; removeCallback: () => void }> {
  return new Promise((resolve, reject) => {
    tmp.dir({ prefix }, (err, path, cleanupCallback) => {
      if (err) reject(err);
      else resolve({ name: path, removeCallback: cleanupCallback });
    });
  });
}

/**
 * Parses CSV content asynchronously
 */
export async function parseCsvAsync(
  content: string,
  options: {
    columns: boolean;
    skip_empty_lines: boolean;
  },
): Promise<Record<string, string>[]> {
  return new Promise((resolve, reject) => {
    parseCsv(content, options, (err, records: unknown) => {
      if (err) reject(err);
      else resolve(records as Record<string, string>[]);
    });
  });
}