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