index.mjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. export {
  182. src_default as default
  183. };
  184. //# sourceMappingURL=index.mjs.map