index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const util = require("util");
  4. const toString = Object.prototype.toString;
  5. const getObjectType = (value) => toString.call(value).slice(8, -1);
  6. const isOfType = (type) => (value) => typeof value === type;
  7. const isObjectOfType = (type) => (value) => getObjectType(value) === type;
  8. function is(value) {
  9. if (value === null) {
  10. return 'null';
  11. }
  12. if (value === true || value === false) {
  13. return 'boolean';
  14. }
  15. const type = typeof value;
  16. if (type === 'undefined') {
  17. return 'undefined';
  18. }
  19. if (type === 'string') {
  20. return 'string';
  21. }
  22. if (type === 'number') {
  23. return 'number';
  24. }
  25. if (type === 'symbol') {
  26. return 'symbol';
  27. }
  28. if (is.function_(value)) {
  29. return 'Function';
  30. }
  31. if (Array.isArray(value)) {
  32. return 'Array';
  33. }
  34. if (Buffer.isBuffer(value)) {
  35. return 'Buffer';
  36. }
  37. const tagType = getObjectType(value);
  38. if (tagType) {
  39. return tagType;
  40. }
  41. if (value instanceof String || value instanceof Boolean || value instanceof Number) {
  42. throw new TypeError('Please don\'t use object wrappers for primitive types');
  43. }
  44. return 'Object';
  45. }
  46. (function (is) {
  47. const isObject = (value) => typeof value === 'object';
  48. is.undefined = isOfType('undefined');
  49. is.string = isOfType('string');
  50. is.number = isOfType('number');
  51. is.function_ = isOfType('function');
  52. is.null_ = (value) => value === null;
  53. is.class_ = (value) => is.function_(value) && value.toString().startsWith('class ');
  54. is.boolean = (value) => value === true || value === false;
  55. is.symbol = isOfType('symbol');
  56. is.array = Array.isArray;
  57. is.buffer = Buffer.isBuffer;
  58. is.nullOrUndefined = (value) => is.null_(value) || is.undefined(value);
  59. is.object = (value) => !is.nullOrUndefined(value) && (is.function_(value) || isObject(value));
  60. is.iterable = (value) => !is.nullOrUndefined(value) && is.function_(value[Symbol.iterator]);
  61. is.generator = (value) => is.iterable(value) && is.function_(value.next) && is.function_(value.throw);
  62. is.nativePromise = isObjectOfType('Promise');
  63. const hasPromiseAPI = (value) => !is.null_(value) &&
  64. isObject(value) &&
  65. is.function_(value.then) &&
  66. is.function_(value.catch);
  67. is.promise = (value) => is.nativePromise(value) || hasPromiseAPI(value);
  68. const isFunctionOfType = (type) => (value) => is.function_(value) && is.function_(value.constructor) && value.constructor.name === type;
  69. is.generatorFunction = isFunctionOfType('GeneratorFunction');
  70. is.asyncFunction = isFunctionOfType('AsyncFunction');
  71. is.regExp = isObjectOfType('RegExp');
  72. is.date = isObjectOfType('Date');
  73. is.error = isObjectOfType('Error');
  74. is.map = isObjectOfType('Map');
  75. is.set = isObjectOfType('Set');
  76. is.weakMap = isObjectOfType('WeakMap');
  77. is.weakSet = isObjectOfType('WeakSet');
  78. is.int8Array = isObjectOfType('Int8Array');
  79. is.uint8Array = isObjectOfType('Uint8Array');
  80. is.uint8ClampedArray = isObjectOfType('Uint8ClampedArray');
  81. is.int16Array = isObjectOfType('Int16Array');
  82. is.uint16Array = isObjectOfType('Uint16Array');
  83. is.int32Array = isObjectOfType('Int32Array');
  84. is.uint32Array = isObjectOfType('Uint32Array');
  85. is.float32Array = isObjectOfType('Float32Array');
  86. is.float64Array = isObjectOfType('Float64Array');
  87. is.arrayBuffer = isObjectOfType('ArrayBuffer');
  88. is.sharedArrayBuffer = isObjectOfType('SharedArrayBuffer');
  89. is.truthy = (value) => Boolean(value);
  90. is.falsy = (value) => !value;
  91. is.nan = (value) => Number.isNaN(value);
  92. const primitiveTypes = new Set([
  93. 'undefined',
  94. 'string',
  95. 'number',
  96. 'boolean',
  97. 'symbol'
  98. ]);
  99. is.primitive = (value) => is.null_(value) || primitiveTypes.has(typeof value);
  100. is.integer = (value) => Number.isInteger(value);
  101. is.safeInteger = (value) => Number.isSafeInteger(value);
  102. is.plainObject = (value) => {
  103. let prototype;
  104. return getObjectType(value) === 'Object' &&
  105. (prototype = Object.getPrototypeOf(value), prototype === null ||
  106. prototype === Object.getPrototypeOf({}));
  107. };
  108. const typedArrayTypes = new Set([
  109. 'Int8Array',
  110. 'Uint8Array',
  111. 'Uint8ClampedArray',
  112. 'Int16Array',
  113. 'Uint16Array',
  114. 'Int32Array',
  115. 'Uint32Array',
  116. 'Float32Array',
  117. 'Float64Array'
  118. ]);
  119. is.typedArray = (value) => typedArrayTypes.has(getObjectType(value));
  120. const isValidLength = (value) => is.safeInteger(value) && value > -1;
  121. is.arrayLike = (value) => !is.nullOrUndefined(value) && !is.function_(value) && isValidLength(value.length);
  122. is.inRange = (value, range) => {
  123. if (is.number(range)) {
  124. return value >= Math.min(0, range) && value <= Math.max(range, 0);
  125. }
  126. if (is.array(range) && range.length === 2) {
  127. return value >= Math.min.apply(null, range) && value <= Math.max.apply(null, range);
  128. }
  129. throw new TypeError(`Invalid range: ${util.inspect(range)}`);
  130. };
  131. const NODE_TYPE_ELEMENT = 1;
  132. const DOM_PROPERTIES_TO_CHECK = [
  133. 'innerHTML',
  134. 'ownerDocument',
  135. 'style',
  136. 'attributes',
  137. 'nodeValue'
  138. ];
  139. is.domElement = (value) => is.object(value) && value.nodeType === NODE_TYPE_ELEMENT && is.string(value.nodeName) &&
  140. !is.plainObject(value) && DOM_PROPERTIES_TO_CHECK.every(property => property in value);
  141. is.infinite = (value) => value === Infinity || value === -Infinity;
  142. const isAbsoluteMod2 = (value) => (rem) => is.integer(rem) && Math.abs(rem % 2) === value;
  143. is.even = isAbsoluteMod2(0);
  144. is.odd = isAbsoluteMod2(1);
  145. const isWhiteSpaceString = (value) => is.string(value) && /\S/.test(value) === false;
  146. const isEmptyStringOrArray = (value) => (is.string(value) || is.array(value)) && value.length === 0;
  147. const isEmptyObject = (value) => !is.map(value) && !is.set(value) && is.object(value) && Object.keys(value).length === 0;
  148. const isEmptyMapOrSet = (value) => (is.map(value) || is.set(value)) && value.size === 0;
  149. is.empty = (value) => is.falsy(value) || isEmptyStringOrArray(value) || isEmptyObject(value) || isEmptyMapOrSet(value);
  150. is.emptyOrWhitespace = (value) => is.empty(value) || isWhiteSpaceString(value);
  151. const predicateOnArray = (method, predicate, args) => {
  152. const values = Array.prototype.slice.call(args, 1);
  153. if (is.function_(predicate) === false) {
  154. throw new TypeError(`Invalid predicate: ${util.inspect(predicate)}`);
  155. }
  156. if (values.length === 0) {
  157. throw new TypeError('Invalid number of values');
  158. }
  159. return method.call(values, predicate);
  160. };
  161. function any(predicate) {
  162. return predicateOnArray(Array.prototype.some, predicate, arguments);
  163. }
  164. is.any = any;
  165. function all(predicate) {
  166. return predicateOnArray(Array.prototype.every, predicate, arguments);
  167. }
  168. is.all = all;
  169. })(is || (is = {}));
  170. Object.defineProperties(is, {
  171. class: {
  172. value: is.class_
  173. },
  174. function: {
  175. value: is.function_
  176. },
  177. null: {
  178. value: is.null_
  179. }
  180. });
  181. exports.default = is;
  182. //# sourceMappingURL=index.js.map