@emqx/shared-ui-utils 0.0.8 → 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.
@@ -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
@@ -1,2 +1,3 @@
1
1
  export * from './objectUtils';
2
2
  export * from './jsonUtils';
3
+ export * from './format';
package/dist/index.js CHANGED
@@ -1,52 +1,60 @@
1
- const l = (r, e = [], t = {}) => {
1
+ const i = (r, t = [], e = {}) => {
2
2
  if (typeof r == "object" && !Array.isArray(r) && r !== null)
3
3
  for (const n of Object.keys(r))
4
- l(r[n], e.concat(n), t);
4
+ i(r[n], t.concat(n), e);
5
5
  else
6
- t[e.join(".")] = r;
7
- return t;
8
- }, i = (r) => {
6
+ e[t.join(".")] = r;
7
+ return e;
8
+ }, f = (r) => {
9
9
  if (Object(r) !== r && !Array.isArray(r))
10
10
  return r;
11
- const e = /\.?([^.[\]]+)|\[(\d+)\]/g, t = {};
11
+ const t = /\.?([^.[\]]+)|\[(\d+)\]/g, e = {};
12
12
  try {
13
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];
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
19
  } catch (n) {
20
20
  console.error(n);
21
21
  }
22
- return t[""] || t;
23
- }, y = (r) => Object.keys(r).length === 0, a = (r) => {
22
+ return e[""] || e;
23
+ }, l = (r) => Object.keys(r).length === 0, y = (r) => {
24
24
  try {
25
25
  return JSON.parse(r);
26
26
  } catch {
27
27
  console.error("An error occurred while parsing the JSON string");
28
28
  }
29
- }, f = (r, e) => {
29
+ }, u = (r, t) => {
30
30
  try {
31
- return typeof r == "string" ? r : JSON.stringify(r, null, e);
32
- } catch (t) {
33
- return console.error(t), "stringify error";
31
+ return typeof r == "string" ? r : JSON.stringify(r, null, t);
32
+ } catch (e) {
33
+ return console.error(e), "stringify error";
34
34
  }
35
- }, u = (r) => {
35
+ }, a = (r) => {
36
36
  if (typeof r != "string")
37
37
  return !1;
38
38
  try {
39
- const e = JSON.parse(r);
40
- return typeof e == "object" && e !== null;
39
+ const t = JSON.parse(r);
40
+ return typeof t == "object" && t !== null;
41
41
  } catch {
42
42
  return !1;
43
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]}`;
44
51
  };
45
52
  export {
46
- l as flattenObject,
47
- y as isEmptyObj,
48
- u as isJSONString,
49
- a as parseJSONSafely,
50
- f as stringifyObjSafely,
51
- i as unflattenObject
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
52
60
  };
@@ -1 +1 @@
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"})});
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emqx/shared-ui-utils",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "homepage": "https://emqx.io",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {