@ocavue/utils 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,43 @@
1
+ /**
2
+ * Gets the document for the given target or the global document if no target is
3
+ * provided.
4
+ */
5
+ declare function getDocument(target?: Element | Window | Node | Document | null | undefined): Document;
6
+ export { getDocument }
7
+ export { getDocument as getDocument_alias_1 }
8
+
9
+ /**
10
+ * Gets a reference to the root node of the document based on the given target.
11
+ */
12
+ declare function getDocumentElement(target?: Element | Node | Window | Document | null | undefined): HTMLElement;
13
+ export { getDocumentElement }
14
+ export { getDocumentElement as getDocumentElement_alias_1 }
15
+
16
+ /**
17
+ * Gets the window object for the given target or the global window object if no
18
+ * target is provided.
19
+ */
20
+ declare function getWindow(target?: Node | ShadowRoot | Document | null | undefined): Window & typeof globalThis;
21
+ export { getWindow }
22
+ export { getWindow as getWindow_alias_1 }
23
+
24
+ /**
25
+ * Checks if the given DOM node is a Document.
26
+ */
27
+ declare function isDocument(node: Node): node is Document;
28
+ export { isDocument }
29
+ export { isDocument as isDocument_alias_1 }
30
+
31
+ /**
32
+ * Checks if the given DOM node is a DocumentFragment.
33
+ */
34
+ declare function isDocumentFragment(node: Node): node is DocumentFragment;
35
+ export { isDocumentFragment }
36
+ export { isDocumentFragment as isDocumentFragment_alias_1 }
37
+
38
+ /**
39
+ * Checks if the given DOM node is an Element.
40
+ */
1
41
  declare function isElement(node: Node): node is Element;
2
42
  export { isElement }
3
43
  export { isElement as isElement_alias_1 }
@@ -9,24 +49,57 @@ declare function isElementLike(value: unknown): value is Element;
9
49
  export { isElementLike }
10
50
  export { isElementLike as isElementLike_alias_1 }
11
51
 
52
+ /**
53
+ * Checks if the given DOM node is an HTMLElement.
54
+ */
12
55
  declare function isHTMLElement(node: Node): node is HTMLElement;
13
56
  export { isHTMLElement }
14
57
  export { isHTMLElement as isHTMLElement_alias_1 }
15
58
 
59
+ /**
60
+ * Checks if the given DOM node is an MathMLElement.
61
+ */
16
62
  declare function isMathMLElement(node: Node): node is MathMLElement;
17
63
  export { isMathMLElement }
18
64
  export { isMathMLElement as isMathMLElement_alias_1 }
19
65
 
66
+ /**
67
+ * Checks if an unknown value is likely a DOM node.
68
+ */
69
+ declare function isNodeLike(value: unknown): value is Node;
70
+ export { isNodeLike }
71
+ export { isNodeLike as isNodeLike_alias_1 }
72
+
20
73
  declare function isObject(value: unknown): value is Record<string | symbol | number, unknown>;
21
74
  export { isObject }
22
75
  export { isObject as isObject_alias_1 }
23
76
 
77
+ /**
78
+ * Checks if the given DOM node is a ShadowRoot.
79
+ */
80
+ declare function isShadowRoot(node: Node): node is ShadowRoot;
81
+ export { isShadowRoot }
82
+ export { isShadowRoot as isShadowRoot_alias_1 }
83
+
84
+ /**
85
+ * Checks if the given DOM node is an SVGElement.
86
+ */
24
87
  declare function isSVGElement(node: Node): node is SVGElement;
25
88
  export { isSVGElement }
26
89
  export { isSVGElement as isSVGElement_alias_1 }
27
90
 
91
+ /**
92
+ * Checks if the given DOM node is a Text node.
93
+ */
28
94
  declare function isTextNode(node: Node): node is Text;
29
95
  export { isTextNode }
30
96
  export { isTextNode as isTextNode_alias_1 }
31
97
 
98
+ /**
99
+ * Checks if the given value is likely a Window object.
100
+ */
101
+ declare function isWindowLike(value: unknown): value is Window;
102
+ export { isWindowLike }
103
+ export { isWindowLike as isWindowLike_alias_1 }
104
+
32
105
  export { }
