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