Supporting Node, AMDs and the browsers ( IE 6+, Chrome 1+, Firefox 3.6+, Opera 12.1+, Safari 5.1+ )
Install node-is as dependency using npm in the command line
> npm install node-js --save
Include the module in your projects javascript file
var is = require("node-is");
Download the code from Github: is.min.js
Include it in your project
<script src="path/to/is.min.js" type="text/Javascript"></script>
is.Number(1)
test for numbersis.Integer(1)
test for integersis.Float(.001)
test for floating point numbersis.Negative(-1.5)
test for negative valuesis.Positive(1.5)
test for positive valuesis.NaN(NaN)
test for NaN (isNaN
link)is.Finite(1)
test for finite values (isFinite
link)is.Infinity(Infinity)
test forInfinity
and-Infinity
is.Boolean(false)
test for boolansis.True(true)
test for true booleansis.False(false)
test for false booleansis.String("is")
test for stringsis.EmptyString("")
test for empty stringsis.Undefined(undefined)
test for undefinedis.Defined({})
(!is.Undefined
)is.Null(null)
test fornull
is.Function(function() {})
test for functionsis.Array([])
test for arraysis.EmptyArray([])
test for empty arraysis.RegExp(/^/g)
test for regular expressionsis.Object({})
test for objectsis.RealObject({})
tests for real objects,null
, arrays, regular expressions and errors will returnfalse
is.Error(new Error())
test for errorsis.ReferenceError(new ReferenceError())
test for reference errorsis.TypeError(new TypeError())
test for type errorsis.SyntaxError(new SyntaxError())
test for syntax errorsis.URIError(new URIError())
test for uri errorsis.EvalError(new EvalError())
test for eval errorsis.RangeError(new RangeError())
test for range errorsis.RealError(new Error())
test for realError
-objects only, will not matchRangeError
,EvalError
,URIError
,SyntaxError
,TypeError
,ReferenceError
returns two testing functions:
equal()
which can be used to test for various types that have to match all, e.g.:is.type.of(1,0).equal("Number","Integer","!Negative"); // returns true
either()
to test for various types, where at least one has to match, e.g.:is.type.of(1,0,2).either("Float","Integer"); //returns true
equal()
and either()
can be used to test with custom functions too:
is.type.of("hello", "world").equal("String", "!EmptyString", function(elm) { return elm[3]==="l"; }); //returns true
Use with mocha is.expect
You can now check types for your tests with mocha. Example:
var expect = require("node-is").expect;
describe("moche with node-is example", function() {
it("is easy and awesome", function() {
expect.type.of(1).to.be.equal("Integer", "Positive");
expect.type.of(-1).to.be.either("Positive", "Negative");
});
});
Parameters:
name
{String
} test name, the function will be registered tois.<name>
fn
{Function
} test function, should accept one argument and return aBoolean
Example:
// register
is.extend("HelloWorld", function(value) {
return is.String(value) && value === "Hello, World!";
});
// use
is.HelloWorld("Hello, World!"); // returns true
is.HelloWorld("Hello, Earth!"); // returns false
- 0.5.2 Bower release
- 0.5.1 Bug fix on global object (browser), added
is.EmptyArray
, some small improvements - 0.5.0 Added
is.extend
- 0.4.1 Bug fix on error type testing, and some small fixes
- 0.4.0 Rewrote library in ES6, compiled using grunt-traceur
- 0.3.2 Performance optimization, added usage section to README, added detailed support information
- 0.3.1 bug fix #1
- 0.3.0 Added mocha support
is.expect
- 0.2.0
is.type.of
can now test an unlimited amount of argumentsis.type.of(..).equal()
andis.type.of(..).either()
can now test custom functions too - 0.1.1 Added travis test
- 0.1.0 initial version