Created
July 25, 2024 10:20
-
-
Save tjunghans/1b8b01cb0df293da0e09678423adc30c to your computer and use it in GitHub Desktop.
Typescript function to check if a given value is a valid port number
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
const isPortnumber = (value: number): boolean => { | |
// https://en.wikipedia.org/wiki/Registered_port | |
return value >= 0 && value <= 65535; | |
}; | |
const isValidPortnumber = (value: unknown): value is number => { | |
if (typeof value === "number") { | |
return isPortnumber(value); | |
} | |
if (typeof value === "string") { | |
return isPortnumber(Number(value)); | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment