@ohbug/utils 1.0.13 → 2.0.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 +0 -1
- package/dist/index.d.ts +28 -7
- package/dist/index.js +167 -0
- package/dist/index.mjs +128 -0
- package/package.json +19 -21
- package/src/dom.ts +44 -0
- package/src/get.ts +34 -0
- package/src/index.ts +7 -0
- package/src/logger.ts +20 -0
- package/src/mixin.ts +38 -0
- package/src/validators.ts +19 -0
- package/src/warning.ts +10 -0
- package/dist/dom.d.ts +0 -2
- package/dist/dom.d.ts.map +0 -1
- package/dist/get.d.ts +0 -6
- package/dist/get.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/logger.d.ts +0 -3
- package/dist/logger.d.ts.map +0 -1
- package/dist/mixin.d.ts +0 -8
- package/dist/mixin.d.ts.map +0 -1
- package/dist/ohbug-utils.es.js +0 -205
- package/dist/ohbug-utils.es.min.js +0 -15
- package/dist/ohbug-utils.umd.js +0 -229
- package/dist/ohbug-utils.umd.min.js +0 -15
- package/dist/validators.d.ts +0 -6
- package/dist/validators.d.ts.map +0 -1
- package/dist/warning.d.ts +0 -3
- package/dist/warning.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@ohbug/utils)
|
|
4
4
|
[](https://bundlephobia.com/result?p=@ohbug/utils)
|
|
5
|
-
[](https://github.com/prettier/prettier)
|
|
6
5
|
|
|
7
6
|
Common utilities used by the Ohbug.
|
|
8
7
|
Warning, This package is not part of our public API contract, please do not use it
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var src_exports = {};
|
|
21
|
+
__export(src_exports, {
|
|
22
|
+
error: () => error,
|
|
23
|
+
getGlobal: () => getGlobal,
|
|
24
|
+
getOhbugObject: () => getOhbugObject,
|
|
25
|
+
getSelector: () => getSelector,
|
|
26
|
+
isBrowser: () => isBrowser,
|
|
27
|
+
isFunction: () => isFunction,
|
|
28
|
+
isNode: () => isNode,
|
|
29
|
+
isNumber: () => isNumber,
|
|
30
|
+
isObject: () => isObject,
|
|
31
|
+
isPromise: () => isPromise,
|
|
32
|
+
isString: () => isString,
|
|
33
|
+
logger: () => logger,
|
|
34
|
+
parseUrl: () => parseUrl,
|
|
35
|
+
replace: () => replace
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(src_exports);
|
|
38
|
+
|
|
39
|
+
// src/warning.ts
|
|
40
|
+
function error(condition, format, ...args) {
|
|
41
|
+
if (format === void 0)
|
|
42
|
+
throw new Error("`Ohbug warning(condition, format, ...args)` requires a warning message argument");
|
|
43
|
+
if (!condition) {
|
|
44
|
+
let argIndex = 0;
|
|
45
|
+
const message = format.replace(/%s/g, () => args[argIndex++]);
|
|
46
|
+
throw new Error(`Ohbug ${message}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/logger.ts
|
|
51
|
+
var logger = {
|
|
52
|
+
log(...args) {
|
|
53
|
+
console.log(...args);
|
|
54
|
+
},
|
|
55
|
+
info(...args) {
|
|
56
|
+
console.info(...args);
|
|
57
|
+
},
|
|
58
|
+
warn(...args) {
|
|
59
|
+
console.warn(...args);
|
|
60
|
+
},
|
|
61
|
+
error(...args) {
|
|
62
|
+
console.error(...args);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/validators.ts
|
|
67
|
+
function isString(value) {
|
|
68
|
+
return typeof value === "string";
|
|
69
|
+
}
|
|
70
|
+
function isNumber(value) {
|
|
71
|
+
return typeof value === "number" && parseInt(`${value}`, 10) === value;
|
|
72
|
+
}
|
|
73
|
+
function isFunction(value) {
|
|
74
|
+
return typeof value === "function";
|
|
75
|
+
}
|
|
76
|
+
function isObject(value) {
|
|
77
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
78
|
+
}
|
|
79
|
+
function isPromise(value) {
|
|
80
|
+
return value instanceof Promise;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/get.ts
|
|
84
|
+
var fallbackGlobalObject = {};
|
|
85
|
+
function getGlobal() {
|
|
86
|
+
return typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : fallbackGlobalObject;
|
|
87
|
+
}
|
|
88
|
+
function getOhbugObject() {
|
|
89
|
+
const global2 = getGlobal();
|
|
90
|
+
error(Boolean(global2.__OHBUG__), "Failed to get `OhbugObject`, please confirm if `Ohbug.setup`");
|
|
91
|
+
return global2.__OHBUG__;
|
|
92
|
+
}
|
|
93
|
+
function isNode() {
|
|
94
|
+
return typeof global !== "undefined";
|
|
95
|
+
}
|
|
96
|
+
function isBrowser() {
|
|
97
|
+
return typeof window !== "undefined";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/mixin.ts
|
|
101
|
+
function replace(source, name, behavior) {
|
|
102
|
+
if (!(name in source))
|
|
103
|
+
return;
|
|
104
|
+
const original = source[name];
|
|
105
|
+
const wrapped = behavior(original);
|
|
106
|
+
source[name] = wrapped;
|
|
107
|
+
return original;
|
|
108
|
+
}
|
|
109
|
+
function parseUrl(url) {
|
|
110
|
+
if (typeof url !== "string")
|
|
111
|
+
return {};
|
|
112
|
+
const match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);
|
|
113
|
+
if (!match)
|
|
114
|
+
return {};
|
|
115
|
+
const query = match[6] || "";
|
|
116
|
+
const fragment = match[8] || "";
|
|
117
|
+
return {
|
|
118
|
+
host: match[4],
|
|
119
|
+
path: match[5],
|
|
120
|
+
protocol: match[2],
|
|
121
|
+
relative: match[5] + query + fragment
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/dom.ts
|
|
126
|
+
function getParentNode(node, path) {
|
|
127
|
+
if (node.parentNode) {
|
|
128
|
+
path.push(node.parentNode);
|
|
129
|
+
getParentNode(node.parentNode, path);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function getPath(node) {
|
|
133
|
+
const path = [];
|
|
134
|
+
path.push(node);
|
|
135
|
+
getParentNode(node, path);
|
|
136
|
+
return path;
|
|
137
|
+
}
|
|
138
|
+
var getSelector = (event) => {
|
|
139
|
+
const immutableTarget = event.target || event.srcElement;
|
|
140
|
+
let target = event.target || event.srcElement;
|
|
141
|
+
const elements = [];
|
|
142
|
+
for (let i = 0; target && target.nodeType === Node.ELEMENT_NODE && target.nodeType !== Node.DOCUMENT_TYPE_NODE; target = target.previousSibling) {
|
|
143
|
+
if (i)
|
|
144
|
+
elements.push(target);
|
|
145
|
+
i += 1;
|
|
146
|
+
}
|
|
147
|
+
const path = typeof event.path === "undefined" ? getPath(event.target) : event.path;
|
|
148
|
+
const { outerHTML } = immutableTarget;
|
|
149
|
+
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(" > ");
|
|
150
|
+
};
|
|
151
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
152
|
+
0 && (module.exports = {
|
|
153
|
+
error,
|
|
154
|
+
getGlobal,
|
|
155
|
+
getOhbugObject,
|
|
156
|
+
getSelector,
|
|
157
|
+
isBrowser,
|
|
158
|
+
isFunction,
|
|
159
|
+
isNode,
|
|
160
|
+
isNumber,
|
|
161
|
+
isObject,
|
|
162
|
+
isPromise,
|
|
163
|
+
isString,
|
|
164
|
+
logger,
|
|
165
|
+
parseUrl,
|
|
166
|
+
replace
|
|
167
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// src/warning.ts
|
|
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
|
+
if (!condition) {
|
|
6
|
+
let argIndex = 0;
|
|
7
|
+
const message = format.replace(/%s/g, () => args[argIndex++]);
|
|
8
|
+
throw new Error(`Ohbug ${message}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// src/logger.ts
|
|
13
|
+
var logger = {
|
|
14
|
+
log(...args) {
|
|
15
|
+
console.log(...args);
|
|
16
|
+
},
|
|
17
|
+
info(...args) {
|
|
18
|
+
console.info(...args);
|
|
19
|
+
},
|
|
20
|
+
warn(...args) {
|
|
21
|
+
console.warn(...args);
|
|
22
|
+
},
|
|
23
|
+
error(...args) {
|
|
24
|
+
console.error(...args);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// src/validators.ts
|
|
29
|
+
function isString(value) {
|
|
30
|
+
return typeof value === "string";
|
|
31
|
+
}
|
|
32
|
+
function isNumber(value) {
|
|
33
|
+
return typeof value === "number" && parseInt(`${value}`, 10) === value;
|
|
34
|
+
}
|
|
35
|
+
function isFunction(value) {
|
|
36
|
+
return typeof value === "function";
|
|
37
|
+
}
|
|
38
|
+
function isObject(value) {
|
|
39
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
40
|
+
}
|
|
41
|
+
function isPromise(value) {
|
|
42
|
+
return value instanceof Promise;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/get.ts
|
|
46
|
+
var fallbackGlobalObject = {};
|
|
47
|
+
function getGlobal() {
|
|
48
|
+
return typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : fallbackGlobalObject;
|
|
49
|
+
}
|
|
50
|
+
function getOhbugObject() {
|
|
51
|
+
const global2 = getGlobal();
|
|
52
|
+
error(Boolean(global2.__OHBUG__), "Failed to get `OhbugObject`, please confirm if `Ohbug.setup`");
|
|
53
|
+
return global2.__OHBUG__;
|
|
54
|
+
}
|
|
55
|
+
function isNode() {
|
|
56
|
+
return typeof global !== "undefined";
|
|
57
|
+
}
|
|
58
|
+
function isBrowser() {
|
|
59
|
+
return typeof window !== "undefined";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/mixin.ts
|
|
63
|
+
function replace(source, name, behavior) {
|
|
64
|
+
if (!(name in source))
|
|
65
|
+
return;
|
|
66
|
+
const original = source[name];
|
|
67
|
+
const wrapped = behavior(original);
|
|
68
|
+
source[name] = wrapped;
|
|
69
|
+
return original;
|
|
70
|
+
}
|
|
71
|
+
function parseUrl(url) {
|
|
72
|
+
if (typeof url !== "string")
|
|
73
|
+
return {};
|
|
74
|
+
const match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);
|
|
75
|
+
if (!match)
|
|
76
|
+
return {};
|
|
77
|
+
const query = match[6] || "";
|
|
78
|
+
const fragment = match[8] || "";
|
|
79
|
+
return {
|
|
80
|
+
host: match[4],
|
|
81
|
+
path: match[5],
|
|
82
|
+
protocol: match[2],
|
|
83
|
+
relative: match[5] + query + fragment
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/dom.ts
|
|
88
|
+
function getParentNode(node, path) {
|
|
89
|
+
if (node.parentNode) {
|
|
90
|
+
path.push(node.parentNode);
|
|
91
|
+
getParentNode(node.parentNode, path);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function getPath(node) {
|
|
95
|
+
const path = [];
|
|
96
|
+
path.push(node);
|
|
97
|
+
getParentNode(node, path);
|
|
98
|
+
return path;
|
|
99
|
+
}
|
|
100
|
+
var getSelector = (event) => {
|
|
101
|
+
const immutableTarget = event.target || event.srcElement;
|
|
102
|
+
let target = event.target || event.srcElement;
|
|
103
|
+
const elements = [];
|
|
104
|
+
for (let i = 0; target && target.nodeType === Node.ELEMENT_NODE && target.nodeType !== Node.DOCUMENT_TYPE_NODE; target = target.previousSibling) {
|
|
105
|
+
if (i)
|
|
106
|
+
elements.push(target);
|
|
107
|
+
i += 1;
|
|
108
|
+
}
|
|
109
|
+
const path = typeof event.path === "undefined" ? getPath(event.target) : event.path;
|
|
110
|
+
const { outerHTML } = immutableTarget;
|
|
111
|
+
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(" > ");
|
|
112
|
+
};
|
|
113
|
+
export {
|
|
114
|
+
error,
|
|
115
|
+
getGlobal,
|
|
116
|
+
getOhbugObject,
|
|
117
|
+
getSelector,
|
|
118
|
+
isBrowser,
|
|
119
|
+
isFunction,
|
|
120
|
+
isNode,
|
|
121
|
+
isNumber,
|
|
122
|
+
isObject,
|
|
123
|
+
isPromise,
|
|
124
|
+
isString,
|
|
125
|
+
logger,
|
|
126
|
+
parseUrl,
|
|
127
|
+
replace
|
|
128
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohbug/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Utilities for all Ohbug SDKs",
|
|
5
|
+
"license": "Apache-2.0",
|
|
5
6
|
"author": "chenyueban <jasonchan0527@gmail.com>",
|
|
6
7
|
"homepage": "https://github.com/ohbug-org/ohbug",
|
|
7
8
|
"bugs": {
|
|
@@ -11,34 +12,31 @@
|
|
|
11
12
|
"type": "git",
|
|
12
13
|
"url": "https://github.com/ohbug-org/ohbug"
|
|
13
14
|
},
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"unpkg": "./dist/ohbug-utils.umd.js",
|
|
18
|
-
"jsdelivr": "./dist/ohbug-utils.umd.js",
|
|
19
|
-
"types": "./dist/index.d.ts",
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"module": "dist/index.mjs",
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
20
18
|
"exports": {
|
|
21
19
|
".": {
|
|
22
|
-
"
|
|
23
|
-
"
|
|
20
|
+
"require": "./dist/index.js",
|
|
21
|
+
"import": "./dist/index.mjs",
|
|
22
|
+
"types": "./dist/index.d.ts"
|
|
24
23
|
}
|
|
25
24
|
},
|
|
26
25
|
"files": [
|
|
27
|
-
"dist"
|
|
26
|
+
"dist",
|
|
27
|
+
"src"
|
|
28
28
|
],
|
|
29
|
-
"
|
|
30
|
-
"name": "OhbugUtils",
|
|
31
|
-
"formats": [
|
|
32
|
-
"es",
|
|
33
|
-
"umd"
|
|
34
|
-
],
|
|
35
|
-
"order": 1
|
|
36
|
-
},
|
|
29
|
+
"sideEffects": false,
|
|
37
30
|
"publishConfig": {
|
|
38
31
|
"access": "public"
|
|
39
32
|
},
|
|
40
33
|
"dependencies": {
|
|
41
|
-
"@ohbug/types": "
|
|
34
|
+
"@ohbug/types": "2.0.0",
|
|
35
|
+
"@types/node": "^17.0.34"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsup",
|
|
39
|
+
"dev": "tsup --watch"
|
|
42
40
|
},
|
|
43
|
-
"
|
|
44
|
-
}
|
|
41
|
+
"readme": "# `@ohbug/utils`\n\n[](https://www.npmjs.com/package/@ohbug/utils)\n[](https://bundlephobia.com/result?p=@ohbug/utils)\n\nCommon utilities used by the Ohbug.\nWarning, This package is not part of our public API contract, please do not use it\n"
|
|
42
|
+
}
|
package/src/dom.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
function getParentNode(node: Node, path: Node[]) {
|
|
2
|
+
if (node.parentNode) {
|
|
3
|
+
path.push(node.parentNode)
|
|
4
|
+
getParentNode(node.parentNode, path)
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
function getPath(node: Node) {
|
|
8
|
+
const path: Node[] = []
|
|
9
|
+
path.push(node)
|
|
10
|
+
getParentNode(node, path)
|
|
11
|
+
return path
|
|
12
|
+
}
|
|
13
|
+
export const getSelector = (event: Event) => {
|
|
14
|
+
const immutableTarget = (event.target || event.srcElement) as any
|
|
15
|
+
let target = (event.target || event.srcElement) as any
|
|
16
|
+
// 获取出错元素在同级元素的 index
|
|
17
|
+
// 储存错误元素前元素
|
|
18
|
+
const elements = []
|
|
19
|
+
for (
|
|
20
|
+
let i = 0;
|
|
21
|
+
target
|
|
22
|
+
&& target.nodeType === Node.ELEMENT_NODE
|
|
23
|
+
&& target.nodeType !== Node.DOCUMENT_TYPE_NODE;
|
|
24
|
+
target = target.previousSibling
|
|
25
|
+
) {
|
|
26
|
+
if (i) elements.push(target)
|
|
27
|
+
i += 1
|
|
28
|
+
}
|
|
29
|
+
// @ts-expect-error error.path 只有 chrome 实现,需要 polyfill
|
|
30
|
+
const path = typeof event.path === 'undefined'
|
|
31
|
+
? getPath(event.target as Node)
|
|
32
|
+
// @ts-expect-error error.path 只有 chrome 实现,需要 polyfill
|
|
33
|
+
: event.path
|
|
34
|
+
const { outerHTML } = immutableTarget
|
|
35
|
+
return path
|
|
36
|
+
.reverse()
|
|
37
|
+
.map((node: Element) =>
|
|
38
|
+
(node.localName || '')
|
|
39
|
+
+ (node.id ? `#${node.id}` : '')
|
|
40
|
+
+ (node.className ? `.${node.className}` : '')
|
|
41
|
+
+ (node.outerHTML === outerHTML ? `:nth-child(${elements.length})` : ''))
|
|
42
|
+
.filter((v: string): boolean => Boolean(v))
|
|
43
|
+
.join(' > ')
|
|
44
|
+
}
|
package/src/get.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { OhbugGlobal, OhbugObject } from '@ohbug/types'
|
|
2
|
+
import { error } from './warning'
|
|
3
|
+
|
|
4
|
+
const fallbackGlobalObject = {}
|
|
5
|
+
export function getGlobal<T = Window>(): T & OhbugGlobal {
|
|
6
|
+
return (
|
|
7
|
+
typeof window !== 'undefined'
|
|
8
|
+
? window
|
|
9
|
+
: typeof global !== 'undefined'
|
|
10
|
+
? global
|
|
11
|
+
: typeof self !== 'undefined'
|
|
12
|
+
? self
|
|
13
|
+
: fallbackGlobalObject
|
|
14
|
+
) as T & OhbugGlobal
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getOhbugObject<T = Window>(): OhbugObject {
|
|
18
|
+
const global = getGlobal<T>()
|
|
19
|
+
|
|
20
|
+
error(
|
|
21
|
+
Boolean(global.__OHBUG__),
|
|
22
|
+
'Failed to get `OhbugObject`, please confirm if `Ohbug.setup`',
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
return global.__OHBUG__
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function isNode(): boolean {
|
|
29
|
+
return typeof global !== 'undefined'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function isBrowser(): boolean {
|
|
33
|
+
return typeof window !== 'undefined'
|
|
34
|
+
}
|
package/src/index.ts
ADDED
package/src/logger.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import type { OhbugLoggerConfig } from '@ohbug/types'
|
|
3
|
+
|
|
4
|
+
export const logger: OhbugLoggerConfig = {
|
|
5
|
+
log(...args: any[]) {
|
|
6
|
+
console.log(...args)
|
|
7
|
+
},
|
|
8
|
+
|
|
9
|
+
info(...args: any[]) {
|
|
10
|
+
console.info(...args)
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
warn(...args: any[]) {
|
|
14
|
+
console.warn(...args)
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
error(...args: any[]) {
|
|
18
|
+
console.error(...args)
|
|
19
|
+
},
|
|
20
|
+
}
|
package/src/mixin.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export function replace(
|
|
2
|
+
source: any,
|
|
3
|
+
name: string,
|
|
4
|
+
behavior: (...args: any[]) => any,
|
|
5
|
+
) {
|
|
6
|
+
if (!(name in source))
|
|
7
|
+
return
|
|
8
|
+
|
|
9
|
+
const original = source[name]
|
|
10
|
+
const wrapped = behavior(original)
|
|
11
|
+
source[name] = wrapped
|
|
12
|
+
|
|
13
|
+
return original
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function parseUrl(url: string): {
|
|
17
|
+
host?: string
|
|
18
|
+
path?: string
|
|
19
|
+
protocol?: string
|
|
20
|
+
relative?: string
|
|
21
|
+
} {
|
|
22
|
+
if (typeof url !== 'string')
|
|
23
|
+
return {}
|
|
24
|
+
|
|
25
|
+
const match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/)
|
|
26
|
+
|
|
27
|
+
if (!match)
|
|
28
|
+
return {}
|
|
29
|
+
|
|
30
|
+
const query = match[6] || ''
|
|
31
|
+
const fragment = match[8] || ''
|
|
32
|
+
return {
|
|
33
|
+
host: match[4],
|
|
34
|
+
path: match[5],
|
|
35
|
+
protocol: match[2],
|
|
36
|
+
relative: match[5] + query + fragment,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function isString(value: any): value is string {
|
|
2
|
+
return typeof value === 'string'
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function isNumber(value: any): value is number {
|
|
6
|
+
return typeof value === 'number' && parseInt(`${value}`, 10) === value
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function isFunction(value: any): value is Function {
|
|
10
|
+
return typeof value === 'function'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function isObject(value: any): value is Object {
|
|
14
|
+
return Object.prototype.toString.call(value) === '[object Object]'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isPromise(value: any): value is Promise<any> {
|
|
18
|
+
return value instanceof Promise
|
|
19
|
+
}
|
package/src/warning.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function error(condition: boolean, format: string, ...args: any[]) {
|
|
2
|
+
if (format === undefined)
|
|
3
|
+
throw new Error('`Ohbug warning(condition, format, ...args)` requires a warning message argument')
|
|
4
|
+
|
|
5
|
+
if (!condition) {
|
|
6
|
+
let argIndex = 0
|
|
7
|
+
const message = format.replace(/%s/g, () => args[argIndex++])
|
|
8
|
+
throw new Error(`Ohbug ${message}`)
|
|
9
|
+
}
|
|
10
|
+
}
|
package/dist/dom.d.ts
DELETED
package/dist/dom.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dom.d.ts","sourceRoot":"","sources":["../src/dom.ts"],"names":[],"mappings":"AAYA,eAAO,MAAM,WAAW,UAAW,KAAK,QAmCvC,CAAA"}
|
package/dist/get.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { OhbugGlobal, OhbugObject } from '@ohbug/types';
|
|
2
|
-
export declare function getGlobal<T = Window>(): T & OhbugGlobal;
|
|
3
|
-
export declare function getOhbugObject<T = Window>(): OhbugObject;
|
|
4
|
-
export declare function isNode(): boolean;
|
|
5
|
-
export declare function isBrowser(): boolean;
|
|
6
|
-
//# sourceMappingURL=get.d.ts.map
|
package/dist/get.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"get.d.ts","sourceRoot":"","sources":["../src/get.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAI5D,wBAAgB,SAAS,CAAC,CAAC,GAAG,MAAM,KAAK,CAAC,GAAG,WAAW,CAUvD;AAED,wBAAgB,cAAc,CAAC,CAAC,GAAG,MAAM,KAAK,WAAW,CASxD;AAED,wBAAgB,MAAM,IAAI,OAAO,CAEhC;AAED,wBAAgB,SAAS,IAAI,OAAO,CAEnC"}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAE5B,cAAc,OAAO,CAAA;AACrB,cAAc,SAAS,CAAA;AACvB,cAAc,OAAO,CAAA"}
|
package/dist/logger.d.ts
DELETED
package/dist/logger.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAIhD,eAAO,MAAM,MAAM,EAAE,iBAkBpB,CAAA"}
|
package/dist/mixin.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export declare function replace(source: any, name: string, behavior: (...args: any[]) => any): any;
|
|
2
|
-
export declare function parseUrl(url: string): {
|
|
3
|
-
host?: string;
|
|
4
|
-
path?: string;
|
|
5
|
-
protocol?: string;
|
|
6
|
-
relative?: string;
|
|
7
|
-
};
|
|
8
|
-
//# sourceMappingURL=mixin.d.ts.map
|
package/dist/mixin.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mixin.d.ts","sourceRoot":"","sources":["../src/mixin.ts"],"names":[],"mappings":"AAAA,wBAAgB,OAAO,CACrB,MAAM,EAAE,GAAG,EACX,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,OAWlC;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG;IACrC,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAsBA"}
|
package/dist/ohbug-utils.es.js
DELETED
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
/*! *****************************************************************************
|
|
2
|
-
Copyright (c) Microsoft Corporation.
|
|
3
|
-
|
|
4
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
-
purpose with or without fee is hereby granted.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
-
***************************************************************************** */
|
|
15
|
-
|
|
16
|
-
function __spreadArray(to, from) {
|
|
17
|
-
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
18
|
-
to[j] = from[i];
|
|
19
|
-
return to;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
var prefix = 'Ohbug';
|
|
23
|
-
var logger = {
|
|
24
|
-
log: function () {
|
|
25
|
-
var args = [];
|
|
26
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
27
|
-
args[_i] = arguments[_i];
|
|
28
|
-
}
|
|
29
|
-
// eslint-disable-next-line no-console
|
|
30
|
-
console.log.apply(console, __spreadArray([prefix], args));
|
|
31
|
-
},
|
|
32
|
-
info: function () {
|
|
33
|
-
var args = [];
|
|
34
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
35
|
-
args[_i] = arguments[_i];
|
|
36
|
-
}
|
|
37
|
-
// eslint-disable-next-line no-console
|
|
38
|
-
console.info.apply(console, __spreadArray([prefix], args));
|
|
39
|
-
},
|
|
40
|
-
warn: function () {
|
|
41
|
-
var args = [];
|
|
42
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
43
|
-
args[_i] = arguments[_i];
|
|
44
|
-
}
|
|
45
|
-
console.warn.apply(console, __spreadArray([prefix], args));
|
|
46
|
-
},
|
|
47
|
-
error: function () {
|
|
48
|
-
var args = [];
|
|
49
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
50
|
-
args[_i] = arguments[_i];
|
|
51
|
-
}
|
|
52
|
-
console.error.apply(console, __spreadArray([prefix], args));
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
function error(condition, format) {
|
|
57
|
-
var args = [];
|
|
58
|
-
for (var _i = 2; _i < arguments.length; _i++) {
|
|
59
|
-
args[_i - 2] = arguments[_i];
|
|
60
|
-
}
|
|
61
|
-
if (format === undefined) {
|
|
62
|
-
throw new Error('`Ohbug warning(condition, format, ...args)` requires a warning message argument');
|
|
63
|
-
}
|
|
64
|
-
if (!condition) {
|
|
65
|
-
var argIndex_1 = 0;
|
|
66
|
-
// eslint-disable-next-line no-plusplus
|
|
67
|
-
var message = format.replace(/%s/g, function () { return args[argIndex_1++]; });
|
|
68
|
-
throw new Error("Ohbug " + message);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
function warning(condition, format) {
|
|
72
|
-
var args = [];
|
|
73
|
-
for (var _i = 2; _i < arguments.length; _i++) {
|
|
74
|
-
args[_i - 2] = arguments[_i];
|
|
75
|
-
}
|
|
76
|
-
try {
|
|
77
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
78
|
-
if (format === undefined) {
|
|
79
|
-
throw new Error('`Ohbug warning(condition, format, ...args)` requires a warning message argument');
|
|
80
|
-
}
|
|
81
|
-
if (!condition) {
|
|
82
|
-
var argIndex_2 = 0;
|
|
83
|
-
// eslint-disable-next-line no-plusplus
|
|
84
|
-
var message = format.replace(/%s/g, function () { return args[argIndex_2++]; });
|
|
85
|
-
logger.warn(message);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
// eslint-disable-next-line no-empty
|
|
89
|
-
}
|
|
90
|
-
catch (e) { }
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function isString(value) {
|
|
94
|
-
return typeof value === 'string';
|
|
95
|
-
}
|
|
96
|
-
function isNumber(value) {
|
|
97
|
-
return typeof value === 'number' && parseInt("" + value, 10) === value;
|
|
98
|
-
}
|
|
99
|
-
function isFunction(value) {
|
|
100
|
-
return typeof value === 'function';
|
|
101
|
-
}
|
|
102
|
-
function isObject(value) {
|
|
103
|
-
return Object.prototype.toString.call(value) === '[object Object]';
|
|
104
|
-
}
|
|
105
|
-
function isPromise(value) {
|
|
106
|
-
return value instanceof Promise;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
var fallbackGlobalObject = {};
|
|
110
|
-
function getGlobal() {
|
|
111
|
-
return (typeof window !== 'undefined'
|
|
112
|
-
? window
|
|
113
|
-
: typeof global !== 'undefined'
|
|
114
|
-
? global
|
|
115
|
-
: typeof self !== 'undefined'
|
|
116
|
-
? self
|
|
117
|
-
: fallbackGlobalObject);
|
|
118
|
-
}
|
|
119
|
-
function getOhbugObject() {
|
|
120
|
-
var global = getGlobal();
|
|
121
|
-
error(Boolean(global.__OHBUG__), 'Failed to get `OhbugObject`, please confirm if `Ohbug.init`');
|
|
122
|
-
return global.__OHBUG__;
|
|
123
|
-
}
|
|
124
|
-
function isNode() {
|
|
125
|
-
return typeof global !== 'undefined';
|
|
126
|
-
}
|
|
127
|
-
function isBrowser() {
|
|
128
|
-
return typeof window !== 'undefined';
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function replace(source, name, behavior) {
|
|
132
|
-
if (!(name in source)) {
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
var original = source[name];
|
|
136
|
-
var wrapped = behavior(original);
|
|
137
|
-
// eslint-disable-next-line no-param-reassign
|
|
138
|
-
source[name] = wrapped;
|
|
139
|
-
return original;
|
|
140
|
-
}
|
|
141
|
-
function parseUrl(url) {
|
|
142
|
-
if (typeof url !== 'string') {
|
|
143
|
-
return {};
|
|
144
|
-
}
|
|
145
|
-
// eslint-disable-next-line
|
|
146
|
-
var match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);
|
|
147
|
-
if (!match) {
|
|
148
|
-
return {};
|
|
149
|
-
}
|
|
150
|
-
var query = match[6] || '';
|
|
151
|
-
var fragment = match[8] || '';
|
|
152
|
-
return {
|
|
153
|
-
host: match[4],
|
|
154
|
-
path: match[5],
|
|
155
|
-
protocol: match[2],
|
|
156
|
-
relative: match[5] + query + fragment,
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function getParentNode(node, path) {
|
|
161
|
-
if (node.parentNode) {
|
|
162
|
-
path.push(node.parentNode);
|
|
163
|
-
getParentNode(node.parentNode, path);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
function getPath(node) {
|
|
167
|
-
var path = [];
|
|
168
|
-
path.push(node);
|
|
169
|
-
getParentNode(node, path);
|
|
170
|
-
return path;
|
|
171
|
-
}
|
|
172
|
-
var getSelector = function (event) {
|
|
173
|
-
var immutableTarget = (event.target || event.srcElement);
|
|
174
|
-
var target = (event.target || event.srcElement);
|
|
175
|
-
// 获取出错元素在同级元素的 index
|
|
176
|
-
// 储存错误元素前元素
|
|
177
|
-
var elements = [];
|
|
178
|
-
for (var i = 0; target &&
|
|
179
|
-
target.nodeType === Node.ELEMENT_NODE &&
|
|
180
|
-
target.nodeType !== Node.DOCUMENT_TYPE_NODE; target = target.previousSibling) {
|
|
181
|
-
if (i)
|
|
182
|
-
elements.push(target);
|
|
183
|
-
i += 1;
|
|
184
|
-
}
|
|
185
|
-
// error.path 只有 chrome 实现,需要 polyfill
|
|
186
|
-
var path =
|
|
187
|
-
// @ts-ignore
|
|
188
|
-
typeof event.path === 'undefined'
|
|
189
|
-
? getPath(event.target)
|
|
190
|
-
: // @ts-ignore
|
|
191
|
-
event.path;
|
|
192
|
-
var outerHTML = immutableTarget.outerHTML;
|
|
193
|
-
return path
|
|
194
|
-
.reverse()
|
|
195
|
-
.map(function (node) {
|
|
196
|
-
return (node.localName || '') +
|
|
197
|
-
(node.id ? "#" + node.id : '') +
|
|
198
|
-
(node.className ? "." + node.className : '') +
|
|
199
|
-
(node.outerHTML === outerHTML ? ":nth-child(" + elements.length + ")" : '');
|
|
200
|
-
})
|
|
201
|
-
.filter(function (v) { return Boolean(v); })
|
|
202
|
-
.join(' > ');
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
export { error, getGlobal, getOhbugObject, getSelector, isBrowser, isFunction, isNode, isNumber, isObject, isPromise, isString, logger, parseUrl, replace, warning };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/*! *****************************************************************************
|
|
2
|
-
Copyright (c) Microsoft Corporation.
|
|
3
|
-
|
|
4
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
-
purpose with or without fee is hereby granted.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
-
***************************************************************************** */
|
|
15
|
-
function n(n,r){for(var e=0,o=r.length,t=n.length;e<o;e++,t++)n[t]=r[e];return n}var r={log:function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];console.log.apply(console,n(["Ohbug"],r))},info:function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];console.info.apply(console,n(["Ohbug"],r))},warn:function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];console.warn.apply(console,n(["Ohbug"],r))},error:function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];console.error.apply(console,n(["Ohbug"],r))}};function e(n,r){for(var e=[],o=2;o<arguments.length;o++)e[o-2]=arguments[o];if(void 0===r)throw new Error("`Ohbug warning(condition, format, ...args)` requires a warning message argument");if(!n){var t=0,i=r.replace(/%s/g,(function(){return e[t++]}));throw new Error("Ohbug "+i)}}function o(n,e){for(var o=[],t=2;t<arguments.length;t++)o[t-2]=arguments[t];try{if("production"!==process.env.NODE_ENV){if(void 0===e)throw new Error("`Ohbug warning(condition, format, ...args)` requires a warning message argument");if(!n){var i=0,u=e.replace(/%s/g,(function(){return o[i++]}));r.warn(u)}}}catch(n){}}function t(n){return"string"==typeof n}function i(n){return"number"==typeof n&&parseInt(""+n,10)===n}function u(n){return"function"==typeof n}function a(n){return"[object Object]"===Object.prototype.toString.call(n)}function f(n){return n instanceof Promise}var c={};function l(){return"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:c}function g(){var n=l();return e(Boolean(n.__OHBUG__),"Failed to get `OhbugObject`, please confirm if `Ohbug.init`"),n.__OHBUG__}function p(){return"undefined"!=typeof global}function s(){return"undefined"!=typeof window}function h(n,r,e){if(r in n){var o=n[r],t=e(o);return n[r]=t,o}}function d(n){if("string"!=typeof n)return{};var r=n.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);if(!r)return{};var e=r[6]||"",o=r[8]||"";return{host:r[4],path:r[5],protocol:r[2],relative:r[5]+e+o}}function v(n,r){n.parentNode&&(r.push(n.parentNode),v(n.parentNode,r))}function w(n){var r=[];return r.push(n),v(n,r),r}var b=function(n){for(var r=n.target||n.srcElement,e=n.target||n.srcElement,o=[],t=0;e&&e.nodeType===Node.ELEMENT_NODE&&e.nodeType!==Node.DOCUMENT_TYPE_NODE;e=e.previousSibling)t&&o.push(e),t+=1;var i=void 0===n.path?w(n.target):n.path,u=r.outerHTML;return i.reverse().map((function(n){return(n.localName||"")+(n.id?"#"+n.id:"")+(n.className?"."+n.className:"")+(n.outerHTML===u?":nth-child("+o.length+")":"")})).filter((function(n){return Boolean(n)})).join(" > ")};export{e as error,l as getGlobal,g as getOhbugObject,b as getSelector,s as isBrowser,u as isFunction,p as isNode,i as isNumber,a as isObject,f as isPromise,t as isString,r as logger,d as parseUrl,h as replace,o as warning};
|
package/dist/ohbug-utils.umd.js
DELETED
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.OhbugUtils = {}));
|
|
5
|
-
}(this, (function (exports) { 'use strict';
|
|
6
|
-
|
|
7
|
-
/*! *****************************************************************************
|
|
8
|
-
Copyright (c) Microsoft Corporation.
|
|
9
|
-
|
|
10
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
-
purpose with or without fee is hereby granted.
|
|
12
|
-
|
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
-
***************************************************************************** */
|
|
21
|
-
|
|
22
|
-
function __spreadArray(to, from) {
|
|
23
|
-
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
24
|
-
to[j] = from[i];
|
|
25
|
-
return to;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
var prefix = 'Ohbug';
|
|
29
|
-
var logger = {
|
|
30
|
-
log: function () {
|
|
31
|
-
var args = [];
|
|
32
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
33
|
-
args[_i] = arguments[_i];
|
|
34
|
-
}
|
|
35
|
-
// eslint-disable-next-line no-console
|
|
36
|
-
console.log.apply(console, __spreadArray([prefix], args));
|
|
37
|
-
},
|
|
38
|
-
info: function () {
|
|
39
|
-
var args = [];
|
|
40
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
41
|
-
args[_i] = arguments[_i];
|
|
42
|
-
}
|
|
43
|
-
// eslint-disable-next-line no-console
|
|
44
|
-
console.info.apply(console, __spreadArray([prefix], args));
|
|
45
|
-
},
|
|
46
|
-
warn: function () {
|
|
47
|
-
var args = [];
|
|
48
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
49
|
-
args[_i] = arguments[_i];
|
|
50
|
-
}
|
|
51
|
-
console.warn.apply(console, __spreadArray([prefix], args));
|
|
52
|
-
},
|
|
53
|
-
error: function () {
|
|
54
|
-
var args = [];
|
|
55
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
56
|
-
args[_i] = arguments[_i];
|
|
57
|
-
}
|
|
58
|
-
console.error.apply(console, __spreadArray([prefix], args));
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
function error(condition, format) {
|
|
63
|
-
var args = [];
|
|
64
|
-
for (var _i = 2; _i < arguments.length; _i++) {
|
|
65
|
-
args[_i - 2] = arguments[_i];
|
|
66
|
-
}
|
|
67
|
-
if (format === undefined) {
|
|
68
|
-
throw new Error('`Ohbug warning(condition, format, ...args)` requires a warning message argument');
|
|
69
|
-
}
|
|
70
|
-
if (!condition) {
|
|
71
|
-
var argIndex_1 = 0;
|
|
72
|
-
// eslint-disable-next-line no-plusplus
|
|
73
|
-
var message = format.replace(/%s/g, function () { return args[argIndex_1++]; });
|
|
74
|
-
throw new Error("Ohbug " + message);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
function warning(condition, format) {
|
|
78
|
-
var args = [];
|
|
79
|
-
for (var _i = 2; _i < arguments.length; _i++) {
|
|
80
|
-
args[_i - 2] = arguments[_i];
|
|
81
|
-
}
|
|
82
|
-
try {
|
|
83
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
84
|
-
if (format === undefined) {
|
|
85
|
-
throw new Error('`Ohbug warning(condition, format, ...args)` requires a warning message argument');
|
|
86
|
-
}
|
|
87
|
-
if (!condition) {
|
|
88
|
-
var argIndex_2 = 0;
|
|
89
|
-
// eslint-disable-next-line no-plusplus
|
|
90
|
-
var message = format.replace(/%s/g, function () { return args[argIndex_2++]; });
|
|
91
|
-
logger.warn(message);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
// eslint-disable-next-line no-empty
|
|
95
|
-
}
|
|
96
|
-
catch (e) { }
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function isString(value) {
|
|
100
|
-
return typeof value === 'string';
|
|
101
|
-
}
|
|
102
|
-
function isNumber(value) {
|
|
103
|
-
return typeof value === 'number' && parseInt("" + value, 10) === value;
|
|
104
|
-
}
|
|
105
|
-
function isFunction(value) {
|
|
106
|
-
return typeof value === 'function';
|
|
107
|
-
}
|
|
108
|
-
function isObject(value) {
|
|
109
|
-
return Object.prototype.toString.call(value) === '[object Object]';
|
|
110
|
-
}
|
|
111
|
-
function isPromise(value) {
|
|
112
|
-
return value instanceof Promise;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
var fallbackGlobalObject = {};
|
|
116
|
-
function getGlobal() {
|
|
117
|
-
return (typeof window !== 'undefined'
|
|
118
|
-
? window
|
|
119
|
-
: typeof global !== 'undefined'
|
|
120
|
-
? global
|
|
121
|
-
: typeof self !== 'undefined'
|
|
122
|
-
? self
|
|
123
|
-
: fallbackGlobalObject);
|
|
124
|
-
}
|
|
125
|
-
function getOhbugObject() {
|
|
126
|
-
var global = getGlobal();
|
|
127
|
-
error(Boolean(global.__OHBUG__), 'Failed to get `OhbugObject`, please confirm if `Ohbug.init`');
|
|
128
|
-
return global.__OHBUG__;
|
|
129
|
-
}
|
|
130
|
-
function isNode() {
|
|
131
|
-
return typeof global !== 'undefined';
|
|
132
|
-
}
|
|
133
|
-
function isBrowser() {
|
|
134
|
-
return typeof window !== 'undefined';
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function replace(source, name, behavior) {
|
|
138
|
-
if (!(name in source)) {
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
var original = source[name];
|
|
142
|
-
var wrapped = behavior(original);
|
|
143
|
-
// eslint-disable-next-line no-param-reassign
|
|
144
|
-
source[name] = wrapped;
|
|
145
|
-
return original;
|
|
146
|
-
}
|
|
147
|
-
function parseUrl(url) {
|
|
148
|
-
if (typeof url !== 'string') {
|
|
149
|
-
return {};
|
|
150
|
-
}
|
|
151
|
-
// eslint-disable-next-line
|
|
152
|
-
var match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);
|
|
153
|
-
if (!match) {
|
|
154
|
-
return {};
|
|
155
|
-
}
|
|
156
|
-
var query = match[6] || '';
|
|
157
|
-
var fragment = match[8] || '';
|
|
158
|
-
return {
|
|
159
|
-
host: match[4],
|
|
160
|
-
path: match[5],
|
|
161
|
-
protocol: match[2],
|
|
162
|
-
relative: match[5] + query + fragment,
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
function getParentNode(node, path) {
|
|
167
|
-
if (node.parentNode) {
|
|
168
|
-
path.push(node.parentNode);
|
|
169
|
-
getParentNode(node.parentNode, path);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
function getPath(node) {
|
|
173
|
-
var path = [];
|
|
174
|
-
path.push(node);
|
|
175
|
-
getParentNode(node, path);
|
|
176
|
-
return path;
|
|
177
|
-
}
|
|
178
|
-
var getSelector = function (event) {
|
|
179
|
-
var immutableTarget = (event.target || event.srcElement);
|
|
180
|
-
var target = (event.target || event.srcElement);
|
|
181
|
-
// 获取出错元素在同级元素的 index
|
|
182
|
-
// 储存错误元素前元素
|
|
183
|
-
var elements = [];
|
|
184
|
-
for (var i = 0; target &&
|
|
185
|
-
target.nodeType === Node.ELEMENT_NODE &&
|
|
186
|
-
target.nodeType !== Node.DOCUMENT_TYPE_NODE; target = target.previousSibling) {
|
|
187
|
-
if (i)
|
|
188
|
-
elements.push(target);
|
|
189
|
-
i += 1;
|
|
190
|
-
}
|
|
191
|
-
// error.path 只有 chrome 实现,需要 polyfill
|
|
192
|
-
var path =
|
|
193
|
-
// @ts-ignore
|
|
194
|
-
typeof event.path === 'undefined'
|
|
195
|
-
? getPath(event.target)
|
|
196
|
-
: // @ts-ignore
|
|
197
|
-
event.path;
|
|
198
|
-
var outerHTML = immutableTarget.outerHTML;
|
|
199
|
-
return path
|
|
200
|
-
.reverse()
|
|
201
|
-
.map(function (node) {
|
|
202
|
-
return (node.localName || '') +
|
|
203
|
-
(node.id ? "#" + node.id : '') +
|
|
204
|
-
(node.className ? "." + node.className : '') +
|
|
205
|
-
(node.outerHTML === outerHTML ? ":nth-child(" + elements.length + ")" : '');
|
|
206
|
-
})
|
|
207
|
-
.filter(function (v) { return Boolean(v); })
|
|
208
|
-
.join(' > ');
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
exports.error = error;
|
|
212
|
-
exports.getGlobal = getGlobal;
|
|
213
|
-
exports.getOhbugObject = getOhbugObject;
|
|
214
|
-
exports.getSelector = getSelector;
|
|
215
|
-
exports.isBrowser = isBrowser;
|
|
216
|
-
exports.isFunction = isFunction;
|
|
217
|
-
exports.isNode = isNode;
|
|
218
|
-
exports.isNumber = isNumber;
|
|
219
|
-
exports.isObject = isObject;
|
|
220
|
-
exports.isPromise = isPromise;
|
|
221
|
-
exports.isString = isString;
|
|
222
|
-
exports.logger = logger;
|
|
223
|
-
exports.parseUrl = parseUrl;
|
|
224
|
-
exports.replace = replace;
|
|
225
|
-
exports.warning = warning;
|
|
226
|
-
|
|
227
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
228
|
-
|
|
229
|
-
})));
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).OhbugUtils={})}(this,(function(e){"use strict";
|
|
2
|
-
/*! *****************************************************************************
|
|
3
|
-
Copyright (c) Microsoft Corporation.
|
|
4
|
-
|
|
5
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
-
purpose with or without fee is hereby granted.
|
|
7
|
-
|
|
8
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
9
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
10
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
11
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
12
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
13
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
14
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
-
***************************************************************************** */function n(e,n){for(var r=0,o=n.length,t=e.length;r<o;r++,t++)e[t]=n[r];return e}var r="Ohbug",o={log:function(){for(var e=[],o=0;o<arguments.length;o++)e[o]=arguments[o];console.log.apply(console,n([r],e))},info:function(){for(var e=[],o=0;o<arguments.length;o++)e[o]=arguments[o];console.info.apply(console,n([r],e))},warn:function(){for(var e=[],o=0;o<arguments.length;o++)e[o]=arguments[o];console.warn.apply(console,n([r],e))},error:function(){for(var e=[],o=0;o<arguments.length;o++)e[o]=arguments[o];console.error.apply(console,n([r],e))}};function t(e,n){for(var r=[],o=2;o<arguments.length;o++)r[o-2]=arguments[o];if(void 0===n)throw new Error("`Ohbug warning(condition, format, ...args)` requires a warning message argument");if(!e){var t=0,i=n.replace(/%s/g,(function(){return r[t++]}));throw new Error("Ohbug "+i)}}var i={};function u(){return"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:i}function f(e,n){e.parentNode&&(n.push(e.parentNode),f(e.parentNode,n))}function a(e){var n=[];return n.push(e),f(e,n),n}e.error=t,e.getGlobal=u,e.getOhbugObject=function(){var e=u();return t(Boolean(e.__OHBUG__),"Failed to get `OhbugObject`, please confirm if `Ohbug.init`"),e.__OHBUG__},e.getSelector=function(e){for(var n=e.target||e.srcElement,r=e.target||e.srcElement,o=[],t=0;r&&r.nodeType===Node.ELEMENT_NODE&&r.nodeType!==Node.DOCUMENT_TYPE_NODE;r=r.previousSibling)t&&o.push(r),t+=1;var i=void 0===e.path?a(e.target):e.path,u=n.outerHTML;return i.reverse().map((function(e){return(e.localName||"")+(e.id?"#"+e.id:"")+(e.className?"."+e.className:"")+(e.outerHTML===u?":nth-child("+o.length+")":"")})).filter((function(e){return Boolean(e)})).join(" > ")},e.isBrowser=function(){return"undefined"!=typeof window},e.isFunction=function(e){return"function"==typeof e},e.isNode=function(){return"undefined"!=typeof global},e.isNumber=function(e){return"number"==typeof e&&parseInt(""+e,10)===e},e.isObject=function(e){return"[object Object]"===Object.prototype.toString.call(e)},e.isPromise=function(e){return e instanceof Promise},e.isString=function(e){return"string"==typeof e},e.logger=o,e.parseUrl=function(e){if("string"!=typeof e)return{};var n=e.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);if(!n)return{};var r=n[6]||"",o=n[8]||"";return{host:n[4],path:n[5],protocol:n[2],relative:n[5]+r+o}},e.replace=function(e,n,r){if(n in e){var o=e[n],t=r(o);return e[n]=t,o}},e.warning=function(e,n){for(var r=[],t=2;t<arguments.length;t++)r[t-2]=arguments[t];try{if("production"!==process.env.NODE_ENV){if(void 0===n)throw new Error("`Ohbug warning(condition, format, ...args)` requires a warning message argument");if(!e){var i=0,u=n.replace(/%s/g,(function(){return r[i++]}));o.warn(u)}}}catch(e){}},Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/dist/validators.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export declare function isString(value: any): value is string;
|
|
2
|
-
export declare function isNumber(value: any): value is number;
|
|
3
|
-
export declare function isFunction(value: any): value is Function;
|
|
4
|
-
export declare function isObject(value: any): value is Object;
|
|
5
|
-
export declare function isPromise(value: any): value is Promise<any>;
|
|
6
|
-
//# sourceMappingURL=validators.d.ts.map
|
package/dist/validators.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../src/validators.ts"],"names":[],"mappings":"AAAA,wBAAgB,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,MAAM,CAEpD;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,MAAM,CAEpD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,QAAQ,CAExD;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,MAAM,CAEpD;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC,CAE3D"}
|
package/dist/warning.d.ts
DELETED
package/dist/warning.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"warning.d.ts","sourceRoot":"","sources":["../src/warning.ts"],"names":[],"mappings":"AAEA,wBAAgB,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,QAavE;AAED,wBAAgB,OAAO,CAAC,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,QAkBzE"}
|