A lightweight utility for parsing integers and real numbers. This module offers a simple way to handle number parsing while enforcing strict type validation by throwing errors when the input is not a valid number.
Install via npm:
npm install @prokopschield/number-parser
The module provides four core functions:
-
parseInt<I>(argument: I): number
- Parses an integer from any input. -
parseReal<I>(argument: I): number
- Parses a real (floating-point) number from any input. -
readInt<P extends object, K extends keyof P>(parent: P, key: K): number
- Reads an integer from a specified property of an object. -
readReal<P extends object, K extends keyof P>(parent: P, key: K): number
- Reads a real number from a specified property of an object.
import {
parseInt,
parseReal,
readInt,
readReal,
} from "@prokopschield/number-parser";
// Parse an integer
const intVal = parseInt("42"); // returns 42
// Parse a real number
const realVal = parseReal("3.14"); // returns 3.14
// Read an integer from an object property
const myObject = { age: "30", weight: "70.5" };
const age = readInt(myObject, "age"); // returns 30
// Read a real number from an object property
const weight = readReal(myObject, "weight"); // returns 70.5
All functions will throw an error if the provided input or property is not a valid number. This ensures strict type safety and prevents unexpected behavior.
// Will throw an error since "abc" is not a valid integer
parseInt("abc");
// Will throw an error if the specified property is not a valid number
const invalidObject = { age: "thirty" };
readInt(invalidObject, "age");
Attempts to parse an integer from the given argument
. Throws an error if the input is not a valid integer.
-
Parameters:
-
argument: I
- The input to be parsed as an integer.
-
- Returns: A number representing the parsed integer.
Attempts to parse a real number (floating-point) from the given argument
. Throws an error if the input is not a valid number.
-
Parameters:
-
argument: I
- The input to be parsed as a real number.
-
- Returns: A number representing the parsed real number.
Attempts to read and parse an integer from the specified property key
of the parent
object. Throws an error if the value is not a valid integer.
-
Parameters:
-
parent: P
- The object containing the property. -
key: K
- The key of the property to read and parse.
-
- Returns: A number representing the parsed integer.
Attempts to read and parse a real number from the specified property key
of the parent
object. Throws an error if the value is not a valid number.
-
Parameters:
-
parent: P
- The object containing the property. -
key: K
- The key of the property to read and parse.
-
- Returns: A number representing the parsed real number.
- Each method has an additional
context?: string
parameter, which, if present, is included in error messages should an error be thrown.
These functions strictly enforce the validation of the input data, throwing errors when encountering invalid or non-numeric inputs:
-
Invalid Integer: If the input for
parseInt
orreadInt
is not a valid integer (e.g., a string that can't be parsed as an integer), an error will be thrown. -
Invalid Real Number: If the input for
parseReal
orreadReal
is not a valid number (e.g., non-numeric characters), an error will be thrown.
Contributions are welcome! Feel free to open an issue or submit a pull request.
This project is licensed under the GNU Lesser General Public License (LGPL).