-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ljs:devices): add type narrowing helper isDeviceModelId
- Loading branch information
1 parent
376cff7
commit 3b9c93c
Showing
3 changed files
with
33 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ledgerhq/devices": patch | ||
--- | ||
|
||
Add type narrowing helper isDeviceModelId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { DeviceModelId } from "."; | ||
|
||
export function isDeviceModelId(val: string | undefined | null): val is DeviceModelId { | ||
if (!val) return false; | ||
return Object.values(DeviceModelId).includes(val as DeviceModelId); | ||
} | ||
|
||
export const stringToDeviceModelId = ( | ||
strDeviceModelId: string, | ||
defaultDeviceModelId: DeviceModelId, | ||
): DeviceModelId => { | ||
if (Object.values(DeviceModelId)?.includes(strDeviceModelId as DeviceModelId)) { | ||
return DeviceModelId[strDeviceModelId as DeviceModelId]; | ||
} | ||
|
||
return defaultDeviceModelId; | ||
if (!isDeviceModelId(strDeviceModelId)) return defaultDeviceModelId; | ||
return DeviceModelId[strDeviceModelId]; | ||
}; |