Comparing version 0.0.2 to 0.0.3
@@ -7,3 +7,3 @@ { | ||
"homepage": "http://ilee.co.uk", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"keywords": [ | ||
@@ -10,0 +10,0 @@ "isnumeric", |
@@ -1,7 +0,5 @@ | ||
var isNumeric = (function () { | ||
var isNumeric = {}; | ||
var isNumeric = function (obj) { | ||
return !isNaN(parseFloat(obj)) && isFinite(obj) && Object.prototype.toString.call(obj).toLowerCase() !== "[object array]"; | ||
}; | ||
return isNumeric; | ||
})(); | ||
if (typeof (exports) !== "undefined") { | ||
@@ -8,0 +6,0 @@ if (typeof (module) !== "undefined" && module.exports) { |
/*! | ||
isnumeric (v0.0.1) 26-09-2013 | ||
isnumeric (v0.0.2) 26-09-2013 | ||
(c) Lee Crossley <leee@hotmail.co.uk> (http://ilee.co.uk/) | ||
*/ | ||
var isNumeric=function(){var a={};return a}();"undefined"!=typeof exports&&("undefined"!=typeof module&&module.exports&&(exports=module.exports=isNumeric),exports.isNumeric=isNumeric); | ||
var isNumeric=function(a){return!isNaN(parseFloat(a))&&isFinite(a)&&"[object array]"!==Object.prototype.toString.call(a).toLowerCase()};"undefined"!=typeof exports&&("undefined"!=typeof module&&module.exports&&(exports=module.exports=isNumeric),exports.isNumeric=isNumeric); |
@@ -6,3 +6,3 @@ { | ||
"homepage": "http://ilee.co.uk", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"main": "isNumeric.min.js", | ||
@@ -9,0 +9,0 @@ "keywords": [ |
@@ -5,2 +5,62 @@ # isNumeric [![Build Status](https://travis-ci.org/leecrossley/isNumeric.png?branch=master)](https://travis-ci.org/leecrossley/isNumeric) [![npm version](https://badge.fury.io/js/isnumeric.png)](https://npmjs.org/package/isnumeric) [![devDependency Status](https://david-dm.org/leecrossley/isNumeric/dev-status.png)](https://david-dm.org/leecrossley/isNumeric#info=devDependencies) | ||
WIP. | ||
### Using npm | ||
``` | ||
npm install isnumeric | ||
``` | ||
To then include isnumeric in your node app: | ||
``` | ||
var isNumeric = require("isnumeric); | ||
``` | ||
### Direct dependency | ||
Download the minified version [here](http://bit.ly/isnumeric), reference the js file and isNumeric will become a global variable. | ||
## Truthy test cases | ||
### Integers | ||
```javascript | ||
expect(isNumeric(1)).toBeTruthy(); | ||
expect(isNumeric(-1)).toBeTruthy(); | ||
expect(isNumeric(0)).toBeTruthy(); | ||
expect(isNumeric("1")).toBeTruthy(); | ||
expect(isNumeric("-1")).toBeTruthy(); | ||
expect(isNumeric("0")).toBeTruthy(); | ||
``` | ||
### Octals | ||
```javascript | ||
expect(isNumeric(0144)).toBeTruthy(); | ||
expect(isNumeric("0144")).toBeTruthy(); | ||
``` | ||
### Hexadecimals | ||
```javascript | ||
expect(isNumeric(0xFF)).toBeTruthy(); | ||
expect(isNumeric("0xFF")).toBeTruthy(); | ||
``` | ||
### Floating-points | ||
```javascript | ||
expect(isNumeric(1.1)).toBeTruthy(); | ||
expect(isNumeric(-1.1)).toBeTruthy(); | ||
expect(isNumeric("1.1")).toBeTruthy(); | ||
expect(isNumeric("-1.1")).toBeTruthy(); | ||
``` | ||
### Exponentials | ||
```javascript | ||
expect(isNumeric(3e5)).toBeTruthy(); | ||
expect(isNumeric(123e-2)).toBeTruthy(); | ||
expect(isNumeric("3e5")).toBeTruthy(); | ||
expect(isNumeric("123e-2")).toBeTruthy(); | ||
``` |
91
spec.js
@@ -7,2 +7,93 @@ describe("isNumeric", function() { | ||
it("should return true for integers", function() { | ||
expect(isNumeric(1)).toBeTruthy(); | ||
expect(isNumeric(-1)).toBeTruthy(); | ||
expect(isNumeric(0)).toBeTruthy(); | ||
expect(isNumeric("1")).toBeTruthy(); | ||
expect(isNumeric("-1")).toBeTruthy(); | ||
expect(isNumeric("0")).toBeTruthy(); | ||
}); | ||
it("should return true for octals", function() { | ||
expect(isNumeric(0144)).toBeTruthy(); | ||
expect(isNumeric("0144")).toBeTruthy(); | ||
}); | ||
it("should return true for hexadecimals", function() { | ||
expect(isNumeric(0xFF)).toBeTruthy(); | ||
expect(isNumeric("0xFF")).toBeTruthy(); | ||
}); | ||
it("should return true for floating-points", function() { | ||
expect(isNumeric(1.1)).toBeTruthy(); | ||
expect(isNumeric(-1.1)).toBeTruthy(); | ||
expect(isNumeric("1.1")).toBeTruthy(); | ||
expect(isNumeric("-1.1")).toBeTruthy(); | ||
}); | ||
it("should return true for exponentials", function() { | ||
expect(isNumeric(3e5)).toBeTruthy(); | ||
expect(isNumeric(123e-2)).toBeTruthy(); | ||
expect(isNumeric("3e5")).toBeTruthy(); | ||
expect(isNumeric("123e-2")).toBeTruthy(); | ||
}); | ||
it("should return false for empty / whitespace", function() { | ||
expect(isNumeric()).toBeFalsy(); | ||
expect(isNumeric("")).toBeFalsy(); | ||
expect(isNumeric(" ")).toBeFalsy(); | ||
expect(isNumeric(" ")).toBeFalsy(); | ||
expect(isNumeric("\t")).toBeFalsy(); | ||
}); | ||
it("should return false for strings that aren't numeric", function() { | ||
expect(isNumeric("ABC")).toBeFalsy(); | ||
expect(isNumeric("abc")).toBeFalsy(); | ||
expect(isNumeric("ABC123")).toBeFalsy(); | ||
expect(isNumeric("abc123")).toBeFalsy(); | ||
expect(isNumeric("123ABC")).toBeFalsy(); | ||
expect(isNumeric("123abc")).toBeFalsy(); | ||
}); | ||
it("should return false for booleans", function() { | ||
expect(isNumeric(true)).toBeFalsy(); | ||
expect(isNumeric(false)).toBeFalsy(); | ||
}); | ||
it("should return false for null / undefined / NaN", function() { | ||
expect(isNumeric(null)).toBeFalsy(); | ||
expect(isNumeric(undefined)).toBeFalsy(); | ||
expect(isNumeric(NaN)).toBeFalsy(); | ||
}); | ||
it("should return false for infinity and Number._INFINITY (ironically)", function() { | ||
expect(isNumeric(Infinity)).toBeFalsy(); | ||
expect(isNumeric(Number.POSITIVE_INFINITY)).toBeFalsy(); | ||
expect(isNumeric(Number.NEGATIVE_INFINITY)).toBeFalsy(); | ||
}); | ||
it("should return false for dates", function() { | ||
expect(isNumeric(new Date())).toBeFalsy(); | ||
expect(isNumeric(new Date(2000, 1, 1))).toBeFalsy(); | ||
}); | ||
it("should return false for arrays", function() { | ||
expect(isNumeric([])).toBeFalsy(); | ||
expect(isNumeric([1])).toBeFalsy(); | ||
expect(isNumeric([1, 2])).toBeFalsy(); | ||
expect(isNumeric(["a"])).toBeFalsy(); | ||
expect(isNumeric(["a", "b"])).toBeFalsy(); | ||
}); | ||
it("should return false for empty objects", function() { | ||
expect(isNumeric({})).toBeFalsy(); | ||
}); | ||
it("should return false for functions", function() { | ||
expect(isNumeric(function() { })).toBeFalsy(); | ||
expect(isNumeric(function(a) { })).toBeFalsy(); | ||
expect(isNumeric(function() { return "a"; })).toBeFalsy(); | ||
expect(isNumeric(function() { return 1; })).toBeFalsy(); | ||
}); | ||
}); |
8748
155
65