index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. "use strict";
  2. /// <reference lib="es2016"/>
  3. /// <reference lib="es2017.sharedmemory"/>
  4. /// <reference lib="esnext.asynciterable"/>
  5. /// <reference lib="dom"/>
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. // TODO: Use the `URL` global when targeting Node.js 10
  8. // tslint:disable-next-line
  9. const URLGlobal = typeof URL === 'undefined' ? require('url').URL : URL;
  10. const toString = Object.prototype.toString;
  11. const isOfType = (type) => (value) => typeof value === type;
  12. const isBuffer = (input) => !is.nullOrUndefined(input) && !is.nullOrUndefined(input.constructor) && is.function_(input.constructor.isBuffer) && input.constructor.isBuffer(input);
  13. const getObjectType = (value) => {
  14. const objectName = toString.call(value).slice(8, -1);
  15. if (objectName) {
  16. return objectName;
  17. }
  18. return null;
  19. };
  20. const isObjectOfType = (type) => (value) => getObjectType(value) === type;
  21. function is(value) {
  22. switch (value) {
  23. case null:
  24. return "null" /* null */;
  25. case true:
  26. case false:
  27. return "boolean" /* boolean */;
  28. default:
  29. }
  30. switch (typeof value) {
  31. case 'undefined':
  32. return "undefined" /* undefined */;
  33. case 'string':
  34. return "string" /* string */;
  35. case 'number':
  36. return "number" /* number */;
  37. case 'symbol':
  38. return "symbol" /* symbol */;
  39. default:
  40. }
  41. if (is.function_(value)) {
  42. return "Function" /* Function */;
  43. }
  44. if (is.observable(value)) {
  45. return "Observable" /* Observable */;
  46. }
  47. if (Array.isArray(value)) {
  48. return "Array" /* Array */;
  49. }
  50. if (isBuffer(value)) {
  51. return "Buffer" /* Buffer */;
  52. }
  53. const tagType = getObjectType(value);
  54. if (tagType) {
  55. return tagType;
  56. }
  57. if (value instanceof String || value instanceof Boolean || value instanceof Number) {
  58. throw new TypeError('Please don\'t use object wrappers for primitive types');
  59. }
  60. return "Object" /* Object */;
  61. }
  62. (function (is) {
  63. // tslint:disable-next-line:strict-type-predicates
  64. const isObject = (value) => typeof value === 'object';
  65. // tslint:disable:variable-name
  66. is.undefined = isOfType('undefined');
  67. is.string = isOfType('string');
  68. is.number = isOfType('number');
  69. is.function_ = isOfType('function');
  70. // tslint:disable-next-line:strict-type-predicates
  71. is.null_ = (value) => value === null;
  72. is.class_ = (value) => is.function_(value) && value.toString().startsWith('class ');
  73. is.boolean = (value) => value === true || value === false;
  74. is.symbol = isOfType('symbol');
  75. // tslint:enable:variable-name
  76. is.numericString = (value) => is.string(value) && value.length > 0 && !Number.isNaN(Number(value));
  77. is.array = Array.isArray;
  78. is.buffer = isBuffer;
  79. is.nullOrUndefined = (value) => is.null_(value) || is.undefined(value);
  80. is.object = (value) => !is.nullOrUndefined(value) && (is.function_(value) || isObject(value));
  81. is.iterable = (value) => !is.nullOrUndefined(value) && is.function_(value[Symbol.iterator]);
  82. is.asyncIterable = (value) => !is.nullOrUndefined(value) && is.function_(value[Symbol.asyncIterator]);
  83. is.generator = (value) => is.iterable(value) && is.function_(value.next) && is.function_(value.throw);
  84. is.nativePromise = (value) => isObjectOfType("Promise" /* Promise */)(value);
  85. const hasPromiseAPI = (value) => !is.null_(value) &&
  86. isObject(value) &&
  87. is.function_(value.then) &&
  88. is.function_(value.catch);
  89. is.promise = (value) => is.nativePromise(value) || hasPromiseAPI(value);
  90. is.generatorFunction = isObjectOfType("GeneratorFunction" /* GeneratorFunction */);
  91. is.asyncFunction = isObjectOfType("AsyncFunction" /* AsyncFunction */);
  92. is.boundFunction = (value) => is.function_(value) && !value.hasOwnProperty('prototype');
  93. is.regExp = isObjectOfType("RegExp" /* RegExp */);
  94. is.date = isObjectOfType("Date" /* Date */);
  95. is.error = isObjectOfType("Error" /* Error */);
  96. is.map = (value) => isObjectOfType("Map" /* Map */)(value);
  97. is.set = (value) => isObjectOfType("Set" /* Set */)(value);
  98. is.weakMap = (value) => isObjectOfType("WeakMap" /* WeakMap */)(value);
  99. is.weakSet = (value) => isObjectOfType("WeakSet" /* WeakSet */)(value);
  100. is.int8Array = isObjectOfType("Int8Array" /* Int8Array */);
  101. is.uint8Array = isObjectOfType("Uint8Array" /* Uint8Array */);
  102. is.uint8ClampedArray = isObjectOfType("Uint8ClampedArray" /* Uint8ClampedArray */);
  103. is.int16Array = isObjectOfType("Int16Array" /* Int16Array */);
  104. is.uint16Array = isObjectOfType("Uint16Array" /* Uint16Array */);
  105. is.int32Array = isObjectOfType("Int32Array" /* Int32Array */);
  106. is.uint32Array = isObjectOfType("Uint32Array" /* Uint32Array */);
  107. is.float32Array = isObjectOfType("Float32Array" /* Float32Array */);
  108. is.float64Array = isObjectOfType("Float64Array" /* Float64Array */);
  109. is.arrayBuffer = isObjectOfType("ArrayBuffer" /* ArrayBuffer */);
  110. is.sharedArrayBuffer = isObjectOfType("SharedArrayBuffer" /* SharedArrayBuffer */);
  111. is.dataView = isObjectOfType("DataView" /* DataView */);
  112. is.directInstanceOf = (instance, klass) => Object.getPrototypeOf(instance) === klass.prototype;
  113. is.urlInstance = (value) => isObjectOfType("URL" /* URL */)(value);
  114. is.urlString = (value) => {
  115. if (!is.string(value)) {
  116. return false;
  117. }
  118. try {
  119. new URLGlobal(value); // tslint:disable-line no-unused-expression
  120. return true;
  121. }
  122. catch (_a) {
  123. return false;
  124. }
  125. };
  126. is.truthy = (value) => Boolean(value);
  127. is.falsy = (value) => !value;
  128. is.nan = (value) => Number.isNaN(value);
  129. const primitiveTypes = new Set([
  130. 'undefined',
  131. 'string',
  132. 'number',
  133. 'boolean',
  134. 'symbol'
  135. ]);
  136. is.primitive = (value) => is.null_(value) || primitiveTypes.has(typeof value);
  137. is.integer = (value) => Number.isInteger(value);
  138. is.safeInteger = (value) => Number.isSafeInteger(value);
  139. is.plainObject = (value) => {
  140. // From: https://github.com/sindresorhus/is-plain-obj/blob/master/index.js
  141. let prototype;
  142. return getObjectType(value) === "Object" /* Object */ &&
  143. (prototype = Object.getPrototypeOf(value), prototype === null || // tslint:disable-line:ban-comma-operator
  144. prototype === Object.getPrototypeOf({}));
  145. };
  146. const typedArrayTypes = new Set([
  147. "Int8Array" /* Int8Array */,
  148. "Uint8Array" /* Uint8Array */,
  149. "Uint8ClampedArray" /* Uint8ClampedArray */,
  150. "Int16Array" /* Int16Array */,
  151. "Uint16Array" /* Uint16Array */,
  152. "Int32Array" /* Int32Array */,
  153. "Uint32Array" /* Uint32Array */,
  154. "Float32Array" /* Float32Array */,
  155. "Float64Array" /* Float64Array */
  156. ]);
  157. is.typedArray = (value) => {
  158. const objectType = getObjectType(value);
  159. if (objectType === null) {
  160. return false;
  161. }
  162. return typedArrayTypes.has(objectType);
  163. };
  164. const isValidLength = (value) => is.safeInteger(value) && value > -1;
  165. is.arrayLike = (value) => !is.nullOrUndefined(value) && !is.function_(value) && isValidLength(value.length);
  166. is.inRange = (value, range) => {
  167. if (is.number(range)) {
  168. return value >= Math.min(0, range) && value <= Math.max(range, 0);
  169. }
  170. if (is.array(range) && range.length === 2) {
  171. return value >= Math.min(...range) && value <= Math.max(...range);
  172. }
  173. throw new TypeError(`Invalid range: ${JSON.stringify(range)}`);
  174. };
  175. const NODE_TYPE_ELEMENT = 1;
  176. const DOM_PROPERTIES_TO_CHECK = [
  177. 'innerHTML',
  178. 'ownerDocument',
  179. 'style',
  180. 'attributes',
  181. 'nodeValue'
  182. ];
  183. is.domElement = (value) => is.object(value) && value.nodeType === NODE_TYPE_ELEMENT && is.string(value.nodeName) &&
  184. !is.plainObject(value) && DOM_PROPERTIES_TO_CHECK.every(property => property in value);
  185. is.observable = (value) => {
  186. if (!value) {
  187. return false;
  188. }
  189. if (value[Symbol.observable] && value === value[Symbol.observable]()) {
  190. return true;
  191. }
  192. if (value['@@observable'] && value === value['@@observable']()) {
  193. return true;
  194. }
  195. return false;
  196. };
  197. is.nodeStream = (value) => !is.nullOrUndefined(value) && isObject(value) && is.function_(value.pipe) && !is.observable(value);
  198. is.infinite = (value) => value === Infinity || value === -Infinity;
  199. const isAbsoluteMod2 = (rem) => (value) => is.integer(value) && Math.abs(value % 2) === rem;
  200. is.even = isAbsoluteMod2(0);
  201. is.odd = isAbsoluteMod2(1);
  202. const isWhiteSpaceString = (value) => is.string(value) && /\S/.test(value) === false;
  203. is.emptyArray = (value) => is.array(value) && value.length === 0;
  204. is.nonEmptyArray = (value) => is.array(value) && value.length > 0;
  205. is.emptyString = (value) => is.string(value) && value.length === 0;
  206. is.nonEmptyString = (value) => is.string(value) && value.length > 0;
  207. is.emptyStringOrWhitespace = (value) => is.emptyString(value) || isWhiteSpaceString(value);
  208. is.emptyObject = (value) => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0;
  209. is.nonEmptyObject = (value) => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length > 0;
  210. is.emptySet = (value) => is.set(value) && value.size === 0;
  211. is.nonEmptySet = (value) => is.set(value) && value.size > 0;
  212. is.emptyMap = (value) => is.map(value) && value.size === 0;
  213. is.nonEmptyMap = (value) => is.map(value) && value.size > 0;
  214. const predicateOnArray = (method, predicate, values) => {
  215. if (is.function_(predicate) === false) {
  216. throw new TypeError(`Invalid predicate: ${JSON.stringify(predicate)}`);
  217. }
  218. if (values.length === 0) {
  219. throw new TypeError('Invalid number of values');
  220. }
  221. return method.call(values, predicate);
  222. };
  223. // tslint:disable variable-name
  224. is.any = (predicate, ...values) => predicateOnArray(Array.prototype.some, predicate, values);
  225. is.all = (predicate, ...values) => predicateOnArray(Array.prototype.every, predicate, values);
  226. // tslint:enable variable-name
  227. })(is || (is = {}));
  228. // Some few keywords are reserved, but we'll populate them for Node.js users
  229. // See https://github.com/Microsoft/TypeScript/issues/2536
  230. Object.defineProperties(is, {
  231. class: {
  232. value: is.class_
  233. },
  234. function: {
  235. value: is.function_
  236. },
  237. null: {
  238. value: is.null_
  239. }
  240. });
  241. exports.default = is;
  242. // For CommonJS default export support
  243. module.exports = is;
  244. module.exports.default = is;
  245. //# sourceMappingURL=index.js.map