mirror of
https://github.com/certd/certd.git
synced 2025-11-25 09:10:11 +08:00
48 lines
971 B
TypeScript
48 lines
971 B
TypeScript
import { isArray } from "lodash-es";
|
|
export default {
|
|
arrayToMap(array: any) {
|
|
if (!array) {
|
|
return {};
|
|
}
|
|
if (!isArray(array)) {
|
|
return array;
|
|
}
|
|
const map: any = {};
|
|
for (const item of array) {
|
|
if (item.key) {
|
|
map[item.key] = item;
|
|
}
|
|
}
|
|
return map;
|
|
},
|
|
mapToArray(map: any) {
|
|
if (!map) {
|
|
return [];
|
|
}
|
|
if (isArray(map)) {
|
|
return map;
|
|
}
|
|
const array: any = [];
|
|
for (const key in map) {
|
|
const item = map[key];
|
|
item.key = key;
|
|
array.push(item);
|
|
}
|
|
return array;
|
|
},
|
|
|
|
async sleep(ms: number) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
},
|
|
|
|
maxLength(str?: string, length = 100) {
|
|
if (str) {
|
|
return str.length > length ? str.slice(0, length) + "..." : str;
|
|
}
|
|
return "";
|
|
},
|
|
transformLink(desc: string = "") {
|
|
return desc.replace(/\[(.*)\]\((.*)\)/g, '<a href="$2" target="_blank">$1</a>');
|
|
}
|
|
};
|