File size: 2,281 Bytes
d02c2a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
export const objectCopy = <T>(obj: T): T => {
  return JSON.parse(JSON.stringify(obj)) as T;
};

export const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

export const imgExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.bmp'];
export const videoExtensions = ['.mp4', '.avi', '.mov', '.mkv', '.wmv', '.m4v', '.flv'];
export const audioExtensions = ['.mp3', '.wav', '.flac', '.ogg'];

export const isVideo = (filePath: string) => videoExtensions.includes(filePath.toLowerCase().slice(-4));
export const isImage = (filePath: string) => imgExtensions.includes(filePath.toLowerCase().slice(-4));
export const isAudio = (filePath: string) => audioExtensions.includes(filePath.toLowerCase().slice(-4));

export const tagsToObj = (tagStr: string): Record<string, any> => {
  const result: Record<string, any> = {};
  const regex = /<([A-Z_][A-Z0-9_]*)>([\s\S]*?)<\/\1>/g;
  let match;
  while ((match = regex.exec(tagStr)) !== null) {
    const value = match[2].trim();
    try {
      result[match[1]] = JSON.parse(value);
    } catch {
      result[match[1]] = value;
    }
  }
  return result;
};

export const objToTags = (obj: Record<string, any>): string => {
  return Object.entries(obj)
    .map(([key, value]) => {
      const content = typeof value === 'string' ? value : JSON.stringify(value);
      return `<${key}>${content}</${key}>`;
    })
    .join('\n');
};

export const getFilename = (filePath: string) => {
  const idx = Math.max(filePath.lastIndexOf('/'), filePath.lastIndexOf('\\'));
  return idx === -1 ? filePath : filePath.slice(idx + 1);
};

export const getFoldername = (filePath: string) => {
  const idx = Math.max(filePath.lastIndexOf('/'), filePath.lastIndexOf('\\'));
  return idx === -1 ? '' : filePath.slice(0, idx);
};

export const pathJoin = (...parts: string[]) => {
  const sep = parts.length > 0 && parts[0].includes('\\') ? '\\' : '/';
  const leadingTrailing = sep === '\\' ? /^\\+|\\+$/g : /^\/+|\/+$/g;
  const trailing = sep === '\\' ? /\\+$/ : /\/+$/;
  return parts
    .map((part, index) => {
      if (index === 0) {
        return part.replace(trailing, '');
      } else {
        return part.replace(leadingTrailing, '');
      }
    })
    .filter(part => part.length > 0)
    .join(sep);
}