util.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. // NOTE: These type checking functions intentionally don't use `instanceof`
  22. // because it is fragile and can be easily faked with `Object.create()`.
  23. function isArray(arg) {
  24. if (Array.isArray) {
  25. return Array.isArray(arg);
  26. }
  27. return objectToString(arg) === '[object Array]';
  28. }
  29. exports.isArray = isArray;
  30. function isBoolean(arg) {
  31. return typeof arg === 'boolean';
  32. }
  33. exports.isBoolean = isBoolean;
  34. function isNull(arg) {
  35. return arg === null;
  36. }
  37. exports.isNull = isNull;
  38. function isNullOrUndefined(arg) {
  39. return arg == null;
  40. }
  41. exports.isNullOrUndefined = isNullOrUndefined;
  42. function isNumber(arg) {
  43. return typeof arg === 'number';
  44. }
  45. exports.isNumber = isNumber;
  46. function isString(arg) {
  47. return typeof arg === 'string';
  48. }
  49. exports.isString = isString;
  50. function isSymbol(arg) {
  51. return typeof arg === 'symbol';
  52. }
  53. exports.isSymbol = isSymbol;
  54. function isUndefined(arg) {
  55. return arg === void 0;
  56. }
  57. exports.isUndefined = isUndefined;
  58. function isRegExp(re) {
  59. return objectToString(re) === '[object RegExp]';
  60. }
  61. exports.isRegExp = isRegExp;
  62. function isObject(arg) {
  63. return typeof arg === 'object' && arg !== null;
  64. }
  65. exports.isObject = isObject;
  66. function isDate(d) {
  67. return objectToString(d) === '[object Date]';
  68. }
  69. exports.isDate = isDate;
  70. function isError(e) {
  71. return (objectToString(e) === '[object Error]' || e instanceof Error);
  72. }
  73. exports.isError = isError;
  74. function isFunction(arg) {
  75. return typeof arg === 'function';
  76. }
  77. exports.isFunction = isFunction;
  78. function isPrimitive(arg) {
  79. return arg === null ||
  80. typeof arg === 'boolean' ||
  81. typeof arg === 'number' ||
  82. typeof arg === 'string' ||
  83. typeof arg === 'symbol' || // ES6 symbol
  84. typeof arg === 'undefined';
  85. }
  86. exports.isPrimitive = isPrimitive;
  87. exports.isBuffer = require('buffer').Buffer.isBuffer;
  88. function objectToString(o) {
  89. return Object.prototype.toString.call(o);
  90. }