This repository has been archived on 2025-10-29. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FPGA_WebLab/src/utils/Common.ts

71 lines
1.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}