Created
January 22, 2026 13:54
-
-
Save The-LukeZ/64557ee9d82b09df7f1a302c39cc7ff1 to your computer and use it in GitHub Desktop.
Safe parse function for typescript
This file contains hidden or 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
| /** | |
| * Attempts to parse a string as an integer with validation and clamping. | |
| * | |
| * @param {unknown} str - The string to parse as an integer | |
| * @param {number} defaultValue - The value to return if parsing fails or validation fails | |
| * @param {string} [min=1] - The minimum allowed value (inclusive), defaults to 1 | |
| * @param {number} [max] - The maximum allowed value (inclusive) | |
| * @returns {number} The parsed integer clamped to the range [min, max], or the default value if parsing fails | |
| * | |
| * @example | |
| * ```typescript | |
| * tryToParseInt("42", 0, 100, 1); // Returns 42 | |
| * tryToParseInt("150", 0, 100, 1); // Returns 100 (clamped to max) | |
| * tryToParseInt("abc", 0, 100, 1); // Returns 0 (default value) | |
| * tryToParseInt("0", 10, 100, 1); // Returns 1 (clamped to min) | |
| * tryToParseInt(" ", 2, 100, 1); // Returns 2 (default value, whitespace input) | |
| * ``` | |
| */ | |
| export function safeParseInt(str, defaultValue, min = 1, max = undefined) { | |
| try { | |
| if (typeof str !== "string") { | |
| return defaultValue; // Return default value if input is not a string | |
| } | |
| const num = parseInt(str, 10); // Always specify radix | |
| // Check if parsing failed or string wasn't purely numeric | |
| if (isNaN(num) || !str.trim() || !/^\d+$/.test(str.trim())) { | |
| return defaultValue; | |
| } | |
| // Clamp the value to the range [min, max] (if max is provided) | |
| return Math.max(min, max !== undefined ? Math.min(max, num) : num); | |
| } catch { | |
| return defaultValue; | |
| } | |
| } |
This file contains hidden or 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
| /** | |
| * Attempts to parse a string as an integer with validation and clamping. | |
| * | |
| * @param str - The string to parse as an integer | |
| * @param defaultValue - The value to return if parsing fails or validation fails | |
| * @param min - The minimum allowed value (inclusive), defaults to 1 | |
| * @param max - The maximum allowed value (inclusive) | |
| * @returns The parsed integer clamped to the range [min, max], or the default value if parsing fails | |
| * | |
| * @example | |
| * ```typescript | |
| * tryToParseInt("42", 0, 100, 1); // Returns 42 | |
| * tryToParseInt("150", 0, 100, 1); // Returns 100 (clamped to max) | |
| * tryToParseInt("abc", 0, 100, 1); // Returns 0 (default value) | |
| * tryToParseInt("0", 10, 100, 1); // Returns 1 (clamped to min) | |
| * tryToParseInt(" ", 2, 100, 1); // Returns 2 (default value, whitespace input) | |
| * ``` | |
| */ | |
| export function safeParseInt(str: unknown, defaultValue: number, min = 1, max?: number): number { | |
| try { | |
| if (typeof str !== "string") { | |
| return defaultValue; // Return default value if input is not a string | |
| } | |
| const num = parseInt(str, 10); // Always specify radix | |
| // Check if parsing failed or string wasn't purely numeric | |
| if (isNaN(num) || !str.trim() || !/^\d+$/.test(str.trim())) { | |
| return defaultValue; | |
| } | |
| // Clamp the value to the range [min, max] (if max is provided) | |
| return Math.max(min, max !== undefined ? Math.min(max, num) : num); | |
| } catch { | |
| return defaultValue; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment