is.js 2.9 KB

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