UNPKG

@yoot/yoot

Version:

The core library for yoot, providing a CDN-agnostic, chainable API for image URL transformations and adapter integration.

98 lines (97 loc) 3.72 kB
import { isNumber as c, hasIntrinsicDimensions as R, isFunction as S, isString as w, isKeyOf as v, invariant as l } from "./utils.js"; function W(i) { return (t) => $(i, t); } function $(i, t) { const { widths: e, densities: n } = i, o = []; if (Array.isArray(e) && e.length > 0) for (const r of e) { if (!c(r) || r < 1) continue; const u = t.map((h) => { const { directives: s } = h, { width: d, height: f } = s; if (s.width = r, !c(f)) return h; const a = c(d) ? d / f : c(s.aspectRatio) ? s.aspectRatio : R(h) ? h.width / h.height : void 0; return s.height = c(a) ? Math.round(r / a) : void 0, h; }); o.push(u.url.concat(` ${r}w`)); } else if (Array.isArray(n) && n.length > 0) for (const r of n) { if (!c(r) || r <= 0) continue; const u = r === 1 ? t : t.dpr(r); o.push(u.url.concat(` ${r}x`)); } return o.join(", "); } function b(i) { const { width: t, height: e, directives: n, ...o } = i.toResolvedJSON(), r = c(n.width) || c(n.height), u = r ? n.width : t, h = r ? n.height : e, s = { ...o, naturalWidth: t, naturalHeight: e, src: i.url }; return c(u) && (s.width = u), c(h) && (s.height = h), s; } function j(i, t) { const { alt: e, sizes: n, srcSetBuilder: o, ...r } = t ?? {}, { src: u, height: h, width: s, ...d } = b(i), f = d.alt || e || "", a = { src: u, alt: f, ...r }; S(o) && (a.srcset = o(i)), w(n) && w(a.srcset) && (a.sizes = n); const g = c(s), A = c(h); return i.toResolvedJSON().directives.fit === "contain" ? (a.style = {}, g && (a.style.maxWidth = `${s}px`), A && (a.style.maxHeight = `${h}px`), a) : (g && (a.width = s), A && (a.height = h), a); } function I(i) { return (t, e) => j(t, { ...i, ...e }); } function x(i, t) { const { srcset: e, srcSetBuilder: n, ...o } = t ?? {}; if (w(o.type) && o.type.startsWith("image/")) { const [, a] = o.type.split("/"), g = a === "jpeg" ? "jpg" : a; i = i.format(g); } const { width: r, height: u, src: h } = b(i), s = { ...o }, d = () => i.toResolvedJSON().directives.format === "auto", f = s.type || M(i); return f && !d() && (s.type = f), f?.startsWith("image/") ? (S(n) ? s.srcset = n(i) : w(e) ? s.srcset = e : s.srcset = h, c(r) && (s.width = r), c(u) && (s.height = u)) : s.src = h, s; } function B(i) { return (t, e) => x(t, { ...i, ...e }); } function H(i) { const t = { ...i.directives }, { height: e, width: n } = t, o = c(n), r = c(e); if (o && r || !c(t.aspectRatio) || t.aspectRatio <= 0) return m(t), { ...t }; const { aspectRatio: u } = t; return o ? (t.height = Math.round(n / u), m(t)) : r ? (t.width = Math.round(e * u), m(t)) : R(i) && (t.width = i.width, t.height = Math.round(t.width / u), m(t)), { ...t }; } function m(i) { "aspectRatio" in i && (i.aspectRatio = void 0); } const p = { jpeg: "image/jpeg", jpg: "image/jpeg", png: "image/png", webp: "image/webp" }; function M(i) { const { format: t } = i.toResolvedJSON().directives; if (v(t, p)) return p[t]; const e = new URL(i.url).pathname.split(".").pop()?.toLowerCase(); return v(e, p) ? p[e] : void 0; } function N(i, t) { return (e, n) => { l(c(n), `${e} must be a number`), l(isFinite(n), `${e} must be a finite number`), l(n >= i && n <= t, `${e} must be between ${i} and ${t}`); }; } function D(i) { return (t, e) => { l(i.has(e), `${t} must be one of: ${[...i].join(", ")}`); }; } export { p as MIME_TYPES, $ as buildSrcSet, W as defineSrcSetBuilder, b as getAttrs, j as getImgAttrs, M as getMimeType, x as getSourceAttrs, N as mustBeInRange, D as mustBeOneOf, H as normalizeDirectives, I as withImgAttrs, B as withSourceAttrs }; //# sourceMappingURL=helpers.js.map