@emqx/shared-ui-utils 0.0.7 → 0.0.9
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/format.d.ts +7 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +53 -20
- package/dist/index.umd.cjs +1 -1
- package/dist/jsonUtils.d.ts +6 -0
- package/dist/objectUtils.d.ts +6 -0
- package/package.json +1 -1
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a size unit value into a human-readable string representation.
|
|
3
|
+
* @param val - The size unit value to format.
|
|
4
|
+
* @returns A string representing the formatted size unit value.
|
|
5
|
+
* @throws {Error} If the input is not a non-negative number.
|
|
6
|
+
*/
|
|
7
|
+
export declare const formatSizeUnit: (val: number) => string;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,27 +1,60 @@
|
|
|
1
|
-
const
|
|
2
|
-
if (typeof
|
|
3
|
-
for (const
|
|
4
|
-
|
|
1
|
+
const i = (r, t = [], e = {}) => {
|
|
2
|
+
if (typeof r == "object" && !Array.isArray(r) && r !== null)
|
|
3
|
+
for (const n of Object.keys(r))
|
|
4
|
+
i(r[n], t.concat(n), e);
|
|
5
5
|
else
|
|
6
|
-
t
|
|
7
|
-
return
|
|
8
|
-
},
|
|
9
|
-
if (Object(
|
|
10
|
-
return
|
|
11
|
-
const
|
|
6
|
+
e[t.join(".")] = r;
|
|
7
|
+
return e;
|
|
8
|
+
}, f = (r) => {
|
|
9
|
+
if (Object(r) !== r && !Array.isArray(r))
|
|
10
|
+
return r;
|
|
11
|
+
const t = /\.?([^.[\]]+)|\[(\d+)\]/g, e = {};
|
|
12
12
|
try {
|
|
13
|
-
for (const
|
|
14
|
-
let
|
|
15
|
-
for (;
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
for (const n in r) {
|
|
14
|
+
let o = e, s = "", c;
|
|
15
|
+
for (; c = t.exec(n); )
|
|
16
|
+
o = o[s] || (o[s] = c[2] ? [] : {}), s = c[2] || c[1];
|
|
17
|
+
o[s] = r[n];
|
|
18
18
|
}
|
|
19
|
-
} catch (
|
|
20
|
-
console.error(
|
|
19
|
+
} catch (n) {
|
|
20
|
+
console.error(n);
|
|
21
21
|
}
|
|
22
|
-
return
|
|
22
|
+
return e[""] || e;
|
|
23
|
+
}, l = (r) => Object.keys(r).length === 0, y = (r) => {
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(r);
|
|
26
|
+
} catch {
|
|
27
|
+
console.error("An error occurred while parsing the JSON string");
|
|
28
|
+
}
|
|
29
|
+
}, u = (r, t) => {
|
|
30
|
+
try {
|
|
31
|
+
return typeof r == "string" ? r : JSON.stringify(r, null, t);
|
|
32
|
+
} catch (e) {
|
|
33
|
+
return console.error(e), "stringify error";
|
|
34
|
+
}
|
|
35
|
+
}, a = (r) => {
|
|
36
|
+
if (typeof r != "string")
|
|
37
|
+
return !1;
|
|
38
|
+
try {
|
|
39
|
+
const t = JSON.parse(r);
|
|
40
|
+
return typeof t == "object" && t !== null;
|
|
41
|
+
} catch {
|
|
42
|
+
return !1;
|
|
43
|
+
}
|
|
44
|
+
}, p = (r) => {
|
|
45
|
+
if (typeof r != "number" || isNaN(r) || r < 0)
|
|
46
|
+
throw new Error("Invalid input: input should be a non-negative number");
|
|
47
|
+
if (r === 0)
|
|
48
|
+
return "0 Bytes";
|
|
49
|
+
const t = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"], e = Math.floor(Math.log(r) / Math.log(1024));
|
|
50
|
+
return `${(r / 1024 ** e).toFixed(1)} ${t[e]}`;
|
|
23
51
|
};
|
|
24
52
|
export {
|
|
25
|
-
|
|
26
|
-
|
|
53
|
+
i as flattenObject,
|
|
54
|
+
p as formatSizeUnit,
|
|
55
|
+
l as isEmptyObj,
|
|
56
|
+
a as isJSONString,
|
|
57
|
+
y as parseJSONSafely,
|
|
58
|
+
u as stringifyObjSafely,
|
|
59
|
+
f as unflattenObject
|
|
27
60
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(r,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(r=typeof globalThis<"u"?globalThis:r||self,o(r["@emqx/shared-ui-utils"]={}))})(this,function(r){"use strict";const o=(e,t=[],n={})=>{if(typeof e=="object"&&!Array.isArray(e)&&e!==null)for(const i of Object.keys(e))o(e[i],t.concat(i),n);else n[t.join(".")]=e;return n},u=e=>{if(Object(e)!==e&&!Array.isArray(e))return e;const t=/\.?([^.[\]]+)|\[(\d+)\]/g,n={};try{for(const i in e){let s=n,c="",f;for(;f=t.exec(i);)s=s[c]||(s[c]=f[2]?[]:{}),c=f[2]||f[1];s[c]=e[i]}}catch(i){console.error(i)}return n[""]||n},l=e=>Object.keys(e).length===0,y=e=>{try{return JSON.parse(e)}catch{console.error("An error occurred while parsing the JSON string")}},a=(e,t)=>{try{return typeof e=="string"?e:JSON.stringify(e,null,t)}catch(n){return console.error(n),"stringify error"}},O=e=>{if(typeof e!="string")return!1;try{const t=JSON.parse(e);return typeof t=="object"&&t!==null}catch{return!1}},d=e=>{if(typeof e!="number"||isNaN(e)||e<0)throw new Error("Invalid input: input should be a non-negative number");if(e===0)return"0 Bytes";const t=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],n=Math.floor(Math.log(e)/Math.log(1024));return`${(e/1024**n).toFixed(1)} ${t[n]}`};r.flattenObject=o,r.formatSizeUnit=d,r.isEmptyObj=l,r.isJSONString=O,r.parseJSONSafely=y,r.stringifyObjSafely=a,r.unflattenObject=u,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
package/dist/jsonUtils.d.ts
CHANGED
|
@@ -11,3 +11,9 @@ export declare const parseJSONSafely: (str: any) => Record<string, any> | void;
|
|
|
11
11
|
* @returns The JSON string representation of the object, or 'stringify error' if an error occurs.
|
|
12
12
|
*/
|
|
13
13
|
export declare const stringifyObjSafely: (obj: Record<string, any>, tabSpaces?: number) => string;
|
|
14
|
+
/**
|
|
15
|
+
* Checks if a given string is a valid JSON string.
|
|
16
|
+
* @param str - The string to be checked.
|
|
17
|
+
* @returns A boolean indicating whether the string is a valid JSON string or not.
|
|
18
|
+
*/
|
|
19
|
+
export declare const isJSONString: (str: string) => boolean;
|
package/dist/objectUtils.d.ts
CHANGED
|
@@ -22,3 +22,9 @@ export declare const flattenObject: (obj: {
|
|
|
22
22
|
export declare const unflattenObject: (obj: {
|
|
23
23
|
[key: string]: any;
|
|
24
24
|
}) => any;
|
|
25
|
+
/**
|
|
26
|
+
* Checks if an object is empty.
|
|
27
|
+
* @param obj - The object to check.
|
|
28
|
+
* @returns True if the object is empty, false otherwise.
|
|
29
|
+
*/
|
|
30
|
+
export declare const isEmptyObj: (obj: Record<any, any>) => boolean;
|