Name: js-handler/node_modules/restify/node_modules/assert-plus/assert.js 
1:
// Copyright (c) 2012, Mark Cavage. All rights reserved.
2:
 
3:
var assert = require('assert');
4:
var Stream = require('stream').Stream;
5:
var util = require('util');
6:
 
7:
 
8:
 
9:
///--- Globals
10:
 
11:
var NDEBUG = process.env.NODE_NDEBUG || false;
12:
 
13:
 
14:
 
15:
///--- Messages
16:
 
17:
var ARRAY_TYPE_REQUIRED = '%s ([%s]) required';
18:
var TYPE_REQUIRED = '%s (%s) is required';
19:
 
20:
 
21:
 
22:
///--- Internal
23:
 
24:
function capitalize(str) {
25:
        return (str.charAt(0).toUpperCase() + str.slice(1));
26:
}
27:
 
28:
function uncapitalize(str) {
29:
        return (str.charAt(0).toLowerCase() + str.slice(1));
30:
}
31:
 
32:
function _() {
33:
        return (util.format.apply(util, arguments));
34:
}
35:
 
36:
 
37:
function _assert(arg, type, name, stackFunc) {
38:
        if (!NDEBUG) {
39:
                name = name || type;
40:
                stackFunc = stackFunc || _assert.caller;
41:
                var t = typeof (arg);
42:
 
43:
                if (t !== type) {
44:
                        throw new assert.AssertionError({
45:
                                message: _(TYPE_REQUIRED, name, type),
46:
                                actual: t,
47:
                                expected: type,
48:
                                operator: '===',
49:
                                stackStartFunction: stackFunc
50:
                        });
51:
                }
52:
        }
53:
}
54:
 
55:
 
56:
 
57:
///--- API
58:
 
59:
function array(arr, type, name) {
60:
        if (!NDEBUG) {
61:
                name = name || type;
62:
 
63:
                if (!Array.isArray(arr)) {
64:
                        throw new assert.AssertionError({
65:
                                message: _(ARRAY_TYPE_REQUIRED, name, type),
66:
                                actual: typeof (arr),
67:
                                expected: 'array',
68:
                                operator: 'Array.isArray',
69:
                                stackStartFunction: array.caller
70:
                        });
71:
                }
72:
 
73:
                for (var i = 0; i < arr.length; i++) {
74:
                        _assert(arr[i], type, name, array);
75:
                }
76:
        }
77:
}
78:
 
79:
 
80:
function bool(arg, name) {
81:
        _assert(arg, 'boolean', name, bool);
82:
}
83:
 
84:
 
85:
function buffer(arg, name) {
86:
        if (!Buffer.isBuffer(arg)) {
87:
                throw new assert.AssertionError({
88:
                        message: _(TYPE_REQUIRED, name, type),
89:
                        actual: typeof (arg),
90:
                        expected: 'buffer',
91:
                        operator: 'Buffer.isBuffer',
92:
                        stackStartFunction: buffer
93:
                });
94:
        }
95:
}
96:
 
97:
 
98:
function func(arg, name) {
99:
        _assert(arg, 'function', name);
100:
}
101:
 
102:
 
103:
function number(arg, name) {
104:
        _assert(arg, 'number', name);
105:
}
106:
 
107:
 
108:
function object(arg, name) {
109:
        _assert(arg, 'object', name);
110:
}
111:
 
112:
 
113:
function stream(arg, name) {
114:
        if (!(arg instanceof Stream)) {
115:
                throw new assert.AssertionError({
116:
                        message: _(TYPE_REQUIRED, name, type),
117:
                        actual: typeof (arg),
118:
                        expected: 'Stream',
119:
                        operator: 'instanceof',
120:
                        stackStartFunction: buffer
121:
                });
122:
        }
123:
}
124:
 
125:
 
126:
function string(arg, name) {
127:
        _assert(arg, 'string', name);
128:
}
129:
 
130:
 
131:
 
132:
///--- Exports
133:
 
134:
module.exports = {
135:
        bool: bool,
136:
        buffer: buffer,
137:
        func: func,
138:
        number: number,
139:
        object: object,
140:
        stream: stream,
141:
        string: string
142:
};
143:
 
144:
 
145:
Object.keys(module.exports).forEach(function (k) {
146:
        if (k === 'buffer')
147:
                return;
148:
 
149:
        var name = 'arrayOf' + capitalize(k);
150:
 
151:
        if (k === 'bool')
152:
                k = 'boolean';
153:
        if (k === 'func')
154:
                k = 'function';
155:
        module.exports[name] = function (arg, name) {
156:
                array(arg, k, name);
157:
        };
158:
});
159:
 
160:
Object.keys(module.exports).forEach(function (k) {
161:
        var _name = 'optional' + capitalize(k);
162:
        var s = uncapitalize(k.replace('arrayOf', ''));
163:
        if (s === 'bool')
164:
                s = 'boolean';
165:
        if (s === 'func')
166:
                s = 'function';
167:
 
168:
        if (k.indexOf('arrayOf') !== -1) {
169:
          module.exports[_name] = function (arg, name) {
170:
                  if (!NDEBUG && arg !== undefined) {
171:
                          array(arg, s, name);
172:
                  }
173:
          };
174:
        } else {
175:
          module.exports[_name] = function (arg, name) {
176:
                  if (!NDEBUG && arg !== undefined) {
177:
                          _assert(arg, s, name);
178:
                  }
179:
          };
180:
        }
181:
});
182:
 
183:
 
184:
// Reexport built-in assertions
185:
Object.keys(assert).forEach(function (k) {
186:
        if (k === 'AssertionError') {
187:
                module.exports[k] = assert[k];
188:
                return;
189:
        }
190:
 
191:
        module.exports[k] = function () {
192:
                if (!NDEBUG) {
193:
                        assert[k].apply(assert[k], arguments);
194:
                }
195:
        };
196:
});