@ohbug/utils 2.0.8 → 2.0.10

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/dist/index.d.mts CHANGED
@@ -1,28 +1,50 @@
1
- import { OhbugLoggerConfig, OhbugGlobal, OhbugObject } from '@ohbug/types';
1
+ import { OhbugGlobal, OhbugLoggerConfig, OhbugObject } from "@ohbug/types";
2
2
 
3
+ //#region src/warning.d.ts
3
4
  declare function error(condition: boolean, format: string, ...args: any[]): void;
4
-
5
+ //#endregion
6
+ //#region src/logger.d.ts
5
7
  declare const logger: OhbugLoggerConfig;
6
-
8
+ //#endregion
9
+ //#region src/validators.d.ts
7
10
  declare function isString(value: any): value is string;
8
11
  declare function isNumber(value: any): value is number;
9
12
  declare function isFunction(value: any): value is Function;
10
- declare function isObject(value: any): value is Object;
13
+ declare function isObject(value: any): value is object;
11
14
  declare function isPromise(value: any): value is Promise<any>;
12
-
15
+ //#endregion
16
+ //#region src/get.d.ts
13
17
  declare function getGlobal<T = Window>(): T & OhbugGlobal;
14
18
  declare function getOhbugObject<T = Window>(): OhbugObject;
15
19
  declare function isNode(): boolean;
16
20
  declare function isBrowser(): boolean;
17
-
21
+ //#endregion
22
+ //#region src/mixin.d.ts
18
23
  declare function replace(source: any, name: string, behavior: (...args: any[]) => any): any;
19
24
  declare function parseUrl(url: string): {
20
- host?: string;
21
- path?: string;
22
- protocol?: string;
23
- relative?: string;
25
+ host?: string;
26
+ path?: string;
27
+ protocol?: string;
28
+ relative?: string;
24
29
  };
25
-
30
+ //#endregion
31
+ //#region src/dom.d.ts
26
32
  declare const getSelector: (event: Event) => any;
27
-
28
- export { error, getGlobal, getOhbugObject, getSelector, isBrowser, isFunction, isNode, isNumber, isObject, isPromise, isString, logger, parseUrl, replace };
33
+ //#endregion
34
+ //#region src/safeCall.d.ts
35
+ /**
36
+ * Prevents recursive exceptions in error-handling paths where system
37
+ * APIs (e.g. `os.hostname()`) might throw inside an uncaughtException handler.
38
+ */
39
+ declare function safeCall<T>(fn: () => T, fallback: T): T;
40
+ //#endregion
41
+ //#region src/getCircularReplacer.d.ts
42
+ /**
43
+ * Creates a JSON replacer that strips circular references.
44
+ * Must be called fresh for each `JSON.stringify` — the internal WeakSet
45
+ * tracks objects from that single serialization pass.
46
+ */
47
+ declare const getCircularReplacer: () => (_: any, value: any) => any;
48
+ //#endregion
49
+ export { error, getCircularReplacer, getGlobal, getOhbugObject, getSelector, isBrowser, isFunction, isNode, isNumber, isObject, isPromise, isString, logger, parseUrl, replace, safeCall };
50
+ //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -1,135 +1,140 @@
1
- // src/warning.ts
1
+ //#region src/warning.ts
2
2
  function error(condition, format, ...args) {
3
- if (format === void 0) {
4
- throw new Error("`Ohbug warning(condition, format, ...args)` requires a warning message argument");
5
- }
6
- if (!condition) {
7
- let argIndex = 0;
8
- const message = format.replace(/%s/g, () => args[argIndex++]);
9
- throw new Error(`Ohbug ${message}`);
10
- }
3
+ if (format === void 0) throw new Error("`Ohbug warning(condition, format, ...args)` requires a warning message argument");
4
+ if (!condition) {
5
+ let argIndex = 0;
6
+ const message = format.replace(/%s/g, () => args[argIndex++]);
7
+ throw new Error(`Ohbug ${message}`);
8
+ }
11
9
  }
