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);
63 check: function (arg) {
64 return typeof (arg) === 'number' && !isNaN(arg) && isFinite(arg);
68 check: function (arg) { return Buffer.isBuffer(arg); },
69 operator: 'Buffer.isBuffer'
72 check: function (arg) { return Array.isArray(arg); },
73 operator: 'Array.isArray'
76 check: function (arg) { return arg instanceof Stream; },
77 operator: 'instanceof',
81 check: function (arg) { return arg instanceof Date; },
82 operator: 'instanceof',
86 check: function (arg) { return arg instanceof RegExp; },
87 operator: 'instanceof',
91 check: function (arg) {
92 return typeof (arg) === 'string' && UUID_REGEXP.test(arg);
98 function _setExports(ndebug) {
99 var keys = Object.keys(types);
102 /* re-export standard assert */
103 if (process.env.NODE_NDEBUG) {
106 out = function (arg, msg) {
108 _toss(msg, 'true', arg);
113 /* standard checks */
114 keys.forEach(function (k) {
120 out[k] = function (arg, msg) {
121 if (!type.check(arg)) {
122 _toss(msg, k, type.operator, arg, type.actual);
127 /* optional checks */
128 keys.forEach(function (k) {
129 var name = 'optional' + _capitalize(k);
135 out[name] = function (arg, msg) {
136 if (arg === undefined || arg === null) {
139 if (!type.check(arg)) {
140 _toss(msg, k, type.operator, arg, type.actual);
146 keys.forEach(function (k) {
147 var name = 'arrayOf' + _capitalize(k);
153 var expected = '[' + k + ']';
154 out[name] = function (arg, msg) {
155 if (!Array.isArray(arg)) {
156 _toss(msg, expected, type.operator, arg, type.actual);
159 for (i = 0; i < arg.length; i++) {
160 if (!type.check(arg[i])) {
161 _toss(msg, expected, type.operator, arg, type.actual);
167 /* optionalArrayOf checks */
168 keys.forEach(function (k) {
169 var name = 'optionalArrayOf' + _capitalize(k);
175 var expected = '[' + k + ']';
176 out[name] = function (arg, msg) {
177 if (arg === undefined || arg === null) {
180 if (!Array.isArray(arg)) {
181 _toss(msg, expected, type.operator, arg, type.actual);
184 for (i = 0; i < arg.length; i++) {
185 if (!type.check(arg[i])) {
186 _toss(msg, expected, type.operator, arg, type.actual);
192 /* re-export built-in assertions */
193 Object.keys(assert).forEach(function (k) {
194 if (k === 'AssertionError') {
205 /* export ourselves (for unit tests _only_) */
206 out._setExports = _setExports;
211 module.exports = _setExports(process.env.NODE_NDEBUG);