Initial commit
[yaffs-website] / node_modules / assert-plus / assert.js
1 // Copyright (c) 2012, Mark Cavage. All rights reserved.
2 // Copyright 2015 Joyent, Inc.
3
4 var assert = require('assert');
5 var Stream = require('stream').Stream;
6 var util = require('util');
7
8
9 ///--- Globals
10
11 /* JSSTYLED */
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}$/;
13
14
15 ///--- Internal
16
17 function _capitalize(str) {
18     return (str.charAt(0).toUpperCase() + str.slice(1));
19 }
20
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),
25         expected: expected,
26         operator: oper || '===',
27         stackStartFunction: _toss.caller
28     });
29 }
30
31 function _getClass(arg) {
32     return (Object.prototype.toString.call(arg).slice(8, -1));
33 }
34
35 function noop() {
36     // Why even bother with asserts?
37 }
38
39
40 ///--- Exports
41
42 var types = {
43     bool: {
44         check: function (arg) { return typeof (arg) === 'boolean'; }
45     },
46     func: {
47         check: function (arg) { return typeof (arg) === 'function'; }
48     },
49     string: {
50         check: function (arg) { return typeof (arg) === 'string'; }
51     },
52     object: {
53         check: function (arg) {
54             return typeof (arg) === 'object' && arg !== null;
55         }
56     },
57     number: {
58         check: function (arg) {
59             return typeof (arg) === 'number' && !isNaN(arg) && isFinite(arg);
60         }
61     },
62     buffer: {
63         check: function (arg) { return Buffer.isBuffer(arg); },
64         operator: 'Buffer.isBuffer'
65     },
66     array: {
67         check: function (arg) { return Array.isArray(arg); },
68         operator: 'Array.isArray'
69     },
70     stream: {
71         check: function (arg) { return arg instanceof Stream; },
72         operator: 'instanceof',
73         actual: _getClass
74     },
75     date: {
76         check: function (arg) { return arg instanceof Date; },
77         operator: 'instanceof',
78         actual: _getClass
79     },
80     regexp: {
81         check: function (arg) { return arg instanceof RegExp; },
82         operator: 'instanceof',
83         actual: _getClass
84     },
85     uuid: {
86         check: function (arg) {
87             return typeof (arg) === 'string' && UUID_REGEXP.test(arg);
88         },
89         operator: 'isUUID'
90     }
91 };
92
93 function _setExports(ndebug) {
94     var keys = Object.keys(types);
95     var out;
96
97     /* re-export standard assert */
98     if (process.env.NODE_NDEBUG) {
99         out = noop;
100     } else {
101         out = function (arg, msg) {
102             if (!arg) {
103                 _toss(msg, 'true', arg);
104             }
105         };
106     }
107
108     /* standard checks */
109     keys.forEach(function (k) {
110         if (ndebug) {
111             out[k] = noop;
112             return;
113         }
114         var type = types[k];
115         out[k] = function (arg, msg) {
116             if (!type.check(arg)) {
117                 _toss(msg, k, type.operator, arg, type.actual);
118             }
119         };
120     });
121
122     /* optional checks */
123     keys.forEach(function (k) {
124         var name = 'optional' + _capitalize(k);
125         if (ndebug) {
126             out[name] = noop;
127             return;
128         }
129         var type = types[k];
130         out[name] = function (arg, msg) {
131             if (arg === undefined || arg === null) {
132                 return;
133             }
134             if (!type.check(arg)) {
135                 _toss(msg, k, type.operator, arg, type.actual);
136             }
137         };
138     });
139
140     /* arrayOf checks */
141     keys.forEach(function (k) {
142         var name = 'arrayOf' + _capitalize(k);
143         if (ndebug) {
144             out[name] = noop;
145             return;
146         }
147         var type = types[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);
152             }
153             var i;
154             for (i = 0; i < arg.length; i++) {
155                 if (!type.check(arg[i])) {
156                     _toss(msg, expected, type.operator, arg, type.actual);
157                 }
158             }
159         };
160     });
161
162     /* optionalArrayOf checks */
163     keys.forEach(function (k) {
164         var name = 'optionalArrayOf' + _capitalize(k);
165         if (ndebug) {
166             out[name] = noop;
167             return;
168         }
169         var type = types[k];
170         var expected = '[' + k + ']';
171         out[name] = function (arg, msg) {
172             if (arg === undefined || arg === null) {
173                 return;
174             }
175             if (!Array.isArray(arg)) {
176                 _toss(msg, expected, type.operator, arg, type.actual);
177             }
178             var i;
179             for (i = 0; i < arg.length; i++) {
180                 if (!type.check(arg[i])) {
181                     _toss(msg, expected, type.operator, arg, type.actual);
182                 }
183             }
184         };
185     });
186
187     /* re-export built-in assertions */
188     Object.keys(assert).forEach(function (k) {
189         if (k === 'AssertionError') {
190             out[k] = assert[k];
191             return;
192         }
193         if (ndebug) {
194             out[k] = noop;
195             return;
196         }
197         out[k] = assert[k];
198     });
199
200     /* export ourselves (for unit tests _only_) */
201     out._setExports = _setExports;
202
203     return out;
204 }
205
206 module.exports = _setExports(process.env.NODE_NDEBUG);