exports.mjs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // src/helpers.ts
  2. var objectTypes = [
  3. "Array",
  4. "ArrayBuffer",
  5. "AsyncFunction",
  6. "AsyncGenerator",
  7. "AsyncGeneratorFunction",
  8. "Date",
  9. "Error",
  10. "Function",
  11. "Generator",
  12. "GeneratorFunction",
  13. "HTMLElement",
  14. "Map",
  15. "Object",
  16. "Promise",
  17. "RegExp",
  18. "Set",
  19. "WeakMap",
  20. "WeakSet"
  21. ];
  22. var primitiveTypes = [
  23. "bigint",
  24. "boolean",
  25. "null",
  26. "number",
  27. "string",
  28. "symbol",
  29. "undefined"
  30. ];
  31. function getObjectType(value) {
  32. const objectTypeName = Object.prototype.toString.call(value).slice(8, -1);
  33. if (/HTML\w+Element/.test(objectTypeName)) {
  34. return "HTMLElement";
  35. }
  36. if (isObjectType(objectTypeName)) {
  37. return objectTypeName;
  38. }
  39. return void 0;
  40. }
  41. function isObjectOfType(type) {
  42. return (value) => getObjectType(value) === type;
  43. }
  44. function isObjectType(name) {
  45. return objectTypes.includes(name);
  46. }
  47. function isOfType(type) {
  48. return (value) => typeof value === type;
  49. }
  50. function isPrimitiveType(name) {
  51. return primitiveTypes.includes(name);
  52. }
  53. // src/index.ts
  54. var DOM_PROPERTIES_TO_CHECK = [
  55. "innerHTML",
  56. "ownerDocument",
  57. "style",
  58. "attributes",
  59. "nodeValue"
  60. ];
  61. function is(value) {
  62. if (value === null) {
  63. return "null";
  64. }
  65. switch (typeof value) {
  66. case "bigint":
  67. return "bigint";
  68. case "boolean":
  69. return "boolean";
  70. case "number":
  71. return "number";
  72. case "string":
  73. return "string";
  74. case "symbol":
  75. return "symbol";
  76. case "undefined":
  77. return "undefined";
  78. default:
  79. }
  80. if (is.array(value)) {
  81. return "Array";
  82. }
  83. if (is.plainFunction(value)) {
  84. return "Function";
  85. }
  86. const tagType = getObjectType(value);
  87. if (tagType) {
  88. return tagType;
  89. }
  90. return "Object";
  91. }
  92. is.array = Array.isArray;
  93. is.arrayOf = (target, predicate) => {
  94. if (!is.array(target) && !is.function(predicate)) {
  95. return false;
  96. }
  97. return target.every((d) => predicate(d));
  98. };
  99. is.asyncGeneratorFunction = (value) => getObjectType(value) === "AsyncGeneratorFunction";
  100. is.asyncFunction = isObjectOfType("AsyncFunction");
  101. is.bigint = isOfType("bigint");
  102. is.boolean = (value) => {
  103. return value === true || value === false;
  104. };
  105. is.date = isObjectOfType("Date");
  106. is.defined = (value) => !is.undefined(value);
  107. is.domElement = (value) => {
  108. return is.object(value) && !is.plainObject(value) && value.nodeType === 1 && is.string(value.nodeName) && DOM_PROPERTIES_TO_CHECK.every((property) => property in value);
  109. };
  110. is.empty = (value) => {
  111. return is.string(value) && value.length === 0 || is.array(value) && value.length === 0 || is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0 || is.set(value) && value.size === 0 || is.map(value) && value.size === 0;
  112. };
  113. is.error = isObjectOfType("Error");
  114. is.function = isOfType("function");
  115. is.generator = (value) => {
  116. return is.iterable(value) && is.function(value.next) && is.function(value.throw);
  117. };
  118. is.generatorFunction = isObjectOfType("GeneratorFunction");
  119. is.instanceOf = (instance, class_) => {
  120. if (!instance || !class_) {
  121. return false;
  122. }
  123. return Object.getPrototypeOf(instance) === class_.prototype;
  124. };
  125. is.iterable = (value) => {
  126. return !is.nullOrUndefined(value) && is.function(value[Symbol.iterator]);
  127. };
  128. is.map = isObjectOfType("Map");
  129. is.nan = (value) => {
  130. return Number.isNaN(value);
  131. };
  132. is.null = (value) => {
  133. return value === null;
  134. };
  135. is.nullOrUndefined = (value) => {
  136. return is.null(value) || is.undefined(value);
  137. };
  138. is.number = (value) => {
  139. return isOfType("number")(value) && !is.nan(value);
  140. };
  141. is.numericString = (value) => {
  142. return is.string(value) && value.length > 0 && !Number.isNaN(Number(value));
  143. };
  144. is.object = (value) => {
  145. return !is.nullOrUndefined(value) && (is.function(value) || typeof value === "object");
  146. };
  147. is.oneOf = (target, value) => {
  148. if (!is.array(target)) {
  149. return false;
  150. }
  151. return target.indexOf(value) > -1;
  152. };
  153. is.plainFunction = isObjectOfType("Function");
  154. is.plainObject = (value) => {
  155. if (getObjectType(value) !== "Object") {
  156. return false;
  157. }
  158. const prototype = Object.getPrototypeOf(value);
  159. return prototype === null || prototype === Object.getPrototypeOf({});
  160. };
  161. is.primitive = (value) => is.null(value) || isPrimitiveType(typeof value);
  162. is.promise = isObjectOfType("Promise");
  163. is.propertyOf = (target, key, predicate) => {
  164. if (!is.object(target) || !key) {
  165. return false;
  166. }
  167. const value = target[key];
  168. if (is.function(predicate)) {
  169. return predicate(value);
  170. }
  171. return is.defined(value);
  172. };
  173. is.regexp = isObjectOfType("RegExp");
  174. is.set = isObjectOfType("Set");
  175. is.string = isOfType("string");
  176. is.symbol = isOfType("symbol");
  177. is.undefined = isOfType("undefined");
  178. is.weakMap = isObjectOfType("WeakMap");
  179. is.weakSet = isObjectOfType("WeakSet");
  180. var src_default = is;
  181. // src/exports.ts
  182. var isArray = src_default.array;
  183. var isArrayOf = src_default.arrayOf;
  184. var isAsyncGeneratorFunction = src_default.asyncGeneratorFunction;
  185. var isAsyncFunction = src_default.asyncFunction;
  186. var isBigInt = src_default.bigint;
  187. var isBoolean = src_default.boolean;
  188. var isDate = src_default.date;
  189. var isDefined = src_default.defined;
  190. var isDomElement = src_default.domElement;
  191. var isEmpty = src_default.empty;
  192. var isError = src_default.error;
  193. var isFunction = src_default.function;
  194. var isGenerator = src_default.generator;
  195. var isGeneratorFunction = src_default.generatorFunction;
  196. var isInstanceOf = src_default.instanceOf;
  197. var isIterable = src_default.iterable;
  198. var isMap = src_default.map;
  199. var isNan = src_default.nan;
  200. var isNull = src_default.null;
  201. var isNullOrUndefined = src_default.nullOrUndefined;
  202. var isNumber = src_default.number;
  203. var isNumericString = src_default.numericString;
  204. var isObject = src_default.object;
  205. var isOneOf = src_default.oneOf;
  206. var isPlainFunction = src_default.plainFunction;
  207. var isPlainObject = src_default.plainObject;
  208. var isPrimitive = src_default.primitive;
  209. var isPromise = src_default.promise;
  210. var isPropertyOf = src_default.propertyOf;
  211. var isRegexp = src_default.regexp;
  212. var isSet = src_default.set;
  213. var isString = src_default.string;
  214. var isSymbol = src_default.symbol;
  215. var isUndefined = src_default.undefined;
  216. var isWeakMap = src_default.weakMap;
  217. var isWeakSet = src_default.weakSet;
  218. export {
  219. isArray,
  220. isArrayOf,
  221. isAsyncFunction,
  222. isAsyncGeneratorFunction,
  223. isBigInt,
  224. isBoolean,
  225. isDate,
  226. isDefined,
  227. isDomElement,
  228. isEmpty,
  229. isError,
  230. isFunction,
  231. isGenerator,
  232. isGeneratorFunction,
  233. isInstanceOf,
  234. isIterable,
  235. isMap,
  236. isNan,
  237. isNull,
  238. isNullOrUndefined,
  239. isNumber,
  240. isNumericString,
  241. isObject,
  242. isOneOf,
  243. isPlainFunction,
  244. isPlainObject,
  245. isPrimitive,
  246. isPromise,
  247. isPropertyOf,
  248. isRegexp,
  249. isSet,
  250. isString,
  251. isSymbol,
  252. isUndefined,
  253. isWeakMap,
  254. isWeakSet
  255. };
  256. //# sourceMappingURL=exports.mjs.map