@ohbug/utils 2.0.0 → 2.0.1

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,167 +1 @@
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
- });
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});
package/dist/index.mjs CHANGED
@@ -1,128 +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
- 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
- };
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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ohbug/utils",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Utilities for all Ohbug SDKs",
5
5
  "license": "Apache-2.0",
6
6
  "author": "chenyueban <jasonchan0527@gmail.com>",
@@ -23,20 +23,18 @@
23
23
  }
24
24
  },
25
25
  "files": [
26
- "dist",
27
- "src"
26
+ "dist"
28
27
  ],
29
28
  "sideEffects": false,
30
29
  "publishConfig": {
31
30
  "access": "public"
32
31
  },
33
32
  "dependencies": {
34
- "@ohbug/types": "2.0.0",
33
+ "@ohbug/types": "2.0.1",
35
34
  "@types/node": "^17.0.34"
36
35
  },
37
36
  "scripts": {
38
37
  "build": "tsup",
39
38
  "dev": "tsup --watch"
40
- },
41
- "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
+ }
42
40
  }
package/src/dom.ts DELETED
@@ -1,44 +0,0 @@
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 DELETED
@@ -1,34 +0,0 @@
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 DELETED
@@ -1,7 +0,0 @@
1
- export * from './warning'
2
- export * from './logger'
3
- export * from './validators'
4
-
5
- export * from './get'
6
- export * from './mixin'
7
- export * from './dom'
package/src/logger.ts DELETED
@@ -1,20 +0,0 @@
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 DELETED
@@ -1,38 +0,0 @@
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
- }
package/src/validators.ts DELETED
@@ -1,19 +0,0 @@
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 DELETED
@@ -1,10 +0,0 @@
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
- }