Ohm-Management - Projektarbeit B-ME
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.

checks.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2012 Mathieu Turcotte
  3. * Licensed under the MIT license.
  4. */
  5. var util = require('util');
  6. var errors = module.exports = require('./errors');
  7. function failCheck(ExceptionConstructor, callee, messageFormat, formatArgs) {
  8. messageFormat = messageFormat || '';
  9. var message = util.format.apply(this, [messageFormat].concat(formatArgs));
  10. var error = new ExceptionConstructor(message);
  11. Error.captureStackTrace(error, callee);
  12. throw error;
  13. }
  14. function failArgumentCheck(callee, message, formatArgs) {
  15. failCheck(errors.IllegalArgumentError, callee, message, formatArgs);
  16. }
  17. function failStateCheck(callee, message, formatArgs) {
  18. failCheck(errors.IllegalStateError, callee, message, formatArgs);
  19. }
  20. module.exports.checkArgument = function(value, message) {
  21. if (!value) {
  22. failArgumentCheck(arguments.callee, message,
  23. Array.prototype.slice.call(arguments, 2));
  24. }
  25. };
  26. module.exports.checkState = function(value, message) {
  27. if (!value) {
  28. failStateCheck(arguments.callee, message,
  29. Array.prototype.slice.call(arguments, 2));
  30. }
  31. };
  32. module.exports.checkIsDef = function(value, message) {
  33. if (value !== undefined) {
  34. return value;
  35. }
  36. failArgumentCheck(arguments.callee, message ||
  37. 'Expected value to be defined but was undefined.',
  38. Array.prototype.slice.call(arguments, 2));
  39. };
  40. module.exports.checkIsDefAndNotNull = function(value, message) {
  41. // Note that undefined == null.
  42. if (value != null) {
  43. return value;
  44. }
  45. failArgumentCheck(arguments.callee, message ||
  46. 'Expected value to be defined and not null but got "' +
  47. typeOf(value) + '".', Array.prototype.slice.call(arguments, 2));
  48. };
  49. // Fixed version of the typeOf operator which returns 'null' for null values
  50. // and 'array' for arrays.
  51. function typeOf(value) {
  52. var s = typeof value;
  53. if (s == 'object') {
  54. if (!value) {
  55. return 'null';
  56. } else if (value instanceof Array) {
  57. return 'array';
  58. }
  59. }
  60. return s;
  61. }
  62. function typeCheck(expect) {
  63. return function(value, message) {
  64. var type = typeOf(value);
  65. if (type == expect) {
  66. return value;
  67. }
  68. failArgumentCheck(arguments.callee, message ||
  69. 'Expected "' + expect + '" but got "' + type + '".',
  70. Array.prototype.slice.call(arguments, 2));
  71. };
  72. }
  73. module.exports.checkIsString = typeCheck('string');
  74. module.exports.checkIsArray = typeCheck('array');
  75. module.exports.checkIsNumber = typeCheck('number');
  76. module.exports.checkIsBoolean = typeCheck('boolean');
  77. module.exports.checkIsFunction = typeCheck('function');
  78. module.exports.checkIsObject = typeCheck('object');