is-number-checker v1.0.4
is-number-checker

A lightweight utility to check if a given value is a number (with optional support for numeric strings).
π Features
- Zero dependencies: Lightweight and easy to integrate.
- Flexible type-checking: Allows strict number validation or optional support for numeric strings.
- Handles edge cases: Works correctly with Object.create(null), number objects (new Number()), and whitespace-trimmed strings.
- Cross-platform: Works in Node.js, browsers, and other JavaScript environments.
- ESM and CJS support: Fully compatible with modern module systems.
π¦ Installation
You can install is-number-checker using your favorite package manager:
# Using pnpm
pnpm add is-number-checker
# Using npm
npm install is-number-checker
# Using yarn
yarn add is-number-checkerπ Usage
Here's how to use the library in your project:
Import the library
// CommonJS
const isNumber = require('is-number-checker');
// ES Modules
import isNumber from 'is-number-checker';Basic example
console.log(isNumber(123)); // true
console.log(isNumber(-45.67)); // true
console.log(isNumber(NaN)); // false
console.log(isNumber(Infinity)); // false
console.log(isNumber(null)); // false
console.log(isNumber(undefined)); // false
// By default, string numbers are not considered valid
console.log(isNumber("123")); // false
// Allowing string numbers
console.log(isNumber("123", true)); // true
console.log(isNumber(" 3.14 ", true)); // true
console.log(isNumber("hello", true)); // false
console.log(isNumber("", true)); // falseπ§ API
isNumber(value)
Checks if the given value is a number.
Parameters
value(any): The value to check.allowStringNumbers(boolean, optional, default: false): Whether to accept numeric strings as valid numbers.The value to check.
Returns
boolean: Returnstrueifvalueis a valid number; otherwise,false.
π οΈ Development
If you want to contribute or run the project locally, follow these steps:
Clone the repository
git clone https://github.com/angelabenavente/is-number-checker.git
cd is-number-checkerInstall dependencies
npm installRun tests
npm run testLint the code
npm run eslintπ§ͺ Testing
This project uses Jest for testing. To run the test suite, simply use:
npm run testExample output:
PASS ./index.test.js
β returns true for valid numbers (10 ms)
β returns false for non-number values
β handles string numbers correctly when allowed
...Feel free to add more test cases in the test file.
π Changelog
See CHANGELOG.md for a detailed history of changes.
π‘ Contributing
Contributions are welcome! If you'd like to contribute, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Submit a pull request with a clear description of the changes.
See CONTRIBUTING.md for more details.
π License
This project is licensed under the MIT License. Created with β€οΈ by Γngela Benavente.
π Links
GitHub Repository: https://github.com/angelabenavente/is-number-checker
NPM Package: https://www.npmjs.com/package/is-number-checker