assert.js 5.2 KB

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