Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

assert.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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);
  47. }
  48. },
  49. finite: {
  50. check: function (arg) {
  51. return typeof (arg) === 'number' && !isNaN(arg) && isFinite(arg);
  52. }
  53. },
  54. buffer: {
  55. check: function (arg) { return Buffer.isBuffer(arg); },
  56. operator: 'Buffer.isBuffer'
  57. },
  58. array: {
  59. check: function (arg) { return Array.isArray(arg); },
  60. operator: 'Array.isArray'
  61. },
  62. stream: {
  63. check: function (arg) { return arg instanceof Stream; },
  64. operator: 'instanceof',
  65. actual: _getClass
  66. },
  67. date: {
  68. check: function (arg) { return arg instanceof Date; },
  69. operator: 'instanceof',
  70. actual: _getClass
  71. },
  72. regexp: {
  73. check: function (arg) { return arg instanceof RegExp; },
  74. operator: 'instanceof',
  75. actual: _getClass
  76. },
  77. uuid: {
  78. check: function (arg) {
  79. return typeof (arg) === 'string' && UUID_REGEXP.test(arg);
  80. },
  81. operator: 'isUUID'
  82. }
  83. };
  84. function _setExports(ndebug) {
  85. var keys = Object.keys(types);
  86. var out;
  87. /* re-export standard assert */
  88. if (process.env.NODE_NDEBUG) {
  89. out = noop;
  90. } else {
  91. out = function (arg, msg) {
  92. if (!arg) {
  93. _toss(msg, 'true', arg);
  94. }
  95. };
  96. }
  97. /* standard checks */
  98. keys.forEach(function (k) {
  99. if (ndebug) {
  100. out[k] = noop;
  101. return;
  102. }
  103. var type = types[k];
  104. out[k] = function (arg, msg) {
  105. if (!type.check(arg)) {
  106. _toss(msg, k, type.operator, arg, type.actual);
  107. }
  108. };
  109. });
  110. /* optional checks */
  111. keys.forEach(function (k) {
  112. var name = 'optional' + _capitalize(k);
  113. if (ndebug) {
  114. out[name] = noop;
  115. return;
  116. }
  117. var type = types[k];
  118. out[name] = function (arg, msg) {
  119. if (arg === undefined || arg === null) {
  120. return;
  121. }
  122. if (!type.check(arg)) {
  123. _toss(msg, k, type.operator, arg, type.actual);
  124. }
  125. };
  126. });
  127. /* arrayOf checks */
  128. keys.forEach(function (k) {
  129. var name = 'arrayOf' + _capitalize(k);
  130. if (ndebug) {
  131. out[name] = noop;
  132. return;
  133. }
  134. var type = types[k];
  135. var expected = '[' + k + ']';
  136. out[name] = function (arg, msg) {
  137. if (!Array.isArray(arg)) {
  138. _toss(msg, expected, type.operator, arg, type.actual);
  139. }
  140. var i;
  141. for (i = 0; i < arg.length; i++) {
  142. if (!type.check(arg[i])) {
  143. _toss(msg, expected, type.operator, arg, type.actual);
  144. }
  145. }
  146. };
  147. });
  148. /* optionalArrayOf checks */
  149. keys.forEach(function (k) {
  150. var name = 'optionalArrayOf' + _capitalize(k);
  151. if (ndebug) {
  152. out[name] = noop;
  153. return;
  154. }
  155. var type = types[k];
  156. var expected = '[' + k + ']';
  157. out[name] = function (arg, msg) {
  158. if (arg === undefined || arg === null) {
  159. return;
  160. }
  161. if (!Array.isArray(arg)) {
  162. _toss(msg, expected, type.operator, arg, type.actual);
  163. }
  164. var i;
  165. for (i = 0; i < arg.length; i++) {
  166. if (!type.check(arg[i])) {
  167. _toss(msg, expected, type.operator, arg, type.actual);
  168. }
  169. }
  170. };
  171. });
  172. /* re-export built-in assertions */
  173. Object.keys(assert).forEach(function (k) {
  174. if (k === 'AssertionError') {
  175. out[k] = assert[k];
  176. return;
  177. }
  178. if (ndebug) {
  179. out[k] = noop;
  180. return;
  181. }
  182. out[k] = assert[k];
  183. });
  184. /* export ourselves (for unit tests _only_) */
  185. out._setExports = _setExports;
  186. return out;
  187. }
  188. module.exports = _setExports(process.env.NODE_NDEBUG);