@ocavue/utils 0.4.0 → 0.6.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.
package/README.md CHANGED
@@ -4,6 +4,10 @@
4
4
 
5
5
  A collection of utility functions.
6
6
 
7
+ ## [API Reference](https://doc.deno.land/https://esm.sh/@ocavue/utils)
8
+
9
+ ## [Changelog](https://github.com/ocavue/utils/releases)
10
+
7
11
  ## License
8
12
 
9
13
  MIT
package/dist/index.d.ts CHANGED
@@ -1,15 +1,87 @@
1
- export { isObject_alias_1 as isObject } from './_tsup-dts-rollup.js';
2
- export { isElement_alias_1 as isElement } from './_tsup-dts-rollup.js';
3
- export { isTextNode_alias_1 as isTextNode } from './_tsup-dts-rollup.js';
4
- export { isHTMLElement_alias_1 as isHTMLElement } from './_tsup-dts-rollup.js';
5
- export { isSVGElement_alias_1 as isSVGElement } from './_tsup-dts-rollup.js';
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';
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';
1
+ /**
2
+ * Checks if the given value is an object.
3
+ */
4
+ declare function isObject(value: unknown): value is Record<string | symbol | number, unknown>;
5
+
6
+ /**
7
+ * Checks if the given DOM node is an Element.
8
+ */
9
+ declare function isElement(node: Node): node is Element;
10
+ /**
11
+ * Checks if the given DOM node is a Text node.
12
+ */
13
+ declare function isTextNode(node: Node): node is Text;
14
+ /**
15
+ * Checks if the given DOM node is an HTMLElement.
16
+ */
17
+ declare function isHTMLElement(node: Node): node is HTMLElement;
18
+ /**
19
+ * Checks if the given DOM node is an SVGElement.
20
+ */
21
+ declare function isSVGElement(node: Node): node is SVGElement;
22
+ /**
23
+ * Checks if the given DOM node is an MathMLElement.
24
+ */
25
+ declare function isMathMLElement(node: Node): node is MathMLElement;
26
+ /**
27
+ * Checks if the given DOM node is a Document.
28
+ */
29
+ declare function isDocument(node: Node): node is Document;
30
+ /**
31
+ * Checks if the given DOM node is a DocumentFragment.
32
+ */
33
+ declare function isDocumentFragment(node: Node): node is DocumentFragment;
34
+ /**
35
+ * Checks if the given DOM node is a ShadowRoot.
36
+ */
37
+ declare function isShadowRoot(node: Node): node is ShadowRoot;
38
+ /**
39
+ * Checks if an unknown value is likely a DOM node.
40
+ */
41
+ declare function isNodeLike(value: unknown): value is Node;
42
+ /**
43
+ * Checks if an unknown value is likely a DOM element.
44
+ */
45
+ declare function isElementLike(value: unknown): value is Element;
46
+ /**
47
+ * Checks if the given value is likely a Window object.
48
+ */
49
+ declare function isWindowLike(value: unknown): value is Window;
50
+ /**
51
+ * Gets the window object for the given target or the global window object if no
52
+ * target is provided.
53
+ */
54
+ declare function getWindow(target?: Node | ShadowRoot | Document | null): Window & typeof globalThis;
55
+ /**
56
+ * Gets the document for the given target or the global document if no target is
57
+ * provided.
58
+ */
59
+ declare function getDocument(target?: Element | Window | Node | Document | null): Document;
60
+ /**
61
+ * Gets a reference to the root node of the document based on the given target.
62
+ */
63
+ declare function getDocumentElement(target?: Element | Node | Window | Document | null): HTMLElement;
64
+
65
+ /**
66
+ * Creates a function that will only execute the provided function once.
67
+ * Subsequent calls will return the cached result from the first execution.
68
+ *
69
+ * @param fn The function to execute once
70
+ * @returns A function that will only execute the provided function once
71
+ * @example
72
+ * ```ts
73
+ * const getValue = once(() => expensiveOperation())
74
+ * getValue() // executes expensiveOperation
75
+ * getValue() // returns cached result
76
+ * ```
77
+ */
78
+ declare function once<T>(fn: () => T): () => T;
79
+
80
+ /**
81
+ * Formats a number of bytes into a human-readable string.
82
+ * @param bytes - The number of bytes to format.
83
+ * @returns A string representing the number of bytes in a human-readable format.
84
+ */
85
+ declare function formatBytes(bytes: number): string;
86
+
87
+ export { formatBytes, getDocument, getDocumentElement, getWindow, isDocument, isDocumentFragment, isElement, isElementLike, isHTMLElement, isMathMLElement, isNodeLike, isObject, isSVGElement, isShadowRoot, isTextNode, isWindowLike, once };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/checker.ts
2
2
  function isObject(value) {
3
- return value != null && typeof value === "object" && Array.isArray(value) === false;
3
+ return value != null && typeof value === "object";
4
4
  }
