@studiohyperdrive/utils
Version:
A package containing common Javascript utils.
96 lines (95 loc) • 3.12 kB
JavaScript
const w = (e) => !e || typeof e != "string" ? "" : e.split(/(?=[A-Z])/).map((r, t) => t === 0 ? r[0].toUpperCase() + r.slice(1).toLowerCase() : r.toLowerCase()).join(" ").trim(), k = (e) => typeof e != "string" ? e : e.replaceAll(/[\s\u00A0]/gi, " "), b = (e) => e.normalize("NFD").replace(/([\u0300-\u036f])/g, ""), F = (e, r, t = !0, c = !0) => O(e, r, t, c, "some"), E = (e, r, t = !0, c = !0) => O(e, r, t, c, "every"), O = (e, r, t = !0, c = !0, n) => {
if (!r.trim())
return !0;
if (!e)
return !1;
const o = r.split(" ").filter((s) => !!s), a = b(e), l = (s) => {
const u = t ? a : e, y = t ? b(s) : s;
return c ? u.toLowerCase().includes(y.toLowerCase()) : u.includes(y);
};
return n === "some" ? o.some(l) : o.every(l);
}, L = (e, r) => {
if (!e || Object.values(e).length === 0)
return;
const t = Object.values(e).indexOf(r);
return Object.keys(e)[t];
};
function h(e) {
return e && e.__esModule && Object.prototype.hasOwnProperty.call(e, "default") ? e.default : e;
}
var i, d;
function g() {
return d || (d = 1, i = (e) => {
if (Object.prototype.toString.call(e) !== "[object Object]")
return !1;
const r = Object.getPrototypeOf(e);
return r === null || r === Object.prototype;
}), i;
}
var f, j;
function m() {
if (j) return f;
j = 1;
const e = g(), r = (n, o, a) => {
let l = n;
return o.cleanArrays === !0 && (l = n.map((s) => t(s, o, a + 1)).filter(Boolean)), a === 0 || o.preserveArrays === !0 || l.length > 0 ? l : null;
}, t = (n, o, a) => {
const l = {};
if (!e(n))
return Array.isArray(n) ? r(n, o, a) : n;
for (const s of Object.keys(n))
if (!(n[s] === null || n[s] === void 0))
if (Array.isArray(n[s])) {
const u = r(n[s], o, a + 1);
u !== null && (l[s] = u);
} else if (e(n[s])) {
const u = t(n[s], o, a + 1);
u !== null && Object.keys(u).length > 0 && (l[s] = u);
} else n[s] !== "" && (l[s] = n[s]);
return a > 0 && Object.keys(l).length === 0 ? null : l;
};
return f = (n, o) => {
const a = {
preserveArrays: !0,
cleanArrays: !0,
...o
};
return t(n, a, 0);
}, f;
}
var p = m();
const A = /* @__PURE__ */ h(p), P = (e, ...r) => r.reduce(
(t, c) => {
const [n, o] = c;
return o == null || typeof o == "object" && Object.keys(A(o)).length === 0 ? t : {
...t,
[n]: o
};
},
// Iben: Cast the start value to any to prevent issues with typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
e
), C = (e, r) => {
let t;
return e.forEach((c) => {
t || (r(c) ? t = c : t = C(c.children, r));
}), t;
}, S = (e, r) => {
try {
const t = URL.createObjectURL(e), c = document.createElement("a");
c.download = r, c.href = t, c.click();
} catch {
throw new Error("Failed to transform blob to file");
}
};
export {
w as camelCaseToSentence,
L as getKeyByValue,
P as merge,
b as normalizeString,
S as openFile,
k as replaceHtmlWhitespace,
C as searchRecursively,
E as searchValueForEvery,
F as searchValueForSome
};