71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import { type FileParameter } from "@/APIClient";
|
||
import { isNull, isUndefined } from "lodash";
|
||
|
||
export function toFileParameter(object: File): FileParameter {
|
||
if (isNull(object) || isUndefined(object))
|
||
throw new Error("File is Null or Undefined");
|
||
return {
|
||
data: object,
|
||
fileName: object.name,
|
||
};
|
||
}
|
||
|
||
export function toFileParameterOrNull(
|
||
object?: File | null,
|
||
): FileParameter | null {
|
||
if (isNull(object) || isUndefined(object)) return null;
|
||
else
|
||
return {
|
||
data: object,
|
||
fileName: object.name,
|
||
};
|
||
}
|
||
|
||
export function toFileParameterOrUndefined(
|
||
object?: File | undefined,
|
||
): FileParameter | undefined {
|
||
if (isNull(object) || isUndefined(object)) return undefined;
|
||
else
|
||
return {
|
||
data: object,
|
||
fileName: object.name,
|
||
};
|
||
}
|
||
|
||
// 自定义 Hook:检查依赖注入值是否为空
|
||
export function useRequiredInjection<T>(useFn: () => T | undefined): T {
|
||
const value = useFn();
|
||
if (value === undefined) {
|
||
throw new Error("Missing required injection");
|
||
}
|
||
return value;
|
||
}
|
||
|
||
export function useOptionalInjection<T>(
|
||
useFn: () => T | undefined,
|
||
defaultValue: T,
|
||
): T {
|
||
const value = useFn();
|
||
return value ?? defaultValue;
|
||
}
|
||
|
||
export function formatDate(date: Date | string) {
|
||
const dateObj = typeof date === "string" ? new Date(date) : date;
|
||
return dateObj.toLocaleString("zh-CN", {
|
||
year: "numeric",
|
||
month: "2-digit",
|
||
day: "2-digit",
|
||
hour: "2-digit",
|
||
minute: "2-digit",
|
||
});
|
||
}
|
||
|
||
export function base64ToArrayBuffer(base64: string) {
|
||
var binaryString = atob(base64);
|
||
var bytes = new Uint8Array(binaryString.length);
|
||
for (var i = 0; i < binaryString.length; i++) {
|
||
bytes[i] = binaryString.charCodeAt(i);
|
||
}
|
||
return bytes.buffer;
|
||
}
|