is.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2013 Lovell Fuller and others.
  2. // SPDX-License-Identifier: Apache-2.0
  3. 'use strict';
  4. /**
  5. * Is this value defined and not null?
  6. * @private
  7. */
  8. const defined = function (val) {
  9. return typeof val !== 'undefined' && val !== null;
  10. };
  11. /**
  12. * Is this value an object?
  13. * @private
  14. */
  15. const object = function (val) {
  16. return typeof val === 'object';
  17. };
  18. /**
  19. * Is this value a plain object?
  20. * @private
  21. */
  22. const plainObject = function (val) {
  23. return Object.prototype.toString.call(val) === '[object Object]';
  24. };
  25. /**
  26. * Is this value a function?
  27. * @private
  28. */
  29. const fn = function (val) {
  30. return typeof val === 'function';
  31. };
  32. /**
  33. * Is this value a boolean?
  34. * @private
  35. */
  36. const bool = function (val) {
  37. return typeof val === 'boolean';
  38. };
  39. /**
  40. * Is this value a Buffer object?
  41. * @private
  42. */
  43. const buffer = function (val) {
  44. return val instanceof Buffer;
  45. };
  46. /**
  47. * Is this value a typed array object?. E.g. Uint8Array or Uint8ClampedArray?
  48. * @private
  49. */
  50. const typedArray = function (val) {
  51. if (defined(val)) {
  52. switch (val.constructor) {
  53. case Uint8Array:
  54. case Uint8ClampedArray:
  55. case Int8Array:
  56. case Uint16Array:
  57. case Int16Array:
  58. case Uint32Array:
  59. case Int32Array:
  60. case Float32Array:
  61. case Float64Array:
  62. return true;
  63. }
  64. }
  65. return false;
  66. };
  67. /**
  68. * Is this value an ArrayBuffer object?
  69. * @private
  70. */
  71. const arrayBuffer = function (val) {
  72. return val instanceof ArrayBuffer;
  73. };
  74. /**
  75. * Is this value a non-empty string?
  76. * @private
  77. */
  78. const string = function (val) {
  79. return typeof val === 'string' && val.length > 0;
  80. };
  81. /**
  82. * Is this value a real number?
  83. * @private
  84. */
  85. const number = function (val) {
  86. return typeof val === 'number' && !Number.isNaN(val);
  87. };
  88. /**
  89. * Is this value an integer?
  90. * @private
  91. */
  92. const integer = function (val) {
  93. return Number.isInteger(val);
  94. };
  95. /**
  96. * Is this value within an inclusive given range?
  97. * @private
  98. */
  99. const inRange = function (val, min, max) {
  100. return val >= min && val <= max;
  101. };
  102. /**
  103. * Is this value within the elements of an array?
  104. * @private
  105. */
  106. const inArray = function (val, list) {
  107. return list.includes(val);
  108. };
  109. /**
  110. * Create an Error with a message relating to an invalid parameter.
  111. *
  112. * @param {string} name - parameter name.
  113. * @param {string} expected - description of the type/value/range expected.
  114. * @param {*} actual - the value received.
  115. * @returns {Error} Containing the formatted message.
  116. * @private
  117. */
  118. const invalidParameterError = function (name, expected, actual) {
  119. return new Error(
  120. `Expected ${expected} for ${name} but received ${actual} of type ${typeof actual}`
  121. );
  122. };
  123. module.exports = {
  124. defined: defined,
  125. object: object,
  126. plainObject: plainObject,
  127. fn: fn,
  128. bool: bool,
  129. buffer: buffer,
  130. typedArray: typedArray,
  131. arrayBuffer: arrayBuffer,
  132. string: string,
  133. number: number,
  134. integer: integer,
  135. inRange: inRange,
  136. inArray: inArray,
  137. invalidParameterError: invalidParameterError
  138. };