Check.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import defined from './defined.js';
  2. import DeveloperError from './DeveloperError.js';
  3. /**
  4. * Contains functions for checking that supplied arguments are of a specified type
  5. * or meet specified conditions
  6. * @private
  7. */
  8. var Check = {};
  9. /**
  10. * Contains type checking functions, all using the typeof operator
  11. */
  12. Check.typeOf = {};
  13. function getUndefinedErrorMessage(name) {
  14. return name + ' is required, actual value was undefined';
  15. }
  16. function getFailedTypeErrorMessage(actual, expected, name) {
  17. return 'Expected ' + name + ' to be typeof ' + expected + ', actual typeof was ' + actual;
  18. }
  19. /**
  20. * Throws if test is not defined
  21. *
  22. * @param {String} name The name of the variable being tested
  23. * @param {*} test The value that is to be checked
  24. * @exception {DeveloperError} test must be defined
  25. */
  26. Check.defined = function (name, test) {
  27. if (!defined(test)) {
  28. throw new DeveloperError(getUndefinedErrorMessage(name));
  29. }
  30. };
  31. /**
  32. * Throws if test is not typeof 'function'
  33. *
  34. * @param {String} name The name of the variable being tested
  35. * @param {*} test The value to test
  36. * @exception {DeveloperError} test must be typeof 'function'
  37. */
  38. Check.typeOf.func = function (name, test) {
  39. if (typeof test !== 'function') {
  40. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'function', name));
  41. }
  42. };
  43. /**
  44. * Throws if test is not typeof 'string'
  45. *
  46. * @param {String} name The name of the variable being tested
  47. * @param {*} test The value to test
  48. * @exception {DeveloperError} test must be typeof 'string'
  49. */
  50. Check.typeOf.string = function (name, test) {
  51. if (typeof test !== 'string') {
  52. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'string', name));
  53. }
  54. };
  55. /**
  56. * Throws if test is not typeof 'number'
  57. *
  58. * @param {String} name The name of the variable being tested
  59. * @param {*} test The value to test
  60. * @exception {DeveloperError} test must be typeof 'number'
  61. */
  62. Check.typeOf.number = function (name, test) {
  63. if (typeof test !== 'number') {
  64. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'number', name));
  65. }
  66. };
  67. /**
  68. * Throws if test is not typeof 'number' and less than limit
  69. *
  70. * @param {String} name The name of the variable being tested
  71. * @param {*} test The value to test
  72. * @param {Number} limit The limit value to compare against
  73. * @exception {DeveloperError} test must be typeof 'number' and less than limit
  74. */
  75. Check.typeOf.number.lessThan = function (name, test, limit) {
  76. Check.typeOf.number(name, test);
  77. if (test >= limit) {
  78. throw new DeveloperError('Expected ' + name + ' to be less than ' + limit + ', actual value was ' + test);
  79. }
  80. };
  81. /**
  82. * Throws if test is not typeof 'number' and less than or equal to limit
  83. *
  84. * @param {String} name The name of the variable being tested
  85. * @param {*} test The value to test
  86. * @param {Number} limit The limit value to compare against
  87. * @exception {DeveloperError} test must be typeof 'number' and less than or equal to limit
  88. */
  89. Check.typeOf.number.lessThanOrEquals = function (name, test, limit) {
  90. Check.typeOf.number(name, test);
  91. if (test > limit) {
  92. throw new DeveloperError('Expected ' + name + ' to be less than or equal to ' + limit + ', actual value was ' + test);
  93. }
  94. };
  95. /**
  96. * Throws if test is not typeof 'number' and greater than limit
  97. *
  98. * @param {String} name The name of the variable being tested
  99. * @param {*} test The value to test
  100. * @param {Number} limit The limit value to compare against
  101. * @exception {DeveloperError} test must be typeof 'number' and greater than limit
  102. */
  103. Check.typeOf.number.greaterThan = function (name, test, limit) {
  104. Check.typeOf.number(name, test);
  105. if (test <= limit) {
  106. throw new DeveloperError('Expected ' + name + ' to be greater than ' + limit + ', actual value was ' + test);
  107. }
  108. };
  109. /**
  110. * Throws if test is not typeof 'number' and greater than or equal to limit
  111. *
  112. * @param {String} name The name of the variable being tested
  113. * @param {*} test The value to test
  114. * @param {Number} limit The limit value to compare against
  115. * @exception {DeveloperError} test must be typeof 'number' and greater than or equal to limit
  116. */
  117. Check.typeOf.number.greaterThanOrEquals = function (name, test, limit) {
  118. Check.typeOf.number(name, test);
  119. if (test < limit) {
  120. throw new DeveloperError('Expected ' + name + ' to be greater than or equal to' + limit + ', actual value was ' + test);
  121. }
  122. };
  123. /**
  124. * Throws if test is not typeof 'object'
  125. *
  126. * @param {String} name The name of the variable being tested
  127. * @param {*} test The value to test
  128. * @exception {DeveloperError} test must be typeof 'object'
  129. */
  130. Check.typeOf.object = function (name, test) {
  131. if (typeof test !== 'object') {
  132. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'object', name));
  133. }
  134. };
  135. /**
  136. * Throws if test is not typeof 'boolean'
  137. *
  138. * @param {String} name The name of the variable being tested
  139. * @param {*} test The value to test
  140. * @exception {DeveloperError} test must be typeof 'boolean'
  141. */
  142. Check.typeOf.bool = function (name, test) {
  143. if (typeof test !== 'boolean') {
  144. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'boolean', name));
  145. }
  146. };
  147. /**
  148. * Throws if test1 and test2 is not typeof 'number' and not equal in value
  149. *
  150. * @param {String} name1 The name of the first variable being tested
  151. * @param {String} name2 The name of the second variable being tested against
  152. * @param {*} test1 The value to test
  153. * @param {*} test2 The value to test against
  154. * @exception {DeveloperError} test1 and test2 should be type of 'number' and be equal in value
  155. */
  156. Check.typeOf.number.equals = function (name1, name2, test1, test2) {
  157. Check.typeOf.number(name1, test1);
  158. Check.typeOf.number(name2, test2);
  159. if (test1 !== test2) {
  160. throw new DeveloperError(name1 + ' must be equal to ' + name2 + ', the actual values are ' + test1 + ' and ' + test2);
  161. }
  162. };
  163. export default Check;