You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const util = require("util");
  4. const toString = Object.prototype.toString;
  5. const isOfType = (type) => (value) => typeof value === type; // tslint:disable-line:strict-type-predicates
  6. const getObjectType = (value) => {
  7. const objectName = toString.call(value).slice(8, -1);
  8. if (objectName) {
  9. return objectName;
  10. }
  11. return null;
  12. };
  13. const isObjectOfType = (typeName) => (value) => {
  14. return getObjectType(value) === typeName;
  15. };
  16. function is(value) {
  17. if (value === null) {
  18. return "null" /* null */;
  19. }
  20. if (value === true || value === false) {
  21. return "boolean" /* boolean */;
  22. }
  23. const type = typeof value;
  24. if (type === 'undefined') {
  25. return "undefined" /* undefined */;
  26. }
  27. if (type === 'string') {
  28. return "string" /* string */;
  29. }
  30. if (type === 'number') {
  31. return "number" /* number */;
  32. }
  33. if (type === 'symbol') {
  34. return "symbol" /* symbol */;
  35. }
  36. if (is.function_(value)) {
  37. return "Function" /* Function */;
  38. }
  39. if (Array.isArray(value)) {
  40. return "Array" /* Array */;
  41. }
  42. if (Buffer.isBuffer(value)) {
  43. return "Buffer" /* Buffer */;
  44. }
  45. const tagType = getObjectType(value);
  46. if (tagType) {
  47. return tagType;
  48. }
  49. if (value instanceof String || value instanceof Boolean || value instanceof Number) {
  50. throw new TypeError('Please don\'t use object wrappers for primitive types');
  51. }
  52. return "Object" /* Object */;
  53. }
  54. (function (is) {
  55. const isObject = (value) => typeof value === 'object';
  56. // tslint:disable:variable-name
  57. is.undefined = isOfType('undefined');
  58. is.string = isOfType('string');
  59. is.number = isOfType('number');
  60. is.function_ = isOfType('function');
  61. is.null_ = (value) => value === null;
  62. is.class_ = (value) => is.function_(value) && value.toString().startsWith('class ');
  63. is.boolean = (value) => value === true || value === false;
  64. // tslint:enable:variable-name
  65. is.symbol = isOfType('symbol');
  66. is.array = Array.isArray;
  67. is.buffer = Buffer.isBuffer;
  68. is.nullOrUndefined = (value) => is.null_(value) || is.undefined(value);
  69. is.object = (value) => !is.nullOrUndefined(value) && (is.function_(value) || isObject(value));
  70. is.iterable = (value) => !is.nullOrUndefined(value) && is.function_(value[Symbol.iterator]);
  71. is.generator = (value) => is.iterable(value) && is.function_(value.next) && is.function_(value.throw);
  72. is.nativePromise = isObjectOfType("Promise" /* Promise */);
  73. const hasPromiseAPI = (value) => !is.null_(value) &&
  74. isObject(value) &&
  75. is.function_(value.then) &&
  76. is.function_(value.catch);
  77. is.promise = (value) => is.nativePromise(value) || hasPromiseAPI(value);
  78. // TODO: Change to use `isObjectOfType` once Node.js 6 or higher is targeted
  79. const isFunctionOfType = (type) => (value) => is.function_(value) && is.function_(value.constructor) && value.constructor.name === type;
  80. is.generatorFunction = isFunctionOfType('GeneratorFunction');
  81. is.asyncFunction = isFunctionOfType('AsyncFunction');
  82. is.boundFunction = (value) => is.function_(value) && !value.hasOwnProperty('prototype');
  83. is.regExp = isObjectOfType("RegExp" /* RegExp */);
  84. is.date = isObjectOfType("Date" /* Date */);
  85. is.error = isObjectOfType("Error" /* Error */);
  86. is.map = isObjectOfType("Map" /* Map */);
  87. is.set = isObjectOfType("Set" /* Set */);
  88. is.weakMap = isObjectOfType("WeakMap" /* WeakMap */);
  89. is.weakSet = isObjectOfType("WeakSet" /* WeakSet */);
  90. is.int8Array = isObjectOfType("Int8Array" /* Int8Array */);
  91. is.uint8Array = isObjectOfType("Uint8Array" /* Uint8Array */);
  92. is.uint8ClampedArray = isObjectOfType("Uint8ClampedArray" /* Uint8ClampedArray */);
  93. is.int16Array = isObjectOfType("Int16Array" /* Int16Array */);
  94. is.uint16Array = isObjectOfType("Uint16Array" /* Uint16Array */);
  95. is.int32Array = isObjectOfType("Int32Array" /* Int32Array */);
  96. is.uint32Array = isObjectOfType("Uint32Array" /* Uint32Array */);
  97. is.float32Array = isObjectOfType("Float32Array" /* Float32Array */);
  98. is.float64Array = isObjectOfType("Float64Array" /* Float64Array */);
  99. is.arrayBuffer = isObjectOfType("ArrayBuffer" /* ArrayBuffer */);
  100. is.sharedArrayBuffer = isObjectOfType("SharedArrayBuffer" /* SharedArrayBuffer */);
  101. is.dataView = isObjectOfType("DataView" /* DataView */);
  102. // TODO: Remove `object` checks when targeting ES2015 or higher
  103. // See `Notes`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
  104. is.directInstanceOf = (instance, klass) => is.object(instance) && is.object(klass) && Object.getPrototypeOf(instance) === klass.prototype;
  105. is.truthy = (value) => Boolean(value);
  106. is.falsy = (value) => !value;
  107. is.nan = (value) => Number.isNaN(value);
  108. const primitiveTypes = new Set([
  109. 'undefined',
  110. 'string',
  111. 'number',
  112. 'boolean',
  113. 'symbol'
  114. ]);
  115. is.primitive = (value) => is.null_(value) || primitiveTypes.has(typeof value);
  116. is.integer = (value) => Number.isInteger(value);
  117. is.safeInteger = (value) => Number.isSafeInteger(value);
  118. is.plainObject = (value) => {
  119. // From: https://github.com/sindresorhus/is-plain-obj/blob/master/index.js
  120. let prototype;
  121. return getObjectType(value) === "Object" /* Object */ &&
  122. (prototype = Object.getPrototypeOf(value), prototype === null || // tslint:disable-line:ban-comma-operator
  123. prototype === Object.getPrototypeOf({}));
  124. };
  125. const typedArrayTypes = new Set([
  126. "Int8Array" /* Int8Array */,
  127. "Uint8Array" /* Uint8Array */,
  128. "Uint8ClampedArray" /* Uint8ClampedArray */,
  129. "Int16Array" /* Int16Array */,
  130. "Uint16Array" /* Uint16Array */,
  131. "Int32Array" /* Int32Array */,
  132. "Uint32Array" /* Uint32Array */,
  133. "Float32Array" /* Float32Array */,
  134. "Float64Array" /* Float64Array */
  135. ]);
  136. is.typedArray = (value) => {
  137. const objectType = getObjectType(value);
  138. if (objectType === null) {
  139. return false;
  140. }
  141. return typedArrayTypes.has(objectType);
  142. };
  143. const isValidLength = (value) => is.safeInteger(value) && value > -1;
  144. is.arrayLike = (value) => !is.nullOrUndefined(value) && !is.function_(value) && isValidLength(value.length);
  145. is.inRange = (value, range) => {
  146. if (is.number(range)) {
  147. return value >= Math.min(0, range) && value <= Math.max(range, 0);
  148. }
  149. if (is.array(range) && range.length === 2) {
  150. // TODO: Use spread operator here when targeting Node.js 6 or higher
  151. return value >= Math.min.apply(null, range) && value <= Math.max.apply(null, range);
  152. }
  153. throw new TypeError(`Invalid range: ${util.inspect(range)}`);
  154. };
  155. const NODE_TYPE_ELEMENT = 1;
  156. const DOM_PROPERTIES_TO_CHECK = [
  157. 'innerHTML',
  158. 'ownerDocument',
  159. 'style',
  160. 'attributes',
  161. 'nodeValue'
  162. ];
  163. is.domElement = (value) => is.object(value) && value.nodeType === NODE_TYPE_ELEMENT && is.string(value.nodeName) &&
  164. !is.plainObject(value) && DOM_PROPERTIES_TO_CHECK.every(property => property in value);
  165. is.nodeStream = (value) => !is.nullOrUndefined(value) && isObject(value) && is.function_(value.pipe);
  166. is.infinite = (value) => value === Infinity || value === -Infinity;
  167. const isAbsoluteMod2 = (value) => (rem) => is.integer(rem) && Math.abs(rem % 2) === value;
  168. is.even = isAbsoluteMod2(0);
  169. is.odd = isAbsoluteMod2(1);
  170. const isWhiteSpaceString = (value) => is.string(value) && /\S/.test(value) === false;
  171. const isEmptyStringOrArray = (value) => (is.string(value) || is.array(value)) && value.length === 0;
  172. const isEmptyObject = (value) => !is.map(value) && !is.set(value) && is.object(value) && Object.keys(value).length === 0;
  173. const isEmptyMapOrSet = (value) => (is.map(value) || is.set(value)) && value.size === 0;
  174. is.empty = (value) => is.falsy(value) || isEmptyStringOrArray(value) || isEmptyObject(value) || isEmptyMapOrSet(value);
  175. is.emptyOrWhitespace = (value) => is.empty(value) || isWhiteSpaceString(value);
  176. const predicateOnArray = (method, predicate, args) => {
  177. // `args` is the calling function's "arguments object".
  178. // We have to do it this way to keep node v4 support.
  179. // So here we convert it to an array and slice off the first item.
  180. const values = Array.prototype.slice.call(args, 1);
  181. if (is.function_(predicate) === false) {
  182. throw new TypeError(`Invalid predicate: ${util.inspect(predicate)}`);
  183. }
  184. if (values.length === 0) {
  185. throw new TypeError('Invalid number of values');
  186. }
  187. return method.call(values, predicate);
  188. };
  189. function any(predicate) {
  190. return predicateOnArray(Array.prototype.some, predicate, arguments);
  191. }
  192. is.any = any;
  193. function all(predicate) {
  194. return predicateOnArray(Array.prototype.every, predicate, arguments);
  195. }
  196. is.all = all;
  197. // tslint:enable:only-arrow-functions no-function-expression
  198. })(is || (is = {}));
  199. // Some few keywords are reserved, but we'll populate them for Node.js users
  200. // See https://github.com/Microsoft/TypeScript/issues/2536
  201. Object.defineProperties(is, {
  202. class: {
  203. value: is.class_
  204. },
  205. function: {
  206. value: is.function_
  207. },
  208. null: {
  209. value: is.null_
  210. }
  211. });
  212. exports.default = is;
  213. // For CommonJS default export support
  214. module.exports = is;
  215. module.exports.default = is;