返回 CodeWhale
contributor-merch.ts
根目录 / web / lib / contributor-merch.ts
1 /** Local contributor draft only: no eligibility check, payment or vendor order. */
2 export type ContributorIdentity = { github: string; contribution: string };
3 export type IdentityError = "missing" | "badIdentity" | "badUrl";
4
5 export function validateContributorIdentity(value: ContributorIdentity): IdentityError | null {
6 const github = value.github.trim().replace(/^@/, "");
7 const contribution = value.contribution.trim();
8 if (!github && !contribution) return "missing";
9 if (github && (!/^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?$/.test(github) || github.includes("--"))) return "badIdentity";
10 if (contribution) {
11 try {
12 const url = new URL(contribution);
13 if (contribution.length > 2048 || url.protocol !== "https:" || url.username || url.password) return "badUrl";
14 } catch { return "badUrl"; }
15 }
16 return null;
17 }
18
19 export const CONTRIBUTOR_CATALOG_SOURCE = "https://www.yoycol.com/product_detail/DJCTX/Unisex-O-neck-Short-Sleeve-T-shirt-180GSM-Cotton-DTF";
20
21 /** Catalog-floor estimate; artwork, tax, address and real FX are still unquoted. */
22 export function contributorCost({ production = 5.12, shipping = 1.11, rate = .039, flat = .30, fx = 6.70 } = {}) {
23 if (![production, shipping, rate, flat, fx].every(Number.isFinite) || production < 0 || shipping < 0 || flat < 0 || rate < 0 || rate >= 1 || fx <= 0) throw new RangeError("Invalid cost inputs");
24 const productionCny = production * fx;
25 const shippingCny = shipping * fx;
26 const unrounded = (productionCny + shippingCny + flat * fx) / (1 - rate);
27 // Round the whole charge upward to avoid rounding below processing-inclusive cost.
28 const total = Math.ceil((unrounded - Number.EPSILON) * 100) / 100;
29 const postage = Math.round(shippingCny * 100) / 100;
30 const made = Math.round(productionCny * 100) / 100;
31 return { production: made, shipping: postage, processing: Math.round((total - made - postage) * 100) / 100, total, merchandise: Math.round((total - postage) * 100) / 100, fx, rate, flat };
32 }
33
33 lines TYPESCRIPT