index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. // src/index.ts
  20. var src_exports = {};
  21. __export(src_exports, {
  22. default: () => src_default
  23. });
  24. module.exports = __toCommonJS(src_exports);
  25. // src/helpers.ts
  26. var objectTypes = [
  27. "Array",
  28. "ArrayBuffer",
  29. "AsyncFunction",
  30. "AsyncGenerator",
  31. "AsyncGeneratorFunction",
  32. "Date",
  33. "Error",
  34. "Function",
  35. "Generator",
  36. "GeneratorFunction",
  37. "HTMLElement",
  38. "Map",
  39. "Object",
  40. "Promise",
  41. "RegExp",
  42. "Set",
  43. "WeakMap",
  44. "WeakSet"
  45. ];
  46. var primitiveTypes = [
  47. "bigint",
  48. "boolean",
  49. "null",
  50. "number",
  51. "string",
  52. "symbol",
  53. "undefined"
  54. ];
  55. function getObjectType(value) {
  56. const objectTypeName = Object.prototype.toString.call(value).slice(8, -1);
  57. if (/HTML\w+Element/.test(objectTypeName)) {
  58. return "HTMLElement";
  59. }
  60. if (isObjectType(objectTypeName)) {
  61. return objectTypeName;
  62. }
  63. return void 0;
  64. }
  65. function isObjectOfType(type) {
  66. return (value) => getObjectType(value) === type;
  67. }
  68. function isObjectType(name) {
  69. return objectTypes.includes(name);
  70. }
  71. function isOfType(type) {
  72. return (value) => typeof value === type;
  73. }
  74. function isPrimitiveType(name) {
  75. return primitiveTypes.includes(name);
  76. }
  77. // src/index.ts
  78. var DOM_PROPERTIES_TO_CHECK = [
  79. "innerHTML",
  80. "ownerDocument",
  81. "style",
  82. "attributes",
  83. "nodeValue"
  84. ];
  85. function is(value) {
  86. if (value === null) {
  87. return "null";
  88. }
  89. switch (typeof value) {
  90. case "bigint":
  91. return "bigint";
  92. case "boolean":
  93. return "boolean";
  94. case "number":
  95. return "number";
  96. case "string":
  97. return "string";
  98. case "symbol":
  99. return "symbol";
  100. case "undefined":
  101. return "undefined";
  102. default:
  103. }
  104. if (is.array(value)) {
  105. return "Array";
  106. }
  107. if (is.plainFunction(value)) {
  108. return "Function";
  109. }
  110. const tagType = getObjectType(value);
  111. if (tagType) {
  112. return tagType;
  113. }
  114. return "Object";
  115. }
  116. is.array = Array.isArray;
  117. is.arrayOf = (target, predicate) => {
  118. if (!is.array(target) && !is.function(predicate)) {
  119. return false;
  120. }
  121. return target.every((d) => predicate(d));
  122. };
  123. is.asyncGeneratorFunction = (value) => getObjectType(value) === "AsyncGeneratorFunction";
  124. is.asyncFunction = isObjectOfType("AsyncFunction");
  125. is.bigint = isOfType("bigint");
  126. is.boolean = (value) => {
  127. return value === true || value === false;
  128. };
  129. is.date = isObjectOfType("Date");
  130. is.defined = (value) => !is.undefined(value);
  131. is.domElement = (value) => {
  132. return is.object(value) && !is.plainObject(value) && value.nodeType === 1 && is.string(value.nodeName) && DOM_PROPERTIES_TO_CHECK.every((property) => property in value);
  133. };
  134. is.empty = (value) => {
  135. 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;
  136. };
  137. is.error = isObjectOfType("Error");
  138. is.function = isOfType("function");
  139. is.generator = (value) => {
  140. return is.iterable(value) && is.function(value.next) && is.function(value.throw);
  141. };
  142. is.generatorFunction = isObjectOfType("GeneratorFunction");
  143. is.instanceOf = (instance, class_) => {
  144. if (!instance || !class_) {
  145. return false;
  146. }
  147. return Object.getPrototypeOf(instance) === class_.prototype;
  148. };
  149. is.iterable = (value) => {
  150. return !is.nullOrUndefined(value) && is.function(value[Symbol.iterator]);
  151. };
  152. is.map = isObjectOfType("Map");
  153. is.nan = (value) => {
  154. return Number.isNaN(value);
  155. };
  156. is.null = (value) => {
  157. return value === null;
  158. };
  159. is.nullOrUndefined = (value) => {
  160. return is.null(value) || is.undefined(value);
  161. };
  162. is.number = (value) => {
  163. return isOfType("number")(value) && !is.nan(value);
  164. };
  165. is.numericString = (value) => {
  166. return is.string(value) && value.length > 0 && !Number.isNaN(Number(value));
  167. };
  168. is.object = (value) => {
  169. return !is.nullOrUndefined(value) && (is.function(value) || typeof value === "object");
  170. };
  171. is.oneOf = (target, value) => {
  172. if (!is.array(target)) {
  173. return false;
  174. }
  175. return target.indexOf(value) > -1;
  176. };
  177. is.plainFunction = isObjectOfType("Function");
  178. is.plainObject = (value) => {
  179. if (getObjectType(value) !== "Object") {
  180. return false;
  181. }
  182. const prototype = Object.getPrototypeOf(value);
  183. return prototype === null || prototype === Object.getPrototypeOf({});
  184. };
  185. is.primitive = (value) => is.null(value) || isPrimitiveType(typeof value);
  186. is.promise = isObjectOfType("Promise");
  187. is.propertyOf = (target, key, predicate) => {
  188. if (!is.object(target) || !key) {
  189. return false;
  190. }
  191. const value = target[key];
  192. if (is.function(predicate)) {
  193. return predicate(value);
  194. }
  195. return is.defined(value);
  196. };
  197. is.regexp = isObjectOfType("RegExp");
  198. is.set = isObjectOfType("Set");
  199. is.string = isOfType("string");
  200. is.symbol = isOfType("symbol");
  201. is.undefined = isOfType("undefined");
  202. is.weakMap = isObjectOfType("WeakMap");
  203. is.weakSet = isObjectOfType("WeakSet");
  204. var src_default = is;
  205. //# sourceMappingURL=index.js.map