@yoot/shopify
Version:
Shopify adapter for @yoot/yoot
48 lines (47 loc) • 1.57 kB
JavaScript
import { defineAdapter as f } from "@yoot/yoot";
import { _isEmpty as i, _isNumber as r } from "@yoot/yoot/internal";
const U = f({
supports: (t) => t.hostname === "cdn.shopify.com",
generateUrl: $,
normalizeUrl: x,
primeState: g
});
function x(t) {
const n = t.searchParams.get("v"), e = new URL(t.href), [o, s] = a(e);
return e.pathname = h(o).concat(s), e.search = "", e.hash = "", i(n) || e.searchParams.set("v", n), e.href;
}
function $(t) {
const { directives: n } = t, e = v(n), o = l(n), s = _(n), c = b(n);
return E(new URL(t.src), { crop: e, dimensions: o, dpr: s, extension: c });
}
function g(t) {
const { directives: n } = t;
return n.fit === "cover" && i(n.crop) && (r(n.width) || r(n.height)) && (n.crop = "center"), t;
}
function E(t, n) {
const { crop: e, dimensions: o, dpr: s, extension: c } = n, [m, u] = a(t), p = h(m).concat(`${o}${e}${s}`);
return t.pathname = `${p}${u}${c}`, t.href;
}
function a(t) {
return t.pathname.split(/(\.[^.]+)(\.(jpg|png|webp))?$/);
}
const d = /(_(\d+)?x(\d+)?)?(_crop_[^@]+)?(@\d+x)?$/;
function h(t) {
return t.replace(d, "");
}
function _({ dpr: t }) {
return r(t) ? (t = Math.round(t), t = Math.max(1, Math.min(t, 3)), t === 1 ? "" : `@${t}x`) : "";
}
function b({ format: t }) {
return i(t) || t === "auto" ? "" : `.${t}`;
}
function l({ width: t, height: n }) {
return r(t) || r(n) ? `_${t ?? ""}x${n ?? ""}` : "";
}
function v({ crop: t, width: n, height: e }) {
return i(t) || !r(n) && !r(e) ? "" : `_crop_${t}`;
}
export {
U as adapter
};
//# sourceMappingURL=adapter.js.map