1 // Copyright (c) 2012, Mark Cavage. All rights reserved.
2 // Copyright 2015 Joyent, Inc.
4 var assert = require('assert');
5 var Stream = require('stream').Stream;
6 var util = require('util');
12 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}$/;
17 function _capitalize(str) {
18 return (str.charAt(0).toUpperCase() + str.slice(1));
21 function _toss(name, expected, oper, arg, actual) {
22 throw new assert.AssertionError({
23 message: util.format('%s (%s) is required', name, expected),
24 actual: (actual === undefined) ? typeof (arg) : actual(arg),
26 operator: oper || '===',
27 stackStartFunction: _toss.caller
31 function _getClass(arg) {
32 return (Object.prototype.toString.call(arg).slice(8, -1));
36 // Why even bother with asserts?
44 check: function (arg) { return typeof (arg) === 'boolean'; }
47 check: function (arg) { return typeof (arg) === 'function'; }
50 check: function (arg) { return typeof (arg) === 'string'; }
53 check: function (arg) {
54 return typeof (arg) === 'object' && arg !== null;
58 check: function (arg) {
59 return typeof (arg) === 'number' && !isNaN(arg) && isFinite(arg);
63 check: function (arg) { return Buffer.isBuffer(arg); },
64 operator: 'Buffer.isBuffer'
67 check: function (arg) { return Array.isArray(arg); },
68 operator: 'Array.isArray'
71 check: function (arg) { return arg instanceof Stream; },
72 operator: 'instanceof',
76 check: function (arg) { return arg instanceof Date; },
77 operator: 'instanceof',
81 check: function (arg) { return arg instanceof RegExp; },
82 operator: 'instanceof',
86 check: function (arg) {
87 return typeof (arg) === 'string' && UUID_REGEXP.test(arg);
93 function _setExports(ndebug) {
94 var keys = Object.keys(types);
97 /* re-export standard assert */
98 if (process.env.NODE_NDEBUG) {
101 out = function (arg, msg) {
103 _toss(msg, 'true', arg);
108 /* standard checks */
109 keys.forEach(function (k) {
115 out[k] = function (arg, msg) {
116 if (!type.check(arg)) {
117 _toss(msg, k, type.operator, arg, type.actual);
122 /* optional checks */
123 keys.forEach(function (k) {
124 var name = 'optional' + _capitalize(k);
130 out[name] = function (arg, msg) {
131 if (arg === undefined || arg === null) {
134 if (!type.check(arg)) {
135 _toss(msg, k, type.operator, arg, type.actual);
141 keys.forEach(function (k) {
142 var name = 'arrayOf' + _capitalize(k);
148 var expected = '[' + k + ']';
149 out[name] = function (arg, msg) {
150 if (!Array.isArray(arg)) {
151 _toss(msg, expected, type.operator, arg, type.actual);
154 for (i = 0; i < arg.length; i++) {
155 if (!type.check(arg[i])) {
156 _toss(msg, expected, type.operator, arg, type.actual);
162 /* optionalArrayOf checks */
163 keys.forEach(function (k) {
164 var name = 'optionalArrayOf' + _capitalize(k);
170 var expected = '[' + k + ']';
171 out[name] = function (arg, msg) {
172 if (arg === undefined || arg === null) {
175 if (!Array.isArray(arg)) {
176 _toss(msg, expected, type.operator, arg, type.actual);
179 for (i = 0; i < arg.length; i++) {
180 if (!type.check(arg[i])) {
181 _toss(msg, expected, type.operator, arg, type.actual);
187 /* re-export built-in assertions */
188 Object.keys(assert).forEach(function (k) {
189 if (k === 'AssertionError') {
200 /* export ourselves (for unit tests _only_) */
201 out._setExports = _setExports;
206 module.exports = _setExports(process.env.NODE_NDEBUG);