Ampersand.js Is Number Utility
amp-is-number is a small, focused utility function originally developed as part of the Ampersand.js ecosystem. It provides a straightforward method to determine if a given JavaScript value is a number, including numeric strings, consistent with the logic prevalent in projects from the mid-2010s. Ampersand.js itself was a modular, Backbone.js-inspired framework, and `amp-is-number` was one of many atomic modules designed for composability. The package is at version 1.0.1, which appears to be its final release. There is no active development or release cadence for this specific utility, nor for the broader Ampersand.js project, which is considered abandoned. This package exclusively uses CommonJS module syntax, reflecting the standard module patterns of its era, and it lacks explicit support for modern ECMAScript modules (ESM). It differs from more recent `is-number` implementations by not necessarily incorporating newer JavaScript language features or edge case handling, instead focusing on the behavior expected within its original framework context.
Common errors
-
TypeError: require is not a function
cause Attempting to use `require()` in an ECMAScript Module (ESM) file, or importing a CommonJS module (like `amp-is-number`) directly with `import` in a native ESM environment without proper interoperability handling.fixIf in an ESM file, use `await import('amp-is-number')` and access the default export: `(await import('amp-is-number')).default(value)`. Alternatively, switch your file to CommonJS if possible, or ensure your build tools are correctly transpiling.
Warnings
- breaking This package is CommonJS-only (uses `module.exports`). It does not provide an ESM export and will cause a `TypeError: require is not a function` error if directly imported in a pure ES Module environment without a CommonJS wrapper or bundler transpilation.
- gotcha The Ampersand.js project, including `amp-is-number`, is no longer actively maintained. Expect no further updates, bug fixes, or security patches. While simple, its implementation might not cover modern JavaScript edge cases or optimizations found in newer utility libraries.
- gotcha The logic for determining a 'number' might differ subtly from `Number.isFinite()` or other popular `is-number` libraries, especially regarding whitespace-only strings, `null`, or `NaN`. It was designed for the Ampersand.js ecosystem's specific needs.
Install
-
npm install amp-is-number -
yarn add amp-is-number -
pnpm add amp-is-number
Imports
- isNumber
import { isNumber } from 'amp-is-number';const isNumber = require('amp-is-number'); - isNumber (dynamic import)
const isNumber = await import('amp-is-number'); const result = isNumber.default(123);
Quickstart
const isNumber = require('amp-is-number');
console.log('Is 123 a number?', isNumber(123)); // Expected: true
console.log('Is "456" a number?', isNumber('456')); // Expected: true
console.log('Is "hello" a number?', isNumber('hello')); // Expected: false
console.log('Is null a number?', isNumber(null)); // Expected: false
console.log('Is undefined a number?', isNumber(undefined)); // Expected: false
console.log('Is NaN a number?', isNumber(NaN)); // Expected: false
// Example of usage in a conditional
function processValue(value) {
if (isNumber(value)) {
console.log(`Processing number: ${Number(value)}`);
} else {
console.log(`Value is not a recognized number: ${value}`);
}
}
processValue('99.5');
processValue({});