@ohbug/utils 2.0.3 → 2.0.6
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.js +175 -1
- package/dist/index.mjs +135 -1
- package/package.json +4 -5
package/dist/index.js
CHANGED
|
@@ -1 +1,175 @@
|
|
|
1
|
-
|
|
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
|
+
});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,135 @@
|
|
|
1
|
-
|
|
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
|
+
}
|
|
6
|
+
if (!condition) {
|
|
7
|
+
let argIndex = 0;
|
|
8
|
+
const message = format.replace(/%s/g, () => args[argIndex++]);
|
|
9
|
+
throw new Error(`Ohbug ${message}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
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
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// src/validators.ts
|
|
30
|
+
function isString(value) {
|
|
31
|
+
return typeof value === "string";
|
|
32
|
+
}
|
|
33
|
+
function isNumber(value) {
|
|
34
|
+
return typeof value === "number" && parseInt(`${value}`, 10) === value;
|
|
35
|
+
}
|
|
36
|
+
function isFunction(value) {
|
|
37
|
+
return typeof value === "function";
|
|
38
|
+
}
|
|
39
|
+
function isObject(value) {
|
|
40
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
41
|
+
}
|
|
42
|
+
function isPromise(value) {
|
|
43
|
+
return value instanceof Promise;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/get.ts
|
|
47
|
+
var fallbackGlobalObject = {};
|
|
48
|
+
function getGlobal() {
|
|
49
|
+
return typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : fallbackGlobalObject;
|
|
50
|
+
}
|
|
51
|
+
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__;
|
|
58
|
+
}
|
|
59
|
+
function isNode() {
|
|
60
|
+
return typeof global !== "undefined";
|
|
61
|
+
}
|
|
62
|
+
function isBrowser() {
|
|
63
|
+
return typeof window !== "undefined";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/mixin.ts
|
|
67
|
+
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;
|
|
75
|
+
}
|
|
76
|
+
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
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/dom.ts
|
|
95
|
+
function getParentNode(node, path) {
|
|
96
|
+
if (node.parentNode) {
|
|
97
|
+
path.push(node.parentNode);
|
|
98
|
+
getParentNode(node.parentNode, path);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function getPath(node) {
|
|
102
|
+
const path = [];
|
|
103
|
+
path.push(node);
|
|
104
|
+
getParentNode(node, path);
|
|
105
|
+
return path;
|
|
106
|
+
}
|
|
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(" > ");
|
|
119
|
+
};
|
|
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
|
|
135
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohbug/utils",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"description": "Utilities for all Ohbug SDKs",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "chenyueban <jasonchan0527@gmail.com>",
|
|
@@ -30,12 +30,11 @@
|
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@ohbug/types": "2.
|
|
34
|
-
"@types/node": "^
|
|
33
|
+
"@ohbug/types": "2.1.1",
|
|
34
|
+
"@types/node": "^18.6.3"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "tsup",
|
|
38
38
|
"dev": "tsup --watch"
|
|
39
|
-
}
|
|
40
|
-
"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"
|
|
39
|
+
}
|
|
41
40
|
}
|