UNPKG

@acrool/js-utils

Version:

Common javascript utils methods for project development

93 lines (92 loc) 2.77 kB
function b(t) { const n = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"], e = function(r) { return Number.isNaN(r) ? "00" : n[(r - r % 16) / 16] + n[r % 16]; }, s = t.toLowerCase().match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); if (s !== null && s.length > 3) return ["#", e(s[1]) + e(s[2]) + e(s[3])].join(""); } function p(t, n = 1) { let e = t.replace("#", ""); const s = void 0; let r; if (/^[0-9A-F]{3}$|^[0-9A-F]{6}$/.test(e.toUpperCase())) { if (e.length === 3) { if (r = e.match(/[0-9A-F]/g), !r) return s; e = r[0] + r[0] + r[1] + r[1] + r[2] + r[2]; } const a = parseInt(e.substr(0, 2), 16), o = parseInt(e.substr(2, 2), 16), i = parseInt(e.substr(4, 2), 16); if (n < 1) { const c = n.toString().replace("0.", "."); return `rgba(${[a, o, i, c].join()})`; } return `rgb(${[a, o, i].join()})`; } return s; } function d(t) { return typeof t > "u" || ["", 0, null].includes(t) ? null : t; } function y(t, n = 0) { const e = Number(t); return isNaN(e) ? n : e; } function g(t, n = !0) { return t === "true" || t === !0 || t === 1 ? !0 : t === "false" || t === 0 || t === !1 ? !1 : n ? void 0 : !1; } function h(t) { const n = new FileReader(); return new Promise((e, s) => { n.onload = (r) => { var a; (a = r.target) != null && a.result && e(r.target.result); }, n.onerror = (r) => s(r), n.readAsDataURL(t); }); } function f(t, n) { n = n || ""; const e = atob(t), s = []; for (let a = 0; a < e.length; a += 1024) { const o = e.slice(a, a + 1024), i = new Array(o.length); for (let l = 0; l < o.length; l++) i[l] = o.charCodeAt(l); const c = new Uint8Array(i); s.push(c); } return new Blob(s, { type: n }); } function m(t) { const e = new RegExp(/data:(.+);base64,/).exec(t); if (e && e[0]) { const s = e[1], r = t.replace(e[0], ""); return f(r, s); } return null; } function $(t) { const n = new FormData(), e = (s, r = "") => { for (const [a, o] of Object.entries(s)) { const i = r ? `${r}[${a}]` : a; if (o instanceof File) n.append(i, o); else if (o instanceof Blob) { const l = o.type.split("/")[1] || "bin", u = `${a}.${l}`; n.append(i, o, u); } else Array.isArray(o) ? o.forEach((c, l) => { typeof c == "object" && c !== null ? e(c, `${i}[${l}]`) : n.append(`${i}[${l}]`, c); }) : typeof o == "object" && o !== null ? e(o, i) : o != null && n.append(i, o); } }; return e(t), n; } export { g as anyToBoolean, y as anyToNumber, f as base64ToBlob, m as base64ToBlobWithContentType, d as emptyToNull, h as fileToBase64, p as hexToRGB, $ as objToFormData, b as rgbToHex };