is-number
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -1,4 +0,1 @@ | ||
--- | ||
tags: ['verb-tag-jscomments'] | ||
--- | ||
# {%= name %} {%= badge("fury") %} | ||
@@ -21,9 +18,71 @@ | ||
var isNumber = require('{%= name %}'); | ||
console.log(isNumber('abc')); | ||
//=> ['a', 'b', 'c']; | ||
``` | ||
## API | ||
{%= jscomments("index.js") %} | ||
### true | ||
```js | ||
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: | ||
```js | ||
var isNumber = require('is-number'); | ||
function isNum(val) { | ||
return isNumber(val) && isFinite(val); | ||
} | ||
``` | ||
### false | ||
```js | ||
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([]); | ||
``` | ||
## Notes | ||
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](https://gist.github.com/jonschlinkert/e30c70c713da325d0e81) for some oddities). | ||
## Author | ||
@@ -30,0 +89,0 @@ {%= include("author") %} |
11
index.js
@@ -0,9 +1,12 @@ | ||
/*! | ||
* is-number <https://github.com/jonschlinkert/is-number> | ||
* | ||
* Copyright (c) 2014 Jon Schlinkert, contributors. | ||
* Licensed under the MIT License | ||
*/ | ||
'use strict'; | ||
module.exports = function isNumber(n) { | ||
return n != null && !!(+n+1/1); | ||
return !!(+n) || n === 0 || n === '0'; | ||
}; | ||
{ | ||
"name": "is-number", | ||
"description": "Is the value a number?", | ||
"version": "0.1.0", | ||
"description": "Is the value a number? Has extensive tests.", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/jonschlinkert/is-number", | ||
@@ -36,10 +36,17 @@ "author": { | ||
"keywords": [ | ||
"docs", | ||
"documentation", | ||
"generate", | ||
"generator", | ||
"markdown", | ||
"templates", | ||
"verb" | ||
"coerce", | ||
"coercion", | ||
"integer", | ||
"is", | ||
"istype", | ||
"javascript", | ||
"math", | ||
"number", | ||
"test", | ||
"type", | ||
"typeof", | ||
"util", | ||
"utility", | ||
"value" | ||
] | ||
} | ||
} |
105
test.js
@@ -14,9 +14,80 @@ /*! | ||
var pass = [0, '0', '012', '012', 1, '1', 1.1, '1.1', -1.1, '-1.1', 10, '10', 10.10, '10.10', 100, '100', Math.abs(1), Math.acos(1), Math.asin(1), Math.atan(1), Math.atan2(1, 2), Math.ceil(1), Math.cos(1), Math.E, Math.exp(1), Math.floor(1), Math.LN10, Math.LN2, Math.log(1), Math.LOG10E, Math.LOG2E, Math.max(1, 2), Math.min(1, 2), Math.PI, Math.pow(1, 2), Math.pow(5, 5), Math.random(1), Math.round(1), Math.sin(1), Math.sqrt(1), Math.SQRT1_2, Math.SQRT2, Math.tan(1), Number.MAX_VALUE, Number.MIN_VALUE, 5e3, '5e3', 0xff, '0xff', Infinity, 'Infinity']; | ||
var shouldPass = [ 0, | ||
5e3, | ||
-1.1, | ||
0, | ||
var fail = ['3abc', 'abc', 'abc3', [1, 2, 3], function () {}, new Buffer('abc'), null, 'null', 'undefined', undefined, {abc: 'abc'}, {} ]; | ||
// 012, Octal literal not allowed in strict mode | ||
parseInt('012'), | ||
parseFloat('012'), | ||
0xff, | ||
1, | ||
1.1, | ||
10, | ||
10.10, | ||
100, | ||
Math.abs(1), | ||
Math.acos(1), | ||
Math.asin(1), | ||
Math.atan(1), | ||
Math.atan2(1, 2), | ||
Math.ceil(1), | ||
Math.cos(1), | ||
Math.E, | ||
Math.exp(1), | ||
Math.floor(1), | ||
Math.LN10, | ||
Math.LN2, | ||
Math.log(1), | ||
Math.LOG10E, | ||
Math.LOG2E, | ||
Math.max(1, 2), | ||
Math.min(1, 2), | ||
Math.PI, | ||
Math.pow(1, 2), | ||
Math.pow(5, 5), | ||
Math.random(1), | ||
Math.round(1), | ||
Math.sin(1), | ||
Math.sqrt(1), | ||
Math.SQRT1_2, | ||
Math.SQRT2, | ||
Math.tan(1), | ||
Number.MAX_VALUE, | ||
Number.MIN_VALUE, | ||
// these fail in strict mode | ||
'-1.1', | ||
'0', | ||
'012', | ||
'0xff', | ||
'1', | ||
'1.1', | ||
'10', | ||
'10.10', | ||
'100', | ||
'5e3' | ||
]; | ||
var shouldFail = [ | ||
'3abc', | ||
'abc', | ||
'abc3', | ||
'null', | ||
'undefined', | ||
[1, 2, 3], | ||
function () {}, | ||
new Buffer('abc'), | ||
null, | ||
undefined, | ||
{abc: 'abc'}, | ||
{}, | ||
[] | ||
]; | ||
describe('is a number', function () { | ||
pass.forEach(function (num) { | ||
shouldPass.forEach(function (num) { | ||
it('"' + num + '" should be a number', function () { | ||
@@ -26,6 +97,32 @@ assert.equal(isNumber(num), true); | ||
}); | ||
assert.equal(isNumber(Infinity), true); | ||
assert.equal(isNumber('Infinity'), true); | ||
}); | ||
describe('is a finite number:', function () { | ||
function isNum(val) { | ||
return isNumber(val) && isFinite(val); | ||
} | ||
assert.equal(isNum(Infinity), false); | ||
assert.equal(isNum('Infinity'), false); | ||
shouldPass.forEach(function (num) { | ||
it('"' + num + '" should be a number', function () { | ||
assert.equal(isNum(num), true); | ||
}); | ||
}); | ||
shouldFail.forEach(function (num) { | ||
it('"' + num + '" should be a number', function () { | ||
assert.equal(isNum(num), false); | ||
}); | ||
}); | ||
}); | ||
describe('is not a number', function () { | ||
fail.forEach(function (num) { | ||
shouldFail.forEach(function (num) { | ||
it('"' + num + '" should not be a number', function () { | ||
@@ -32,0 +129,0 @@ assert.equal(isNumber(num), false); |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
9060
10
120
0
105