@ohbug/utils 1.0.11 → 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 +22 -19
- 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.cjs.js +0 -223
- package/dist/ohbug-utils.esm.js +0 -205
- package/dist/ohbug-utils.esm.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,29 +12,31 @@
|
|
|
11
12
|
"type": "git",
|
|
12
13
|
"url": "https://github.com/ohbug-org/ohbug"
|
|
13
14
|
},
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"module": "dist/ohbug-utils.esm.js",
|
|
17
|
-
"unpkg": "dist/ohbug-utils.umd.js",
|
|
18
|
-
"jsdelivr": "dist/ohbug-utils.umd.js",
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"module": "dist/index.mjs",
|
|
19
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
|
+
}
|
|
24
|
+
},
|
|
20
25
|
"files": [
|
|
21
|
-
"dist"
|
|
26
|
+
"dist",
|
|
27
|
+
"src"
|
|
22
28
|
],
|
|
23
|
-
"
|
|
24
|
-
"name": "OhbugUtils",
|
|
25
|
-
"formats": [
|
|
26
|
-
"esm",
|
|
27
|
-
"umd",
|
|
28
|
-
"cjs"
|
|
29
|
-
],
|
|
30
|
-
"order": 1
|
|
31
|
-
},
|
|
29
|
+
"sideEffects": false,
|
|
32
30
|
"publishConfig": {
|
|
33
31
|
"access": "public"
|
|
34
32
|
},
|
|
35
33
|
"dependencies": {
|
|
36
|
-
"@ohbug/types": "
|
|
34
|
+
"@ohbug/types": "2.0.0",
|
|
35
|
+
"@types/node": "^17.0.34"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsup",
|
|
39
|
+
"dev": "tsup --watch"
|
|
37
40
|
},
|
|
38
|
-
"
|
|
39
|
-
}
|
|
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,CAQvD;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,CACtB,GAAG,EAAE,MAAM,GACV;IACD,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"}
|