@yoot/yoot
Version:
The core library for yoot, providing a CDN-agnostic, chainable API for image URL transformations and adapter integration.
88 lines (87 loc) • 2.99 kB
JavaScript
import { _getAdapter as z } from "./adapter.js";
import { normalizeDirectives as v, mustBeInRange as d, mustBeOneOf as h } from "./helpers.js";
import { YOOT_BRAND as O } from "./store.js";
import { isUrl as p, isString as U, isPlainObject as w, isNullish as D, invariant as N, isEmpty as S, isNumber as y } from "./utils.js";
const P = (t) => m(a(R(t)));
function m(t) {
function r(i, l) {
return (u) => (l(i, u), u === void 0 ? e() : e({ directives: { [i]: u } }));
}
let c, s, n;
function f() {
return c || (L(t, "Image source URL is required"), c = z(new URL(t.src)), c);
}
function o() {
if (n) return n;
const { src: i } = t;
if (!i)
return n = v(t), n;
const l = a({ ...t, src: i }), u = f().primeState?.(l) ?? l;
return n = v(u), n;
}
function g() {
return s || (s = f().generateUrl({
src: t.src,
// `src` is guaranteed to be a string here
directives: o()
}), s);
}
function b() {
const { src: i } = t;
if (S(i)) return null;
try {
const l = new URL(i);
return f().normalizeUrl(l) ?? null;
} catch {
return null;
}
}
function I() {
return a(t);
}
function J() {
return a({ ...t, directives: o() });
}
const e = (i) => D(i) ? m(a(t)) : m(a(t, R(i)));
return e.src = (i) => (N(p(i), "[yoot] src isn't a valid URL"), e({ src: i })), e.alt = (i) => e({ alt: i }), e.map = (i) => e(i(a(t))), e.aspectRatio = e.ar = r("aspectRatio", d(1, 1 / 0)), e.crop = r("crop", h(A)), e.dpr = r("dpr", d(1, 1 / 0)), e.fit = r("fit", h(j)), e.format = e.fm = r("format", h(q)), e.height = e.h = r("height", d(1, 1 / 0)), e.quality = e.q = r("quality", d(0, 100)), e.width = e.w = r("width", d(1, 1 / 0)), Object.defineProperties(e, {
// Branding symbol
[O]: { value: !0 },
// -- Output methods --
toJSON: { value: I },
toResolvedJSON: { value: J },
toString: { value: g },
url: { get: g },
baseUrl: { get: b },
hasSrc: { get: () => !S(t.src) }
}), Object.freeze(e);
}
function R(t) {
if (U(t)) {
if (p(t)) return { src: t };
try {
const r = JSON.parse(t);
return w(r) ? r : {};
} catch {
return {};
}
}
return _(t) ? t.toJSON() : w(t) ? { ...t } : {};
}
function _(t) {
return typeof t == "function" && O in t;
}
const j = /* @__PURE__ */ new Set(["contain", "cover"]), q = /* @__PURE__ */ new Set(["auto", "jpg", "png", "webp"]), A = /* @__PURE__ */ new Set(["center", "top", "bottom", "left", "right"]);
function a(t, r = {}) {
const c = "src" in r ? r.src : t.src, s = "alt" in r ? r.alt : t.alt, n = "width" in r ? r.width : t.width, f = "height" in r ? r.height : t.height, o = {
directives: { ...t.directives, ...r.directives }
};
return p(c) && (o.src = c), U(s) && (o.alt = s), y(n) && (o.width = n), y(f) && (o.height = f), o;
}
function L(t, r) {
N(typeof t.src == "string", r);
}
export {
R as unwrapInput,
P as yoot
};
//# sourceMappingURL=yoot.js.map