index.ts
6.28 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { createVNode, VNode } from 'vue';
import { Form, FormItem, Modal } from '@arco-design/web-vue';
import { AnyObject } from '@/types/global';
import { RenderContent } from '@arco-design/web-vue/es/_utils/types';
import { ModalConfig } from '@arco-design/web-vue/es/modal/interface';
import { compact, get } from 'lodash';
type TargetContext = '_self' | '_parent' | '_blank' | '_top';
export const openWindow = (url: string, opts?: { target?: TargetContext; [key: string]: any }) => {
const { target = '_blank', ...others } = opts || {};
window.open(
url,
target,
Object.entries(others)
.reduce((preValue: string[], curValue) => {
const [key, value] = curValue;
return [...preValue, `${key}=${value}`];
}, [])
.join(',')
);
};
export const regexUrl = new RegExp(
'^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
'i'
);
export const createFormVNode = (props: AnyObject, children: VNode | VNode[]) =>
createVNode(Form, { autoLabelWidth: true, ...props }, () => children);
export const createFormItemVNode = (props: AnyObject, children: VNode | VNode[]) =>
createVNode(FormItem, props, { default: () => children });
export const createModalVNode = (content: RenderContent, props: Omit<ModalConfig, 'content'>) =>
Modal.open({ content, closable: false, maskClosable: false, escToClose: false, ...props });
export const arrayToTree = (list: any[], parentId: unknown = 0, config = {}) => {
const idKey = get(config, 'idKey', 'id');
const parentKey = get(config, 'parentKey', 'parent_id');
const childrenKey = get(config, 'childrenKey', 'children');
const removeEmpty = get(config, 'removeEmpty', false);
return list
.filter((item) => item[parentKey] === parentId)
.map((item) => {
item[childrenKey] = arrayToTree(list, item[idKey], config);
if (removeEmpty && item[childrenKey].length === 0) {
delete item[childrenKey];
}
return item;
});
};
export const findTreeParentIds = (nodeData: AnyObject[], parentId: any, config = {}): any[] => {
const ids: any[] = [];
const idKey = get(config, 'idKey', 'id');
const parentKey = get(config, 'parentKey', 'parent_id');
nodeData
?.filter((item) => item[idKey] === parentId)
?.forEach((item) => ids.push(item[idKey], ...findTreeParentIds(nodeData, item[parentKey])));
return ids;
};
export const findTreeChildIds = (nodeData: AnyObject[], nodeId: any, config = {}): any[] => {
const ids: any[] = [];
const idKey = get(config, 'idKey', 'id');
const parentKey = get(config, 'parentKey', 'parent_id');
nodeData
?.filter((item) => item[parentKey] === nodeId)
?.forEach((item) => ids.push(item[idKey], ...findTreeChildIds(nodeData, item[idKey])));
return ids;
};
export const bytesForHuman = (bytes: number, decimals = 2) => {
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
let i = 0;
// eslint-disable-next-line no-plusplus
for (i; bytes > 1024; i++) {
bytes /= 1024;
}
return `${parseFloat(bytes.toFixed(decimals))} ${units[i]}`;
};
export const audioBufferToWav = (audioBuffer: AudioBuffer, len: number) => {
const numOfChan = audioBuffer.numberOfChannels;
const length = len * numOfChan * 2 + 44;
const buffer = new ArrayBuffer(length);
const view = new DataView(buffer);
const channels: Float32Array[] = [];
let i: number;
let sample: number;
let offset = 0;
let pos = 0;
// write WAVE header
// eslint-disable-next-line no-use-before-define
setUint32(0x46464952); // "RIFF"
// eslint-disable-next-line no-use-before-define
setUint32(length - 8); // file length - 8
// eslint-disable-next-line no-use-before-define
setUint32(0x45564157); // "WAVE"
// eslint-disable-next-line no-use-before-define
setUint32(0x20746d66); // "fmt " chunk
// eslint-disable-next-line no-use-before-define
setUint32(16); // length = 16
// eslint-disable-next-line no-use-before-define
setUint16(1); // PCM (uncompressed)
// eslint-disable-next-line no-use-before-define
setUint16(numOfChan);
// eslint-disable-next-line no-use-before-define
setUint32(audioBuffer.sampleRate);
// eslint-disable-next-line no-use-before-define
setUint32(audioBuffer.sampleRate * 2 * numOfChan); // avg. bytes/sec
// eslint-disable-next-line no-use-before-define
setUint16(numOfChan * 2); // block-align
// eslint-disable-next-line no-use-before-define
setUint16(16); // 16-bit (hardcoded in this demo)
// eslint-disable-next-line no-use-before-define
setUint32(0x61746164); // "data" - chunk
// eslint-disable-next-line no-use-before-define
setUint32(length - pos - 4); // chunk length
// write interleaved data
// eslint-disable-next-line no-plusplus
for (i = 0; i < audioBuffer.numberOfChannels; i++) {
// @ts-ignore
channels.push(audioBuffer.getChannelData(i));
}
while (pos < length) {
// eslint-disable-next-line no-plusplus
for (i = 0; i < numOfChan; i++) {
// interleave channels
sample = Math.max(-1, Math.min(1, channels[i][offset])); // clamp
// eslint-disable-next-line no-bitwise
sample = (0.5 + sample < 0 ? sample * 32768 : sample * 32767) | 0; // scale to 16-bit signed int
view.setInt16(pos, sample, true); // write 16-bit sample
pos += 2;
}
// eslint-disable-next-line no-plusplus
offset++; // next source sample
}
// create Blob
return new Blob([buffer], { type: 'audio/wav' });
function setUint16(data: number) {
view.setUint16(pos, data, true);
pos += 2;
}
function setUint32(data: number) {
view.setUint32(pos, data, true);
pos += 4;
}
};
export const getLyricTimeArr = (lyric: string): string[] => {
const times: string[] = [];
lyric?.split('\n').forEach((item) => {
item = item.replace(/(^\s*)|(\s*$)/g, '');
times.push(item.substring(item.indexOf('[') + 1, item.indexOf(']')));
});
return compact(times);
};
export const promiseToBoolean = (callback: Promise<any> | undefined) => callback?.then(() => true).catch(() => false) || false;
export default null;