isNumeric
The `isNumeric` package provides a utility function to determine if a given JavaScript object represents a numeric value. It's designed to be more permissive than standard JavaScript's `Number.isFinite()` or `isNaN()`, accepting various string representations of numbers, including integers, floating-points, octals (e.g., `0144`), hexadecimals (e.g., `0xFF`), and scientific notation (e.g., `3e5`). This package is currently at version 0.3.3 and was last published over 8 years ago, indicating it is no longer actively maintained. Its key differentiator is its broad interpretation of what constitutes a 'numeric' value, especially its handling of string inputs, which includes unusual parsing of 'decimal commas' (e.g., '1,1') that deviates from standard JavaScript number parsing. Given its abandonment, users might consider more modern, actively maintained alternatives or native `Number` methods for stricter validation.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require('isnumeric')` in a JavaScript file treated as an ES module (e.g., `"type": "module"` in `package.json`).fixFor CommonJS modules in an ES module environment, you typically need to use `import { createRequire } from 'module'; const require = createRequire(import.meta.url); const isNumeric = require('isnumeric');` or migrate to an ESM-compatible library. -
TypeError: (0 , isnumeric__WEBPACK_IMPORTED_MODULE_0__.isNumeric) is not a function
cause Incorrectly attempting a named ESM import (`import { isNumeric } from 'isnumeric';`) when the underlying module is CommonJS and only exports a default (or `module.exports = ...`).fixIf using ESM syntax, try importing as a default: `import isNumeric from 'isnumeric';`. If this still causes issues, or if in a CJS context, revert to `const isNumeric = require('isnumeric');`.
Warnings
- breaking The package `isnumeric` is no longer actively maintained, with its last publish date being over 8 years ago. This means it will not receive updates for new JavaScript features, bug fixes, or security vulnerabilities. It is highly recommended to migrate to an actively maintained alternative.
- gotcha The `isNumeric` function considers strings with decimal commas (e.g., `"1,1"`) as numeric and returns `true`. This behavior is non-standard in JavaScript's `Number` parsing (`parseFloat("1,1")` would return `1`) and can lead to unexpected results, particularly in internationalized applications where commas are used as thousands separators, not decimal points, or where strictly numeric input is expected.
- gotcha This package is a CommonJS (CJS) module and does not officially support ES Modules (ESM). While Node.js can sometimes interop, direct named ESM imports (`import { isNumeric } from 'isnumeric';`) will fail. Default ESM imports (`import isNumeric from 'isnumeric';`) might work but rely on Node.js's CJS compatibility layer.
- gotcha The definition of 'numeric' used by `isNumeric` is very broad, including various string formats (octal, hexadecimal, exponential). This is distinct from stricter checks like `Number.isFinite()` which only considers actual `number` primitives that are finite. Relying on `isNumeric` might allow values that are not suitable for mathematical operations without further explicit type coercion and validation.
Install
-
npm install isnumeric -
yarn add isnumeric -
pnpm add isnumeric
Imports
- isNumeric
const isNumeric = require('isnumeric'); - isNumeric
import { isNumeric } from 'isnumeric';import isNumeric from 'isnumeric';
Quickstart
const isNumeric = require('isnumeric');
console.log('--- Truthy Test Cases ---');
console.log(`isNumeric(1): ${isNumeric(1)}`); // Expected: true
console.log(`isNumeric(-1): ${isNumeric(-1)}`); // Expected: true
console.log(`isNumeric("1"): ${isNumeric("1")}`); // Expected: true
console.log(`isNumeric("0xFF"): ${isNumeric("0xFF")}`); // Expected: true (Hexadecimal string)
console.log(`isNumeric(0144): ${isNumeric(0144)}`); // Expected: true (Octal literal, becomes 100 in decimal)
console.log(`isNumeric("1.1"): ${isNumeric("1.1")}`); // Expected: true (Floating-point string)
console.log(`isNumeric("3e5"): ${isNumeric("3e5")}`); // Expected: true (Exponential string)
console.log(`isNumeric("1,1"): ${isNumeric("1,1")}`); // Expected: true (Decimal comma - unusual behavior)
console.log('\n--- Falsy Test Cases (inferred, as README only shows truthy) ---');
console.log(`isNumeric('abc'): ${isNumeric('abc')}`); // Expected: false
console.log(`isNumeric([]): ${isNumeric([])}`); // Expected: false
console.log(`isNumeric({}): ${isNumeric({})}`); // Expected: false
console.log(`isNumeric(null): ${isNumeric(null)}`); // Expected: false
console.log(`isNumeric(undefined): ${isNumeric(undefined)}`); // Expected: false