Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The is-number npm package is a simple utility for checking if a value is a number. It works with various types of numeric representations, including strings that can be coerced to numbers, and provides a straightforward API for determining if a value can be considered a number in JavaScript.
Check if a value is a number
This feature allows you to check if a value is a number, including numeric strings.
const isNumber = require('is-number');
console.log(isNumber(5)); // true
console.log(isNumber('5')); // true
console.log(isNumber('five')); // false
Check if a value is not a number
This feature allows you to check if a value is not a number, including strings that cannot be coerced to numbers and other non-numeric types.
const isNumber = require('is-number');
console.log(isNumber('foo')); // false
console.log(isNumber([1, 2, 3])); // false
Check if a value is a finite number
This feature allows you to check if a value is a finite number, excluding Infinity, -Infinity, and NaN.
const isNumber = require('is-number');
console.log(isNumber(Infinity)); // false
console.log(isNumber(-Infinity)); // false
console.log(isNumber(NaN)); // false
The isnumeric package offers similar functionality to is-number by checking if a value is numeric. It is similar in its simplicity and direct approach.
The numerable package is a more comprehensive library for number validation and formatting. It provides additional functionality compared to is-number, such as parsing and formatting numbers with localization support.
This package is part of the validate.io suite and checks if a value is a number primitive. It is more strict than is-number as it does not consider numeric strings to be numbers.
Is the value a number? Has extensive tests.
npm i is-number --save-dev
npm test
var isNumber = require('is-number');
isNumber(5e3);
isNumber(0xff);
isNumber(-1.1);
isNumber(0);
isNumber(1);
isNumber(1.1);
isNumber(10);
isNumber(10.10);
isNumber(100);
isNumber('-1.1');
isNumber('0');
isNumber('012');
isNumber('0xff');
isNumber('1');
isNumber('1.1');
isNumber('10');
isNumber('10.10');
isNumber('100');
isNumber('5e3');
isNumber(parseInt('012'));
isNumber(parseFloat('012'));
isNumber(Infinity);
isNumber('Infinity');
If you want Infinity
to be false
, just do:
var isNumber = require('is-number');
function isNum(val) {
return isNumber(val) && isFinite(val);
}
isNumber('3abc');
isNumber('abc');
isNumber('abc3');
isNumber('null');
isNumber('undefined');
isNumber([1, 2, 3]);
isNumber(function () {});
isNumber(new Buffer('abc'));
isNumber(null);
isNumber(undefined);
isNumber({abc: 'abc'});
isNumber({});
isNumber([]);
Instead of using isFinite()
, you can also achieve similar results by using something like ((+n+1) / (+n+1) === 1))
, but this alone allows values like null
to pass as numbers (in JavaScript, the leading +
coerces the value to a number, see this gist for some oddities).
Jon Schlinkert
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
This file was generated by verb-cli on September 21, 2014.
FAQs
Returns true if a number or string value is a finite number. Useful for regex matches, parsing, user input, etc.
The npm package is-number receives a total of 55,632,608 weekly downloads. As such, is-number popularity was classified as popular.
We found that is-number demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.