Check.js 6.2 KB

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