File size: 1,855 Bytes
6e41657
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
import dayjs from 'dayjs';
import { ITEM_SHOW_TYPE } from '~/config';

export function sleep(ms: number = 1000): Promise<void> {
  return new Promise(resolve => setTimeout(resolve, ms));
}

export function timeout(ms: number = 1000): Promise<never> {
  return new Promise((_, reject) => {
    setTimeout(() => reject(new Error('operation timeout')), ms);
  });
}

export function throwException(message: string) {
  throw new Error(message);
}

export function maxLen(text: string, max = 35): string {
  if (text.length > max) {
    return text.slice(0, max) + '...';
  }
  return text;
}

// 过滤文件名中的非法字符
export function filterInvalidFilenameChars(input: string): string {
  // 只保留中文字符、英文字符、数字
  const regex = /[^\u4e00-\u9fa5a-zA-Z0-9()()]/g;
  return input.replace(regex, '_').slice(0, 100).trim();
}

// 格式化消耗时间
export function formatElapsedTime(seconds: number): string {
  const hours = Math.floor(seconds / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  const secs = seconds % 60;

  let result = '';
  if (hours > 0) {
    result += `${hours}小时`;
  }
  if (minutes > 0) {
    result += `${minutes}分`;
  }
  if (secs > 0 || result === '') {
    result += `${secs}秒`;
  }
  return result;
}

// 将时长字符串转为秒数
export function durationToSeconds(duration: string | undefined) {
  if (!duration) return 0;
  const [min, sec] = duration.split(':').map(Number);
  return min * 60 + sec;
}

// 时间戳(单位秒)转日期字符串
export function formatTimeStamp(timestamp: number) {
  return dayjs.unix(timestamp).format('YYYY-MM-DD HH:mm:ss');
}

// 格式化文章显示类型
export function formatItemShowType(type: number) {
  return ITEM_SHOW_TYPE[type] || '未识别';
}