index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. var utils = require('core-util-is');
  3. var isStearm = require('isstream');
  4. // wait for https://github.com/miguelmota/is-class/pull/6 merge
  5. var isClass = require('is-class-hotfix');
  6. /**
  7. * Expose all methods in core-util-is
  8. */
  9. Object.keys(utils).map(function (name) {
  10. exports[transform(name)] = utils[name];
  11. });
  12. /**
  13. * Stream detected by isstream
  14. */
  15. exports.stream = isStearm;
  16. exports.readableStream = isStearm.isReadable;
  17. exports.writableStream = isStearm.isWritable;
  18. exports.duplexStream = isStearm.isDuplex;
  19. /**
  20. * Class detected by is-class
  21. */
  22. exports.class = isClass;
  23. /**
  24. * Extend method
  25. */
  26. exports.finite = Number.isFinite;
  27. exports.NaN = Number.isNaN
  28. exports.generator = function (obj) {
  29. return obj
  30. && 'function' === typeof obj.next
  31. && 'function' === typeof obj.throw;
  32. };
  33. exports.generatorFunction = function (obj) {
  34. return obj
  35. && obj.constructor
  36. && 'GeneratorFunction' === obj.constructor.name;
  37. };
  38. exports.asyncFunction = function (obj) {
  39. return obj
  40. && obj.constructor
  41. && 'AsyncFunction' === obj.constructor.name;
  42. };
  43. exports.promise = function (obj) {
  44. return obj
  45. && 'function' === typeof obj.then;
  46. };
  47. var MAX_INT_31 = Math.pow(2, 31);
  48. exports.int = function (obj) {
  49. return utils.isNumber(obj)
  50. && obj % 1 === 0;
  51. };
  52. exports.int32 = function (obj) {
  53. return exports.int(obj)
  54. && obj < MAX_INT_31
  55. && obj >= -MAX_INT_31;
  56. };
  57. exports.long = function (obj) {
  58. return exports.int(obj)
  59. && (obj >= MAX_INT_31 || obj < -MAX_INT_31);
  60. };
  61. exports.Long = function (obj) {
  62. return exports.object(obj)
  63. && exports.number(obj.high)
  64. && exports.number(obj.low);
  65. };
  66. exports.double = function (obj) {
  67. return utils.isNumber(obj)
  68. && !isNaN(obj)
  69. && obj % 1 !== 0;
  70. };
  71. /**
  72. * override core-util-is
  73. */
  74. exports.date = function isDate(obj) {
  75. return obj instanceof Date;
  76. };
  77. exports.regExp = function isRegExp(obj) {
  78. return obj instanceof RegExp;
  79. };
  80. exports.regexp = exports.regExp;
  81. exports.error = function isError(obj) {
  82. return obj instanceof Error;
  83. };
  84. exports.array = Array.isArray;
  85. /**
  86. * transform isNull type to null
  87. * @param {[type]} m [description]
  88. * @return {[type]} [description]
  89. */
  90. function transform(m) {
  91. var name = m.slice(2);
  92. name = name[0].toLowerCase() + name.slice(1);
  93. return name;
  94. }