is.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. /**
  3. * Is this value defined and not null?
  4. * @private
  5. */
  6. const defined = function (val) {
  7. return typeof val !== 'undefined' && val !== null;
  8. };
  9. /**
  10. * Is this value an object?
  11. * @private
  12. */
  13. const object = function (val) {
  14. return typeof val === 'object';
  15. };
  16. /**
  17. * Is this value a plain object?
  18. * @private
  19. */
  20. const plainObject = function (val) {
  21. return object(val) && Object.prototype.toString.call(val) === '[object Object]';
  22. };
  23. /**
  24. * Is this value a function?
  25. * @private
  26. */
  27. const fn = function (val) {
  28. return typeof val === 'function';
  29. };
  30. /**
  31. * Is this value a boolean?
  32. * @private
  33. */
  34. const bool = function (val) {
  35. return typeof val === 'boolean';
  36. };
  37. /**
  38. * Is this value a Buffer object?
  39. * @private
  40. */
  41. const buffer = function (val) {
  42. return object(val) && val instanceof Buffer;
  43. };
  44. /**
  45. * Is this value a non-empty string?
  46. * @private
  47. */
  48. const string = function (val) {
  49. return typeof val === 'string' && val.length > 0;
  50. };
  51. /**
  52. * Is this value a real number?
  53. * @private
  54. */
  55. const number = function (val) {
  56. return typeof val === 'number' && !Number.isNaN(val);
  57. };
  58. /**
  59. * Is this value an integer?
  60. * @private
  61. */
  62. const integer = function (val) {
  63. return number(val) && val % 1 === 0;
  64. };
  65. /**
  66. * Is this value within an inclusive given range?
  67. * @private
  68. */
  69. const inRange = function (val, min, max) {
  70. return val >= min && val <= max;
  71. };
  72. /**
  73. * Is this value within the elements of an array?
  74. * @private
  75. */
  76. const inArray = function (val, list) {
  77. return list.indexOf(val) !== -1;
  78. };
  79. /**
  80. * Create an Error with a message relating to an invalid parameter.
  81. *
  82. * @param {string} name - parameter name.
  83. * @param {string} expected - description of the type/value/range expected.
  84. * @param {*} actual - the value received.
  85. * @returns {Error} Containing the formatted message.
  86. * @private
  87. */
  88. const invalidParameterError = function (name, expected, actual) {
  89. return new Error(
  90. `Expected ${expected} for ${name} but received ${actual} of type ${typeof actual}`
  91. );
  92. };
  93. module.exports = {
  94. defined: defined,
  95. object: object,
  96. plainObject: plainObject,
  97. fn: fn,
  98. bool: bool,
  99. buffer: buffer,
  100. string: string,
  101. number: number,
  102. integer: integer,
  103. inRange: inRange,
  104. inArray: inArray,
  105. invalidParameterError: invalidParameterError
  106. };