12
-
13
- // src/logger.ts
14
- var logger = {
15
- log(...args) {
16
- console.log(...args);
17
- },
18
- info(...args) {
19
- console.info(...args);
20
- },
21
- warn(...args) {
22
- console.warn(...args);
23
- },
24
- error(...args) {
25
- console.error(...args);
26
- }
10
+ //#endregion
11
+ //#region src/logger.ts
12
+ const logger = {
13
+ log(...args) {
14
+ console.log(...args);
15
+ },
16
+ info(...args) {
17
+ console.info(...args);
18
+ },
19
+ warn(...args) {
20
+ console.warn(...args);
21
+ },
22
+ error(...args) {
23
+ console.error(...args);
24
+ }
27
25
  };
28
-
29
- // src/validators.ts
26
+ //#endregion
27
+ //#region src/validators.ts
30
28
  function isString(value) {
31
- return typeof value === "string";
29
+ return typeof value === "string";
32
30
  }
33
31
  function isNumber(value) {
34
- return typeof value === "number" && parseInt(`${value}`, 10) === value;
32
+ return typeof value === "number" && parseInt(`${value}`, 10) === value;
35
33
  }
36
34
  function isFunction(value) {
37
- return typeof value === "function";
35
+ return typeof value === "function";
38
36
  }
39
37
  function isObject(value) {
40
- return Object.prototype.toString.call(value) === "[object Object]";
38
+ return Object.prototype.toString.call(value) === "[object Object]";
41
39
  }
42
40
  function isPromise(value) {
43
- return value instanceof Promise;
41
+ return value instanceof Promise;
44
42
  }
45
-
46
- // src/get.ts
47
- var fallbackGlobalObject = {};
43
+ //#endregion
44
+ //#region src/get.ts
45
+ const fallbackGlobalObject = {};
48
46
  function getGlobal() {
49
- return typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : fallbackGlobalObject;
47
+ return typeof window !== "undefined" ? window : typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : fallbackGlobalObject;
50
48
  }
51
49
  function getOhbugObject() {
52
- const global2 = getGlobal();
53
- error(
54
- Boolean(global2.__OHBUG__),
55
- "Failed to get `OhbugObject`, please confirm if `Ohbug.setup`"
56
- );
57
- return global2.__OHBUG__;
50
+ const global = getGlobal();
51
+ error(Boolean(global.__OHBUG__), "Failed to get `OhbugObject`, please confirm if `Ohbug.setup`");
52
+ return global.__OHBUG__;
58
53
  }
59
54
  function isNode() {
60
- return typeof global !== "undefined";
55
+ return typeof globalThis !== "undefined" && typeof window === "undefined";
61
56
  }
62
57
  function isBrowser() {
63
- return typeof window !== "undefined";
58
+ return typeof window !== "undefined";
64
59
  }
65
-
66
- // src/mixin.ts
60
+ //#endregion
61
+ //#region src/mixin.ts
67
62
  function replace(source, name, behavior) {
68
- if (!(name in source)) {
69
- return;
70
- }
71
- const original = source[name];
72
- const wrapped = behavior(original);
73
- source[name] = wrapped;
74
- return original;
63
+ if (!(name in source)) return;
64
+ const original = source[name];
65
+ source[name] = behavior(original);
66
+ return original;
75
67
  }
76
68
  function parseUrl(url) {
77
- if (typeof url !== "string") {
78
- return {};
79
- }
80
- const match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);
81
- if (!match) {
82
- return {};
83
- }
84
- const query = match[6] || "";
85
- const fragment = match[8] || "";
86
- return {
87
- host: match[4],
88
- path: match[5],
89
- protocol: match[2],
90
- relative: match[5] + query + fragment
91
- };
69
+ if (typeof url !== "string") return {};
70
+ const match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);
71
+ if (!match) return {};
72
+ const query = match[6] || "";
73
+ const fragment = match[8] || "";
74
+ return {
75
+ host: match[4],
76
+ path: match[5],
77
+ protocol: match[2],
78
+ relative: match[5] + query + fragment
79
+ };
92
80
  }
93
-
94
- // src/dom.ts
81
+ //#endregion
82
+ //#region src/dom.ts
95
83
  function getParentNode(node, path) {
96
- if (node.parentNode) {
97
- path.push(node.parentNode);
98
- getParentNode(node.parentNode, path);
99
- }
84
+ if (node.parentNode) {
85
+ path.push(node.parentNode);
86
+ getParentNode(node.parentNode, path);
87
+ }
100
88
  }
101
89
  function getPath(node) {
102
- const path = [];
103
- path.push(node);
104
- getParentNode(node, path);
105
- return path;
90
+ const path = [];
91
+ path.push(node);
92
+ getParentNode(node, path);
93
+ return path;
106
94
  }