5
5
 
6
6
  // src/dom.ts
@@ -66,7 +66,35 @@ function getDocument(target) {
66
66
  function getDocumentElement(target) {
67
67
  return getDocument(target).documentElement;
68
68
  }
69
+
70
+ // src/once.ts
71
+ function once(fn) {
72
+ let called = false;
73
+ let result;
74
+ return () => {
75
+ if (!called) {
76
+ result = fn();
77
+ called = true;
78
+ fn = void 0;
79
+ }
80
+ return result;
81
+ };
82
+ }
83
+
84
+ // src/format-bytes.ts
85
+ function formatBytes(bytes) {
86
+ const units = ["B", "KB", "MB", "GB"];
87
+ let unitIndex = 0;
88
+ let num = bytes;
89
+ while (Math.abs(num) >= 1024 && unitIndex < units.length - 1) {
90
+ num /= 1024;
91
+ unitIndex++;
92
+ }
93
+ const fraction = unitIndex === 0 && num % 1 === 0 ? 0 : 1;
94
+ return `${num.toFixed(fraction)}${units[unitIndex]}`;
95
+ }
69
96
  export {
97
+ formatBytes,
70
98
  getDocument,
71
99
  getDocumentElement,
72
100
  getWindow,
@@ -81,5 +109,6 @@ export {
81
109
  isSVGElement,
82
110
  isShadowRoot,
83
111
  isTextNode,
84
- isWindowLike
112
+ isWindowLike,
113
+ once
85
114
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocavue/utils",
3
3
  "type": "module",
4
- "version": "0.4.0",
4
+ "version": "0.6.0",
5
5
  "description": "",
6
6
  "author": "ocavue <ocavue@gmail.com>",
7
7
  "license": "MIT",
@@ -36,21 +36,27 @@
36
36
  "dist"
37
37
  ],
38
38
  "devDependencies": {
39
- "@microsoft/api-extractor": "^7.52.2",
40
- "@ocavue/eslint-config": "^2.13.1",
41
- "@ocavue/tsconfig": "^0.2.0",
39
+ "@ocavue/eslint-config": "^3.2.0",
40
+ "@ocavue/tsconfig": "^0.3.7",
41
+ "@size-limit/preset-small-lib": "^11.2.0",
42
42
  "@types/node": "^20.17.27",
43
- "eslint": "^9.23.0",
44
- "jsdom": "^26.0.0",
45
- "prettier": "^3.5.3",
46
- "tsup": "^8.4.0",
47
- "typescript": "^5.7.2",
48
- "vite": "^6.0.12",
49
- "vitest": "^3.0.9"
43
+ "eslint": "^9.31.0",
44
+ "jsdom": "^26.1.0",
45
+ "prettier": "^3.6.2",
46
+ "size-limit": "^11.2.0",
47
+ "tsup": "^8.5.0",
48
+ "typescript": "^5.8.3",
49
+ "vite": "^6.3.5",
50
+ "vitest": "^3.2.4"
50
51
  },
51
52
  "publishConfig": {
52
53
  "access": "public"
53
54
  },
55
+ "size-limit": [
56
+ {
57
+ "path": "dist/index.js"
58
+ }
59
+ ],
54
60
  "renovate": {
55
61
  "extends": [
56
62
  "github>ocavue/config-renovate"
@@ -1,105 +0,0 @@
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
- */
41
- declare function isElement(node: Node): node is Element;
42
- export { isElement }
43
- export { isElement as isElement_alias_1 }
44
-
45
- /**
46
- * Checks if an unknown value is likely a DOM element.
47
- */
48
- declare function isElementLike(value: unknown): value is Element;
49
- export { isElementLike }
50
- export { isElementLike as isElementLike_alias_1 }
51
-
52
- /**
53
- * Checks if the given DOM node is an HTMLElement.
54
- */
55
- declare function isHTMLElement(node: Node): node is HTMLElement;
56
- export { isHTMLElement }
57
- export { isHTMLElement as isHTMLElement_alias_1 }
58
-
59
- /**
60
- * Checks if the given DOM node is an MathMLElement.
61
- */
62
- declare function isMathMLElement(node: Node): node is MathMLElement;
63
- export { isMathMLElement }
64
- export { isMathMLElement as isMathMLElement_alias_1 }
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
-
73
- declare function isObject(value: unknown): value is Record<string | symbol | number, unknown>;
74
- export { isObject }
75
- export { isObject as isObject_alias_1 }
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
- */
87
- declare function isSVGElement(node: Node): node is SVGElement;
88
- export { isSVGElement }
89
- export { isSVGElement as isSVGElement_alias_1 }
90
-
91
- /**
92
- * Checks if the given DOM node is a Text node.
93
- */
94
- declare function isTextNode(node: Node): node is Text;
95
- export { isTextNode }
96
- export { isTextNode as isTextNode_alias_1 }
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
-
105
- export { }