Check.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 (
  18. "Expected " +
  19. name +
  20. " to be typeof " +
  21. expected +
  22. ", actual typeof was " +
  23. actual
  24. );
  25. }
  26. /**
  27. * Throws if test is not defined
  28. *
  29. * @param {String} name The name of the variable being tested
  30. * @param {*} test The value that is to be checked
  31. * @exception {DeveloperError} test must be defined
  32. */
  33. Check.defined = function (name, test) {
  34. if (!defined(test)) {
  35. throw new DeveloperError(getUndefinedErrorMessage(name));
  36. }
  37. };
  38. /**
  39. * Throws if test is not typeof 'function'
  40. *
  41. * @param {String} name The name of the variable being tested
  42. * @param {*} test The value to test
  43. * @exception {DeveloperError} test must be typeof 'function'
  44. */
  45. Check.typeOf.func = function (name, test) {
  46. if (typeof test !== "function") {
  47. throw new DeveloperError(
  48. getFailedTypeErrorMessage(typeof test, "function", name)
  49. );
  50. }
  51. };
  52. /**
  53. * Throws if test is not typeof 'string'
  54. *
  55. * @param {String} name The name of the variable being tested
  56. * @param {*} test The value to test
  57. * @exception {DeveloperError} test must be typeof 'string'
  58. */
  59. Check.typeOf.string = function (name, test) {
  60. if (typeof test !== "string") {
  61. throw new DeveloperError(
  62. getFailedTypeErrorMessage(typeof test, "string", name)
  63. );
  64. }
  65. };
  66. /**
  67. * Throws if test is not typeof 'number'
  68. *
  69. * @param {String} name The name of the variable being tested
  70. * @param {*} test The value to test
  71. * @exception {DeveloperError} test must be typeof 'number'
  72. */
  73. Check.typeOf.number = function (name, test) {
  74. if (typeof test !== "number") {
  75. throw new DeveloperError(
  76. getFailedTypeErrorMessage(typeof test, "number", name)
  77. );
  78. }
  79. };
  80. /**
  81. * Throws if test is not typeof 'number' and less than limit
  82. *
  83. * @param {String} name The name of the variable being tested
  84. * @param {*} test The value to test
  85. * @param {Number} limit The limit value to compare against
  86. * @exception {DeveloperError} test must be typeof 'number' and less than limit
  87. */
  88. Check.typeOf.number.lessThan = function (name, test, limit) {
  89. Check.typeOf.number(name, test);
  90. if (test >= limit) {
  91. throw new DeveloperError(
  92. "Expected " +
  93. name +
  94. " to be less than " +
  95. limit +
  96. ", actual value was " +
  97. test
  98. );
  99. }
  100. };
  101. /**
  102. * Throws if test is not typeof 'number' and less than or equal to limit
  103. *
  104. * @param {String} name The name of the variable being tested
  105. * @param {*} test The value to test
  106. * @param {Number} limit The limit value to compare against
  107. * @exception {DeveloperError} test must be typeof 'number' and less than or equal to limit
  108. */
  109. Check.typeOf.number.lessThanOrEquals = function (name, test, limit) {
  110. Check.typeOf.number(name, test);
  111. if (test > limit) {
  112. throw new DeveloperError(
  113. "Expected " +
  114. name +
  115. " to be less than or equal to " +
  116. limit +
  117. ", actual value was " +
  118. test
  119. );
  120. }
  121. };
  122. /**
  123. * Throws if test is not typeof 'number' and greater than limit
  124. *
  125. * @param {String} name The name of the variable being tested
  126. * @param {*} test The value to test
  127. * @param {Number} limit The limit value to compare against
  128. * @exception {DeveloperError} test must be typeof 'number' and greater than limit
  129. */
  130. Check.typeOf.number.greaterThan = function (name, test, limit) {
  131. Check.typeOf.number(name, test);
  132. if (test <= limit) {
  133. throw new DeveloperError(
  134. "Expected " +
  135. name +
  136. " to be greater than " +
  137. limit +
  138. ", actual value was " +
  139. test
  140. );
  141. }
  142. };
  143. /**
  144. * Throws if test is not typeof 'number' and greater than or equal to limit
  145. *
  146. * @param {String} name The name of the variable being tested
  147. * @param {*} test The value to test
  148. * @param {Number} limit The limit value to compare against
  149. * @exception {DeveloperError} test must be typeof 'number' and greater than or equal to limit
  150. */
  151. Check.typeOf.number.greaterThanOrEquals = function (name, test, limit) {
  152. Check.typeOf.number(name, test);
  153. if (test < limit) {
  154. throw new DeveloperError(
  155. "Expected " +
  156. name +
  157. " to be greater than or equal to" +
  158. limit +
  159. ", actual value was " +
  160. test
  161. );
  162. }
  163. };
  164. /**
  165. * Throws if test is not typeof 'object'
  166. *
  167. * @param {String} name The name of the variable being tested
  168. * @param {*} test The value to test
  169. * @exception {DeveloperError} test must be typeof 'object'
  170. */
  171. Check.typeOf.object = function (name, test) {
  172. if (typeof test !== "object") {
  173. throw new DeveloperError(
  174. getFailedTypeErrorMessage(typeof test, "object", name)
  175. );
  176. }
  177. };
  178. /**
  179. * Throws if test is not typeof 'boolean'
  180. *
  181. * @param {String} name The name of the variable being tested
  182. * @param {*} test The value to test
  183. * @exception {DeveloperError} test must be typeof 'boolean'
  184. */
  185. Check.typeOf.bool = function (name, test) {
  186. if (typeof test !== "boolean") {
  187. throw new DeveloperError(
  188. getFailedTypeErrorMessage(typeof test, "boolean", name)
  189. );
  190. }
  191. };
  192. /**
  193. * Throws if test1 and test2 is not typeof 'number' and not equal in value
  194. *
  195. * @param {String} name1 The name of the first variable being tested
  196. * @param {String} name2 The name of the second variable being tested against
  197. * @param {*} test1 The value to test
  198. * @param {*} test2 The value to test against
  199. * @exception {DeveloperError} test1 and test2 should be type of 'number' and be equal in value
  200. */
  201. Check.typeOf.number.equals = function (name1, name2, test1, test2) {
  202. Check.typeOf.number(name1, test1);
  203. Check.typeOf.number(name2, test2);
  204. if (test1 !== test2) {
  205. throw new DeveloperError(
  206. name1 +
  207. " must be equal to " +
  208. name2 +
  209. ", the actual values are " +
  210. test1 +
  211. " and " +
  212. test2
  213. );
  214. }
  215. };
  216. export default Check;