@emqx/shared-ui-utils 0.0.5 → 0.0.8
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.d.ts +2 -1
- package/dist/index.js +50 -4
- package/dist/index.umd.cjs +1 -1
- package/dist/jsonUtils.d.ts +19 -0
- package/dist/objectUtils.d.ts +30 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './objectUtils';
|
|
2
|
+
export * from './jsonUtils';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const l = (r, e = [], t = {}) => {
|
|
2
|
+
if (typeof r == "object" && !Array.isArray(r) && r !== null)
|
|
3
|
+
for (const n of Object.keys(r))
|
|
4
|
+
l(r[n], e.concat(n), t);
|
|
5
|
+
else
|
|
6
|
+
t[e.join(".")] = r;
|
|
7
|
+
return t;
|
|
8
|
+
}, i = (r) => {
|
|
9
|
+
if (Object(r) !== r && !Array.isArray(r))
|
|
10
|
+
return r;
|
|
11
|
+
const e = /\.?([^.[\]]+)|\[(\d+)\]/g, t = {};
|
|
12
|
+
try {
|
|
13
|
+
for (const n in r) {
|
|
14
|
+
let c = t, s = "", o;
|
|
15
|
+
for (; o = e.exec(n); )
|
|
16
|
+
c = c[s] || (c[s] = o[2] ? [] : {}), s = o[2] || o[1];
|
|
17
|
+
c[s] = r[n];
|
|
18
|
+
}
|
|
19
|
+
} catch (n) {
|
|
20
|
+
console.error(n);
|
|
21
|
+
}
|
|
22
|
+
return t[""] || t;
|
|
23
|
+
}, y = (r) => Object.keys(r).length === 0, a = (r) => {
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(r);
|
|
26
|
+
} catch {
|
|
27
|
+
console.error("An error occurred while parsing the JSON string");
|
|
28
|
+
}
|
|
29
|
+
}, f = (r, e) => {
|
|
30
|
+
try {
|
|
31
|
+
return typeof r == "string" ? r : JSON.stringify(r, null, e);
|
|
32
|
+
} catch (t) {
|
|
33
|
+
return console.error(t), "stringify error";
|
|
34
|
+
}
|
|
35
|
+
}, u = (r) => {
|
|
36
|
+
if (typeof r != "string")
|
|
37
|
+
return !1;
|
|
38
|
+
try {
|
|
39
|
+
const e = JSON.parse(r);
|
|
40
|
+
return typeof e == "object" && e !== null;
|
|
41
|
+
} catch {
|
|
42
|
+
return !1;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
4
45
|
export {
|
|
5
|
-
|
|
46
|
+
l as flattenObject,
|
|
47
|
+
y as isEmptyObj,
|
|
48
|
+
u as isJSONString,
|
|
49
|
+
a as parseJSONSafely,
|
|
50
|
+
f as stringifyObjSafely,
|
|
51
|
+
i as unflattenObject
|
|
6
52
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(t,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(t=typeof globalThis<"u"?globalThis:t||self,s(t["@emqx/shared-ui-utils"]={}))})(this,function(t){"use strict";const s=(e,r=[],n={})=>{if(typeof e=="object"&&!Array.isArray(e)&&e!==null)for(const i of Object.keys(e))s(e[i],r.concat(i),n);else n[r.join(".")]=e;return n},l=e=>{if(Object(e)!==e&&!Array.isArray(e))return e;const r=/\.?([^.[\]]+)|\[(\d+)\]/g,n={};try{for(const i in e){let c=n,f="",o;for(;o=r.exec(i);)c=c[f]||(c[f]=o[2]?[]:{}),f=o[2]||o[1];c[f]=e[i]}}catch(i){console.error(i)}return n[""]||n},y=e=>Object.keys(e).length===0,u=e=>{try{return JSON.parse(e)}catch{console.error("An error occurred while parsing the JSON string")}},a=(e,r)=>{try{return typeof e=="string"?e:JSON.stringify(e,null,r)}catch(n){return console.error(n),"stringify error"}},O=e=>{if(typeof e!="string")return!1;try{const r=JSON.parse(e);return typeof r=="object"&&r!==null}catch{return!1}};t.flattenObject=s,t.isEmptyObj=y,t.isJSONString=O,t.parseJSONSafely=u,t.stringifyObjSafely=a,t.unflattenObject=l,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a JSON string safely and returns a JavaScript object.
|
|
3
|
+
* @param str - The JSON string to parse.
|
|
4
|
+
* @returns A JavaScript object if parsing is successful, otherwise void.
|
|
5
|
+
*/
|
|
6
|
+
export declare const parseJSONSafely: (str: any) => Record<string, any> | void;
|
|
7
|
+
/**
|
|
8
|
+
* Safely stringify a JavaScript object to a JSON string, handling circular references.
|
|
9
|
+
* @param obj - The object to stringify.
|
|
10
|
+
* @param tabSpaces - The number of spaces to use for indentation.
|
|
11
|
+
* @returns The JSON string representation of the object, or 'stringify error' if an error occurs.
|
|
12
|
+
*/
|
|
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;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flattens a nested object into a single-level object with dot-separated keys.
|
|
3
|
+
* @param obj - The object to flatten.
|
|
4
|
+
* @param prefix - An optional array of keys to use as a prefix for the flattened keys.
|
|
5
|
+
* @param current - An optional object to use as the initial flattened object.
|
|
6
|
+
* @returns The flattened object.
|
|
7
|
+
* Example: { a: { b: c: 1 } } => { 'a.b.c': 1 }
|
|
8
|
+
*/
|
|
9
|
+
export declare const flattenObject: (obj: {
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}, prefix?: any[], current?: {
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
}) => {
|
|
14
|
+
[key: string]: any;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Converts a flattened object to a nested object.
|
|
18
|
+
* @param obj - The flattened object to convert.
|
|
19
|
+
* @returns The nested object.
|
|
20
|
+
* Example: { 'a.b.c': 1 } => { a: { b: { c: 1 } } }
|
|
21
|
+
*/
|
|
22
|
+
export declare const unflattenObject: (obj: {
|
|
23
|
+
[key: string]: any;
|
|
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;
|