@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 CHANGED
@@ -1 +1,175 @@
1
- var s=Object.defineProperty;var c=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var y=(e,t)=>{for(var o in t)s(e,o,{get:t[o],enumerable:!0})},m=(e,t,o,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of f(t))!b.call(e,n)&&n!==o&&s(e,n,{get:()=>t[n],enumerable:!(r=c(t,n))||r.enumerable});return e};var d=e=>m(s({},"__esModule",{value:!0}),e);var H={};y(H,{error:()=>u,getGlobal:()=>l,getOhbugObject:()=>j,getSelector:()=>L,isBrowser:()=>G,isFunction:()=>N,isNode:()=>_,isNumber:()=>x,isObject:()=>w,isPromise:()=>E,isString:()=>O,logger:()=>h,parseUrl:()=>$,replace:()=>P});module.exports=d(H);function u(e,t,...o){if(t===void 0)throw new Error("`Ohbug warning(condition, format, ...args)` requires a warning message argument");if(!e){let r=0,n=t.replace(/%s/g,()=>o[r++]);throw new Error(`Ohbug ${n}`)}}var h={log(...e){console.log(...e)},info(...e){console.info(...e)},warn(...e){console.warn(...e)},error(...e){console.error(...e)}};function O(e){return typeof e=="string"}function x(e){return typeof e=="number"&&parseInt(`${e}`,10)===e}function N(e){return typeof e=="function"}function w(e){return Object.prototype.toString.call(e)==="[object Object]"}function E(e){return e instanceof Promise}var T={};function l(){return typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:T}function j(){let e=l();return u(Boolean(e.__OHBUG__),"Failed to get `OhbugObject`, please confirm if `Ohbug.setup`"),e.__OHBUG__}function _(){return typeof global<"u"}function G(){return typeof window<"u"}function P(e,t,o){if(!(t in e))return;let r=e[t],n=o(r);return e[t]=n,r}function $(e){if(typeof e!="string")return{};let t=e.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);if(!t)return{};let o=t[6]||"",r=t[8]||"";return{host:t[4],path:t[5],protocol:t[2],relative:t[5]+o+r}}function g(e,t){e.parentNode&&(t.push(e.parentNode),g(e.parentNode,t))}function B(e){let t=[];return t.push(e),g(e,t),t}var L=e=>{let t=e.target||e.srcElement,o=e.target||e.srcElement,r=[];for(let i=0;o&&o.nodeType===Node.ELEMENT_NODE&&o.nodeType!==Node.DOCUMENT_TYPE_NODE;o=o.previousSibling)i&&r.push(o),i+=1;let n=typeof e.path>"u"?B(e.target):e.path,{outerHTML:p}=t;return n.reverse().map(i=>(i.localName||"")+(i.id?`#${i.id}`:"")+(i.className?`.${i.className}`:"")+(i.outerHTML===p?`:nth-child(${r.length})`:"")).filter(i=>Boolean(i)).join(" > ")};0&&(module.exports={error,getGlobal,getOhbugObject,getSelector,isBrowser,isFunction,isNode,isNumber,isObject,isPromise,isString,logger,parseUrl,replace});
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
- function u(e,t,...o){if(t===void 0)throw new Error("`Ohbug warning(condition, format, ...args)` requires a warning message argument");if(!e){let n=0,s=t.replace(/%s/g,()=>o[n++]);throw new Error(`Ohbug ${s}`)}}var y={log(...e){console.log(...e)},info(...e){console.info(...e)},warn(...e){console.warn(...e)},error(...e){console.error(...e)}};function d(e){return typeof e=="string"}function h(e){return typeof e=="number"&&parseInt(`${e}`,10)===e}function O(e){return typeof e=="function"}function x(e){return Object.prototype.toString.call(e)==="[object Object]"}function N(e){return e instanceof Promise}var p={};function c(){return typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:p}function T(){let e=c();return u(Boolean(e.__OHBUG__),"Failed to get `OhbugObject`, please confirm if `Ohbug.setup`"),e.__OHBUG__}function j(){return typeof global<"u"}function _(){return typeof window<"u"}function P(e,t,o){if(!(t in e))return;let n=e[t],s=o(n);return e[t]=s,n}function $(e){if(typeof e!="string")return{};let t=e.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);if(!t)return{};let o=t[6]||"",n=t[8]||"";return{host:t[4],path:t[5],protocol:t[2],relative:t[5]+o+n}}function l(e,t){e.parentNode&&(t.push(e.parentNode),l(e.parentNode,t))}function f(e){let t=[];return t.push(e),l(e,t),t}var L=e=>{let t=e.target||e.srcElement,o=e.target||e.srcElement,n=[];for(let r=0;o&&o.nodeType===Node.ELEMENT_NODE&&o.nodeType!==Node.DOCUMENT_TYPE_NODE;o=o.previousSibling)r&&n.push(o),r+=1;let s=typeof e.path>"u"?f(e.target):e.path,{outerHTML:g}=t;return s.reverse().map(r=>(r.localName||"")+(r.id?`#${r.id}`:"")+(r.className?`.${r.className}`:"")+(r.outerHTML===g?`:nth-child(${n.length})`:"")).filter(r=>Boolean(r)).join(" > ")};export{u as error,c as getGlobal,T as getOhbugObject,L as getSelector,_ as isBrowser,O as isFunction,j as isNode,h as isNumber,x as isObject,N as isPromise,d as isString,y as logger,$ as parseUrl,P as replace};
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",
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.0.3",
34
- "@types/node": "^17.0.34"
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[![npm](https://img.shields.io/npm/v/@ohbug/utils.svg?style=flat-square)](https://www.npmjs.com/package/@ohbug/utils)\n[![npm bundle size](https://img.shields.io/bundlephobia/min/@ohbug/utils?style=flat-square)](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
  }