refactor: tryGetDeviceTypeVal

This commit is contained in:
yangsy
2025-09-30 15:09:12 +08:00
parent 97bf9855c7
commit d0adb869da
11 changed files with 68 additions and 25 deletions

View File

@@ -34,11 +34,14 @@ export const DeviceTypeName: Record<DeviceTypeVal, string> = {
[DeviceType.Keyboard]: '网络键盘',
};
export const getDeviceTypeVal = (devcieTypeCode?: string): DeviceTypeVal => {
/**
* @deprecated
*/
export const getDeviceTypeVal = (deviceTypeCode?: string): DeviceTypeVal => {
let result: DeviceTypeVal = DeviceType.Camera;
if (devcieTypeCode) {
if (deviceTypeCode) {
for (const key in DeviceTypeCode) {
if (DeviceTypeCode[key as DeviceTypeVal].includes(devcieTypeCode)) {
if (DeviceTypeCode[key as DeviceTypeVal].includes(deviceTypeCode)) {
result = key as DeviceTypeVal;
break;
}
@@ -46,3 +49,9 @@ export const getDeviceTypeVal = (devcieTypeCode?: string): DeviceTypeVal => {
}
return result;
};
export const tryGetDeviceTypeVal = (deviceTypeCode?: string) => {
if (!deviceTypeCode) return undefined;
const entry = Object.entries(DeviceTypeCode).find(([, codes]) => codes.includes(deviceTypeCode));
return entry?.[0] as DeviceTypeVal | undefined;
};