assert-plus
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -12,2 +12,3 @@ // Copyright (c) 2012, Mark Cavage. All rights reserved. | ||
var NDEBUG = process.env.NODE_NDEBUG || false; | ||
var UUID_REGEXP = /^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/; | ||
@@ -57,3 +58,25 @@ | ||
function _instanceof(arg, type, name, stackFunc) { | ||
if (!NDEBUG) { | ||
name = name || type; | ||
stackFunc = stackFunc || _instanceof.caller; | ||
if (!(arg instanceof type)) { | ||
throw new assert.AssertionError({ | ||
message: _(TYPE_REQUIRED, name, type.name), | ||
actual: _getClass(arg), | ||
expected: type.name, | ||
operator: 'instanceof', | ||
stackStartFunction: stackFunc | ||
}); | ||
} | ||
} | ||
} | ||
function _getClass(object) { | ||
return (Object.prototype.toString.call(object).slice(8, -1)); | ||
}; | ||
///--- API | ||
@@ -90,3 +113,3 @@ | ||
throw new assert.AssertionError({ | ||
message: _(TYPE_REQUIRED, name, type), | ||
message: _(TYPE_REQUIRED, name || '', 'Buffer'), | ||
actual: typeof (arg), | ||
@@ -108,2 +131,11 @@ expected: 'buffer', | ||
_assert(arg, 'number', name); | ||
if (!NDEBUG && (isNaN(arg) || !isFinite(arg))) { | ||
throw new assert.AssertionError({ | ||
message: _(TYPE_REQUIRED, name, 'number'), | ||
actual: arg, | ||
expected: 'number', | ||
operator: 'isNaN', | ||
stackStartFunction: number | ||
}); | ||
} | ||
} | ||
@@ -118,14 +150,15 @@ | ||
function stream(arg, name) { | ||
if (!(arg instanceof Stream)) { | ||
throw new assert.AssertionError({ | ||
message: _(TYPE_REQUIRED, name, type), | ||
actual: typeof (arg), | ||
expected: 'Stream', | ||
operator: 'instanceof', | ||
stackStartFunction: buffer | ||
}); | ||
} | ||
_instanceof(arg, Stream, name); | ||
} | ||
function date(arg, name) { | ||
_instanceof(arg, Date, name); | ||
} | ||
function regexp(arg, name) { | ||
_instanceof(arg, RegExp, name); | ||
} | ||
function string(arg, name) { | ||
@@ -136,3 +169,16 @@ _assert(arg, 'string', name); | ||
function uuid(arg, name) { | ||
string(arg, name); | ||
if (!NDEBUG && !UUID_REGEXP.test(arg)) { | ||
throw new assert.AssertionError({ | ||
message: _(TYPE_REQUIRED, name, 'uuid'), | ||
actual: 'string', | ||
expected: 'uuid', | ||
operator: 'test', | ||
stackStartFunction: uuid | ||
}); | ||
} | ||
} | ||
///--- Exports | ||
@@ -143,7 +189,10 @@ | ||
buffer: buffer, | ||
date: date, | ||
func: func, | ||
number: number, | ||
object: object, | ||
regexp: regexp, | ||
stream: stream, | ||
string: string | ||
string: string, | ||
uuid: uuid | ||
}; | ||
@@ -150,0 +199,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "Extra assertions on top of node's assert module", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"main": "./assert.js", | ||
@@ -8,0 +8,0 @@ "devDependencies": {}, |
10571
186