107
- var getSelector = (event) => {
108
- const immutableTarget = event.target || event.srcElement;
109
- let target = event.target || event.srcElement;
110
- const elements = [];
111
- for (let i = 0; target && target.nodeType === Node.ELEMENT_NODE && target.nodeType !== Node.DOCUMENT_TYPE_NODE; target = target.previousSibling) {
112
- if (i)
113
- elements.push(target);
114
- i += 1;
115
- }
116
- const path = typeof event.path === "undefined" ? getPath(event.target) : event.path;
117
- const { outerHTML } = immutableTarget;
118
- return path.reverse().map((node) => (node.localName || "") + (node.id ? `#${node.id}` : "") + (node.className ? `.${node.className}` : "") + (node.outerHTML === outerHTML ? `:nth-child(${elements.length})` : "")).filter((v) => Boolean(v)).join(" > ");
95
+ const getSelector = (event) => {
96
+ const immutableTarget = event.target || event.srcElement;
97
+ let target = event.target || event.srcElement;
98
+ const elements = [];
99
+ for (let i = 0; target && target.nodeType === Node.ELEMENT_NODE && target.nodeType !== Node.DOCUMENT_TYPE_NODE; target = target.previousSibling) {
100
+ if (i) elements.push(target);
101
+ i += 1;
102
+ }
103
+ const path = typeof event.path === "undefined" ? getPath(event.target) : event.path;
104
+ const { outerHTML } = immutableTarget;
105
+ return path.reverse().map((node) => (node.localName || "") + (node.id ? `#${node.id}` : "") + (node.className ? `.${node.className}` : "") + (node.outerHTML === outerHTML ? `:nth-child(${elements.length})` : "")).filter((v) => Boolean(v)).join(" > ");
119
106
  };
120
- export {
121
- error,
122
- getGlobal,
123
- getOhbugObject,
124
- getSelector,
125
- isBrowser,
126
- isFunction,
127
- isNode,
128
- isNumber,
129
- isObject,
130
- isPromise,
131
- isString,
132
- logger,
133
- parseUrl,
134
- replace
107
+ //#endregion
108
+ //#region src/safeCall.ts
109
+ /**
110
+ * Prevents recursive exceptions in error-handling paths where system
111
+ * APIs (e.g. `os.hostname()`) might throw inside an uncaughtException handler.
112
+ */
113
+ function safeCall(fn, fallback) {
114
+ try {
115
+ return fn();
116
+ } catch {
117
+ return fallback;
118
+ }
119
+ }
120
+ //#endregion
121
+ //#region src/getCircularReplacer.ts
122
+ /**
123
+ * Creates a JSON replacer that strips circular references.
124
+ * Must be called fresh for each `JSON.stringify` — the internal WeakSet
125
+ * tracks objects from that single serialization pass.
126
+ */
127
+ const getCircularReplacer = () => {
128
+ const seen = /* @__PURE__ */ new WeakSet();
129
+ return (_, value) => {
130
+ if (typeof value === "object" && value !== null) {
131
+ if (seen.has(value)) return;
132
+ seen.add(value);
133
+ }
134
+ return value;
135
+ };
135
136
  };
137
+ //#endregion
138
+ export { error, getCircularReplacer, getGlobal, getOhbugObject, getSelector, isBrowser, isFunction, isNode, isNumber, isObject, isPromise, isString, logger, parseUrl, replace, safeCall };
139
+
140
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/warning.ts","../src/logger.ts","../src/validators.ts","../src/get.ts","../src/mixin.ts","../src/dom.ts","../src/safeCall.ts","../src/getCircularReplacer.ts"],"sourcesContent":["export function error(condition: boolean, format: string, ...args: any[]) {\n if (format === undefined) {\n throw new Error(\n \"`Ohbug warning(condition, format, ...args)` requires a warning message argument\",\n );\n }\n\n if (!condition) {\n let argIndex = 0;\n const message = format.replace(/%s/g, () => args[argIndex++]);\n throw new Error(`Ohbug ${message}`);\n }\n}\n","/* eslint-disable no-console */\nimport type { OhbugLoggerConfig } from \"@ohbug/types\";\n\nexport const logger: OhbugLoggerConfig = {\n log(...args: any[]) {\n console.log(...args);\n },\n\n info(...args: any[]) {\n console.info(...args);\n },\n\n warn(...args: any[]) {\n console.warn(...args);\n },\n\n error(...args: any[]) {\n console.error(...args);\n },\n};\n","export function isString(value: any): value is string {\n return typeof value === \"string\";\n}\n\nexport function isNumber(value: any): value is number {\n return typeof value === \"number\" && parseInt(`${value}`, 10) === value;\n}\n\nexport function isFunction(value: any): value is Function {\n return typeof value === \"function\";\n}\n\nexport function isObject(value: any): value is object {\n return Object.prototype.toString.call(value) === \"[object Object]\";\n}\n\nexport function isPromise(value: any): value is Promise<any> {\n return value instanceof Promise;\n}\n","import type { OhbugGlobal, OhbugObject } from \"@ohbug/types\";\n\nimport { error } from \"./warning\";\n\nconst fallbackGlobalObject = {};\nexport function getGlobal<T = Window>(): T & OhbugGlobal {\n return (\n typeof window !== \"undefined\"\n ? window\n : typeof globalThis !== \"undefined\"\n ? globalThis\n : typeof self !== \"undefined\"\n ? self\n : fallbackGlobalObject\n ) as T & OhbugGlobal;\n}\n\nexport function getOhbugObject<T = Window>(): OhbugObject {\n const global = getGlobal<T>();\n\n error(Boolean(global.__OHBUG__), \"Failed to get `OhbugObject`, please confirm if `Ohbug.setup`\");\n\n return global.__OHBUG__;\n}\n\nexport function isNode(): boolean {\n return typeof globalThis !== \"undefined\" && typeof window === \"undefined\";\n}\n\nexport function isBrowser(): boolean {\n return typeof window !== \"undefined\";\n}\n","export function replace(source: any, name: string, behavior: (...args: any[]) => any) {\n if (!(name in source)) {\n return;\n }\n\n const original = source[name];\n const wrapped = behavior(original);\n source[name] = wrapped;\n\n return original;\n}\n\nexport function parseUrl(url: string): {\n host?: string;\n path?: string;\n protocol?: string;\n relative?: string;\n} {\n if (typeof url !== \"string\") {\n return {};\n }\n\n const match = url.match(/^(([^:/?#]+):)?(\\/\\/([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$/);\n\n if (!match) {\n return {};\n }\n\n const query = match[6] || \"\";\n const fragment = match[8] || \"\";\n return {\n host: match[4],\n path: match[5],\n protocol: match[2],\n relative: match[5] + query + fragment,\n };\n}\n","function getParentNode(node: Node, path: Node[]) {\n if (node.parentNode) {\n path.push(node.parentNode);\n getParentNode(node.parentNode, path);\n }\n}\nfunction getPath(node: Node) {\n const path: Node[] = [];\n path.push(node);\n getParentNode(node, path);\n return path;\n}\nexport const getSelector = (event: Event) => {\n const immutableTarget = (event.target || event.srcElement) as any;\n let target = (event.target || event.srcElement) as any;\n // 获取出错元素在同级元素的 index\n // 储存错误元素前元素\n const elements = [];\n for (\n let i = 0;\n target && target.nodeType === Node.ELEMENT_NODE && target.nodeType !== Node.DOCUMENT_TYPE_NODE;\n target = target.previousSibling\n ) {\n if (i) elements.push(target);\n i += 1;\n }\n const path =\n typeof (event as any).path === \"undefined\"\n ? getPath(event.target as Node)\n : (event as any).path;\n const { outerHTML } = immutableTarget;\n return path\n .reverse()\n .map(\n (node: Element) =>\n (node.localName || \"\") +\n (node.id ? `#${node.id}` : \"\") +\n (node.className ? `.${node.className}` : \"\") +\n (node.outerHTML === outerHTML ? `:nth-child(${elements.length})` : \"\"),\n )\n .filter((v: string): boolean => Boolean(v))\n .join(\" > \");\n};\n","/**\n * Prevents recursive exceptions in error-handling paths where system\n * APIs (e.g. `os.hostname()`) might throw inside an uncaughtException handler.\n */\nexport function safeCall<T>(fn: () => T, fallback: T): T {\n try {\n return fn();\n } catch {\n return fallback;\n }\n}\n","/**\n * Creates a JSON replacer that strips circular references.\n * Must be called fresh for each `JSON.stringify` — the internal WeakSet\n * tracks objects from that single serialization pass.\n */\nexport const getCircularReplacer = () => {\n const seen = new WeakSet();\n return (_: any, value: any) => {\n if (typeof value === \"object\" && value !== null) {\n if (seen.has(value)) {\n return;\n }\n\n seen.add(value);\n }\n return value;\n };\n};\n"],"mappings":"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"}
package/package.json CHANGED
@@ -1,40 +1,38 @@
1
1
  {
2
2
  "name": "@ohbug/utils",
3
- "version": "2.0.8",
3
+ "version": "2.0.10",
4
4
  "description": "Utilities for all Ohbug SDKs",
5
- "license": "Apache-2.0",
6
- "author": "chenyueban <jasonchan0527@gmail.com>",
7
- "homepage": "https://github.com/ohbug-org/ohbug",
5
+ "homepage": "https://github.com/ohbug-org/ohbug#readme",
8
6
  "bugs": {
9
7
  "url": "https://github.com/ohbug-org/ohbug/issues"
10
8
  },
9
+ "license": "Apache-2.0",
10
+ "author": {
11
+ "name": "xinyao",
12
+ "email": "hi@xinyao.me"
13
+ },
11
14
  "repository": {
12
15
  "type": "git",
13
- "url": "https://github.com/ohbug-org/ohbug"
14
- },
15
- "main": "dist/index.js",
16
- "module": "dist/index.mjs",
17
- "types": "dist/index.d.ts",
18
- "exports": {
19
- ".": {
20
- "require": "./dist/index.js",
21
- "import": "./dist/index.mjs",
22
- "types": "./dist/index.d.ts"
23
- }
16
+ "url": "git+https://github.com/ohbug-org/ohbug.git"
24
17
  },
18
+ "funding": "https://github.com/sponsors/xinyao27",
25
19
  "files": [
26
20
  "dist"
27
21
  ],
22
+ "type": "module",
28
23
  "sideEffects": false,
24
+ "types": "dist/index.d.mts",
25
+ "exports": {
26
+ ".": "./dist/index.mjs",
27
+ "./package.json": "./package.json"
28
+ },
29
29
  "publishConfig": {
30
30
  "access": "public"
31
31
  },
32
32
  "dependencies": {
33
- "@types/node": "^20.4.0",
34
- "@ohbug/types": "2.2.1"
33
+ "@ohbug/types": "2.2.2"
35
34
  },
36
35
  "scripts": {
37
- "build": "tsup",
38
- "dev": "tsup --watch"
36
+ "dev": "vp pack --watch"
39
37
  }
40
38
  }
package/dist/index.d.ts DELETED
@@ -1,28 +0,0 @@
1
- import { OhbugLoggerConfig, OhbugGlobal, OhbugObject } from '@ohbug/types';
2
-
3
- declare function error(condition: boolean, format: string, ...args: any[]): void;
4
-
5
- declare const logger: OhbugLoggerConfig;
6
-
7
- declare function isString(value: any): value is string;
8
- declare function isNumber(value: any): value is number;
9
- declare function isFunction(value: any): value is Function;
10
- declare function isObject(value: any): value is Object;
11
- declare function isPromise(value: any): value is Promise<any>;
12
-
13
- declare function getGlobal<T = Window>(): T & OhbugGlobal;
14
- declare function getOhbugObject<T = Window>(): OhbugObject;
15
- declare function isNode(): boolean;
16
- declare function isBrowser(): boolean;
17
-
18
- declare function replace(source: any, name: string, behavior: (...args: any[]) => any): any;
19
- declare function parseUrl(url: string): {
20
- host?: string;
21
- path?: string;
22
- protocol?: string;
23
- relative?: string;
24
- };
25
-
26
- declare const getSelector: (event: Event) => any;
27
-
28
- export { error, getGlobal, getOhbugObject, getSelector, isBrowser, isFunction, isNode, isNumber, isObject, isPromise, isString, logger, parseUrl, replace };
package/dist/index.js DELETED
@@ -1,175 +0,0 @@
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
-
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- error: () => error,
24
- getGlobal: () => getGlobal,
25
- getOhbugObject: () => getOhbugObject,
26
- getSelector: () => getSelector,
27
- isBrowser: () => isBrowser,
28
- isFunction: () => isFunction,
29
- isNode: () => isNode,
30
- isNumber: () => isNumber,
31
- isObject: () => isObject,
32
- isPromise: () => isPromise,
33
- isString: () => isString,
34
- logger: () => logger,
35
- parseUrl: () => parseUrl,
36
- replace: () => replace
37
- });
38
- module.exports = __toCommonJS(src_exports);
39
-
40
- // src/warning.ts
41
- function error(condition, format, ...args) {
42
- if (format === void 0) {
43
- throw new Error("`Ohbug warning(condition, format, ...args)` requires a warning message argument");
44
- }
45
- if (!condition) {
46
- let argIndex = 0;
47
- const message = format.replace(/%s/g, () => args[argIndex++]);
48
- throw new Error(`Ohbug ${message}`);
49
- }
50
- }
51
-
52
- // src/logger.ts
53
- var logger = {
54
- log(...args) {
55
- console.log(...args);
56
- },
57
- info(...args) {
58
- console.info(...args);
59
- },
60
- warn(...args) {
61
- console.warn(...args);
62
- },
63
- error(...args) {
64
- console.error(...args);
65
- }
66
- };
67
-
68
- // src/validators.ts
69
- function isString(value) {
70
- return typeof value === "string";
71
- }
72
- function isNumber(value) {
73
- return typeof value === "number" && parseInt(`${value}`, 10) === value;
74
- }
75
- function isFunction(value) {
76
- return typeof value === "function";
77
- }
78
- function isObject(value) {
79
- return Object.prototype.toString.call(value) === "[object Object]";
80
- }
81
- function isPromise(value) {
82
- return value instanceof Promise;
83
- }
84
-
85
- // src/get.ts
86
- var fallbackGlobalObject = {};
87
- function getGlobal() {
88
- return typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : fallbackGlobalObject;
89
- }
90
- function getOhbugObject() {
91
- const global2 = getGlobal();
92
- error(
93
- Boolean(global2.__OHBUG__),
94
- "Failed to get `OhbugObject`, please confirm if `Ohbug.setup`"
95
- );
96
- return global2.__OHBUG__;
97
- }
98
- function isNode() {
99
- return typeof global !== "undefined";
100
- }
101
- function isBrowser() {
102
- return typeof window !== "undefined";
103
- }
104
-
105
- // src/mixin.ts
106
- function replace(source, name, behavior) {
107
- if (!(name in source)) {
108
- return;
109
- }
110
- const original = source[name];
111
- const wrapped = behavior(original);
112
- source[name] = wrapped;
113
- return original;
114
- }
115
- function parseUrl(url) {
116
- if (typeof url !== "string") {
117
- return {};
118
- }
119
- const match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);
120
- if (!match) {
121
- return {};
122
- }
123
- const query = match[6] || "";
124
- const fragment = match[8] || "";
125
- return {
126
- host: match[4],
127
- path: match[5],
128
- protocol: match[2],
129
- relative: match[5] + query + fragment
130
- };
131
- }
132
-
133
- // src/dom.ts
134
- function getParentNode(node, path) {
135
- if (node.parentNode) {
136
- path.push(node.parentNode);
137
- getParentNode(node.parentNode, path);
138
- }
139
- }
140
- function getPath(node) {
141
- const path = [];
142
- path.push(node);
143
- getParentNode(node, path);
144
- return path;
145
- }
146
- var getSelector = (event) => {
147
- const immutableTarget = event.target || event.srcElement;
148
- let target = event.target || event.srcElement;
149
- const elements = [];
150
- for (let i = 0; target && target.nodeType === Node.ELEMENT_NODE && target.nodeType !== Node.DOCUMENT_TYPE_NODE; target = target.previousSibling) {
151
- if (i)
152
- elements.push(target);
153
- i += 1;
154
- }
155
- const path = typeof event.path === "undefined" ? getPath(event.target) : event.path;
156
- const { outerHTML } = immutableTarget;
157
- return path.reverse().map((node) => (node.localName || "") + (node.id ? `#${node.id}` : "") + (node.className ? `.${node.className}` : "") + (node.outerHTML === outerHTML ? `:nth-child(${elements.length})` : "")).filter((v) => Boolean(v)).join(" > ");
158
- };
159
- // Annotate the CommonJS export names for ESM import in node:
160
- 0 && (module.exports = {
161
- error,
162
- getGlobal,
163
- getOhbugObject,
164
- getSelector,
165
- isBrowser,
166
- isFunction,
167
- isNode,
168
- isNumber,
169
- isObject,
170
- isPromise,
171
- isString,
172
- logger,
173
- parseUrl,
174
- replace
175
- });