package/dist/index.d.ts CHANGED
@@ -4,4 +4,12 @@ export { isTextNode_alias_1 as isTextNode } from './_tsup-dts-rollup.js';
4
4
  export { isHTMLElement_alias_1 as isHTMLElement } from './_tsup-dts-rollup.js';
5
5
  export { isSVGElement_alias_1 as isSVGElement } from './_tsup-dts-rollup.js';
6
6
  export { isMathMLElement_alias_1 as isMathMLElement } from './_tsup-dts-rollup.js';
7
+ export { isDocument_alias_1 as isDocument } from './_tsup-dts-rollup.js';
8
+ export { isDocumentFragment_alias_1 as isDocumentFragment } from './_tsup-dts-rollup.js';
9
+ export { isShadowRoot_alias_1 as isShadowRoot } from './_tsup-dts-rollup.js';
10
+ export { isNodeLike_alias_1 as isNodeLike } from './_tsup-dts-rollup.js';
7
11
  export { isElementLike_alias_1 as isElementLike } from './_tsup-dts-rollup.js';
12
+ export { isWindowLike_alias_1 as isWindowLike } from './_tsup-dts-rollup.js';
13
+ export { getWindow_alias_1 as getWindow } from './_tsup-dts-rollup.js';
14
+ export { getDocument_alias_1 as getDocument } from './_tsup-dts-rollup.js';
15
+ export { getDocumentElement_alias_1 as getDocumentElement } from './_tsup-dts-rollup.js';
package/dist/index.js CHANGED
@@ -19,15 +19,67 @@ function isSVGElement(node) {
19
19
  function isMathMLElement(node) {
20
20
  return isElement(node) && node.namespaceURI === "http://www.w3.org/1998/Math/MathML";
21
21
  }
22
+ function isDocument(node) {
23
+ return node.nodeType === 9 /* DOCUMENT_NODE */;
24
+ }
25
+ function isDocumentFragment(node) {
26
+ return node.nodeType === 11 /* DOCUMENT_FRAGMENT_NODE */;
27
+ }
28
+ function isShadowRoot(node) {
29
+ return isDocumentFragment(node) && "host" in node && isElementLike(node.host);
30
+ }
31
+ function isNodeLike(value) {
32
+ return isObject(value) && value.nodeType !== void 0;
33
+ }
22
34
  function isElementLike(value) {
23
35
  return isObject(value) && value.nodeType === 1 /* ELEMENT_NODE */ && typeof value.nodeName === "string";
24
36
  }
37
+ function isWindowLike(value) {
38
+ return isObject(value) && value.window === value;
39
+ }
40
+ function getWindow(target) {
41
+ if (target) {
42
+ if (isShadowRoot(target)) {
43
+ return getWindow(target.host);
44
+ }
45
+ if (isDocument(target)) {
46
+ return target.defaultView || window;
47
+ }
48
+ if (isElement(target)) {
49
+ return target.ownerDocument?.defaultView || window;
50
+ }
51
+ }
52
+ return window;
53
+ }
54
+ function getDocument(target) {
55
+ if (target) {
56
+ if (isWindowLike(target)) {
57
+ return target.document;
58
+ }
59
+ if (isDocument(target)) {
60
+ return target;
61
+ }
62
+ return target.ownerDocument || document;
63
+ }
64
+ return document;
65
+ }
66
+ function getDocumentElement(target) {
67
+ return getDocument(target).documentElement;
68
+ }
25
69
  export {
70
+ getDocument,
71
+ getDocumentElement,
72
+ getWindow,
73
+ isDocument,
74
+ isDocumentFragment,
26
75
  isElement,
27
76
  isElementLike,
28
77
  isHTMLElement,
29
78
  isMathMLElement,
79
+ isNodeLike,
30
80
  isObject,
31
81
  isSVGElement,
32
- isTextNode
82
+ isShadowRoot,
83
+ isTextNode,
84
+ isWindowLike
33
85
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocavue/utils",
3
3
  "type": "module",
4
- "version": "0.3.0",
4
+ "version": "0.4.0",
5
5
  "description": "",
6
6
  "author": "ocavue <ocavue@gmail.com>",
7
7
  "license": "MIT",