UNPKG

@ndriadev/react-tools

Version:

A React library of hooks, components, utils and types ready to use

179 lines (178 loc) 5.61 kB
import { d as j, h as x, i as B, a as U, b as F, c as N } from "./detectBrowser-BNhxl8AF.js"; import { lazy as l } from "react"; const h = (e) => e.nativeEvent instanceof MouseEvent, m = () => typeof window < "u" && !!window.document; function i(e) { return e instanceof Map ? JSON.stringify(Object.fromEntries(e)) : e instanceof Set ? JSON.stringify(Array.from(e)) : JSON.stringify(e); } function f(e) { return new Promise((n, a) => { const r = new FileReader(); r.onload = (t) => { n(t.target.result); }, r.onerror = a, r.readAsDataURL(e); }); } function d(e) { return new Promise((n, a) => { e.complete ? n() : (e.onload = () => { n(); }, e.onerror = a); }); } function C(e, n) { return new Promise((a, r) => { if (!window) return r(Error("getBase64 is executable only in browser context.")); try { if (e == null) a(""); else if (typeof e == "string") a(f(new Blob([e], { type: "text/plain" }))); else if (e instanceof Blob) a(f(e)); else if (e instanceof ArrayBuffer) a(window.btoa(String.fromCharCode(...new Uint8Array(e)))); else if (e instanceof HTMLCanvasElement) a(e.toDataURL(n == null ? void 0 : n.type, n == null ? void 0 : n.quality)); else if (e instanceof HTMLImageElement) { const t = e.cloneNode(!1); t.crossOrigin = "Anonymous", d(t).then(() => { const c = document.createElement("canvas"), o = c.getContext("2d"); c.width = t.width, c.height = t.height, o.drawImage(t, 0, 0, c.width, c.height), a(c.toDataURL(n == null ? void 0 : n.type, n == null ? void 0 : n.quality)); }).catch(r); } else if (typeof e == "object") { const c = ((n == null ? void 0 : n.serializer) || i)(e); return a(f(new Blob([c], { type: "application/json" }))); } else r(new Error("Unsupported target type.")); } catch (t) { r(t); } }); } function b(e, n) { return [...e].map((a) => { let r = a; if (Array.isArray(n)) n.forEach((t) => { const { [t]: c, ...o } = a; r = { ...o }; }); else { const { [n]: t, ...c } = a; r = { ...c }; } return r; }); } function A(e, ...n) { const a = []; for (const r of n) a.push(...r); return e !== "none" ? Array.isArray(e) ? a.reduce((r, t) => (r.findIndex((c) => { let o = !0; for (const u of e) if (c[u] !== t[u]) { o = !1; break; } return o; }) === -1 && r.push(t), r), []) : a.reduce((r, t) => (r.findIndex((c) => c[e] === t[e]) === -1 && r.push(t), r), []) : [...new Set(a)]; } function E(e, n) { return n && Object.keys(e).find((r) => e[r] === n) || void 0; } function k(e, n, a) { const r = a || {}, t = e.split("."); let c = r; for (let o = 0, u = t.length - 1; o < u; o++) !(t[o] in c) && (c[t[o]] = {}), c = c[t[o]]; return c[t[t.length - 1]] = n, r; } function y(e, n, a) { const r = { ...e }; for (const [t, c] of Object.entries(n)) c === void 0 && a && t ? r[t] = c : c !== void 0 && (!Array.isArray(c) && typeof c == "object" && c !== null ? r[t] = y(r[t], n[t] || {}) : r[t] = c); return r; } function p({ string1: e, string2: n, compareType: a, locales: r, opts: t }) { const c = e.localeCompare(n, r, t); switch (a) { case "<": return c < 0; case ">": return c > 0; case "=": return c === 0; case "<=": return c <= 0; case ">=": return c >= 0; default: return c; } } const s = { pascalCase: (e) => { var n; return e ? ((n = e.charAt(0)) == null ? void 0 : n.toUpperCase()) + e.substring(1).toLowerCase() : ""; }, snakeCase: (e, n) => e ? n ? ["upperCase", "lowerCase"].includes(n) ? e.replaceAll(n === "upperCase" ? /[A-Z]/g : /[a-z]/g, "_$&") : e.replaceAll(n, "_") : e : "", kebabCase: (e, n) => e ? n ? ["upperCase", "lowerCase"].includes(n) ? e.replaceAll(n === "upperCase" ? /[A-Z]/g : /[a-z]/g, "-$&") : e.replaceAll(n, "-") : e : "", camelCase: (e, n) => e ? n ? e.toLowerCase().split("").map((a, r, t) => a === n ? "" : r > 0 && t[r - 1] === n ? a.toUpperCase() : a).join("") : e : "" }; function v({ string: e, caseType: n, delimiter: a }) { switch (n) { case "pascalCase": return s.pascalCase(e); case "snakeCase": return s.snakeCase(e, a); case "kebabCase": return s.kebabCase(e, a); case "camelCase": return s.camelCase(e, a); default: throw Error("Unknown case type."); } } const L = (e, n = {}) => l(() => { n.beforeLoad && n.beforeLoad(); const a = e(); return a.then(() => !!n.afterLoad && n.afterLoad()), a.then((r) => { if ("default" in r) return { default: r.default }; if (n.componentName && n.componentName in r) return { default: r[n.componentName] }; const t = Reflect.ownKeys(r).filter((c) => c !== "__esModule"); if (t.length > 0) return { default: r[t[0]] }; throw Error("Invalid import"); }); }); function z(e) { return (n) => { var a; n.code === e && ((a = n.target) == null || a.click()); }; } export { p as alphanumericCompare, v as changeStringCase, z as clickElementOnKeydownEvent, i as defaultSerializer, j as detectBrowser, C as getBase64, E as getKeyObjectFromValue, k as getObjectFromDottedString, x as hotKeyHandler, B as isAsync, m as isClient, U as isDeepEqual, h as isMouseEvent, F as isShallowEqual, N as isTouchEvent, L as lazy, y as mergeObjects, b as removePropertiesFromArrayObjects, A as uniqueElementsArray };