Files
bitvid/dist/js/payments/platformFee.js
T
google-labs-jules[bot]andPR0M3TH3AN 0bfa3b08e8 fix(ci): fix netlify deployment by building to dist
- Move from root publishing to clean `dist/` directory
- Create `scripts/build-dist.mjs` to orchestrate build and copy assets
- Update `package.json` build script
- Update `netlify.toml` to publish `dist/`
- Ignore `dist/` in `.gitignore`

Co-authored-by: PR0M3TH3AN <[email protected]>
2026-01-29 09:07:15 +00:00

75 lines
1.9 KiB
JavaScript

// js/payments/platformFee.js
import { PLATFORM_FEE_PERCENT } from "../config.js";
export function parsePercentValue(value) {
if (typeof value === "number") {
return Number.isFinite(value) ? value : NaN;
}
if (typeof value === "bigint") {
return Number(value);
}
if (typeof value === "string") {
const trimmed = value.trim();
if (!trimmed) {
return NaN;
}
if (trimmed.includes("/")) {
const [firstRaw, secondRaw] = trimmed.split("/").map((part) => part.trim());
if (firstRaw && secondRaw) {
const first = Number(firstRaw);
const second = Number(secondRaw);
if (Number.isFinite(first) && Number.isFinite(second) && first >= 0 && second >= 0) {
const total = first + second;
if (total > 0) {
return (second / total) * 100;
}
}
}
}
const percentStripped = trimmed.endsWith("%") ? trimmed.slice(0, -1).trim() : trimmed;
const directNumeric = Number(percentStripped);
if (Number.isFinite(directNumeric)) {
return directNumeric;
}
const match = percentStripped.match(/-?\d+(?:\.\d+)?/);
if (match) {
const fallback = Number(match[0]);
return Number.isFinite(fallback) ? fallback : NaN;
}
return NaN;
}
return Number.isFinite(value) ? Number(value) : NaN;
}
export function clampPercent(value) {
const numeric = parsePercentValue(value);
if (!Number.isFinite(numeric)) {
return 0;
}
return Math.min(100, Math.max(0, numeric));
}
export function getDefaultPlatformFeePercent() {
const parsed = parsePercentValue(PLATFORM_FEE_PERCENT);
if (!Number.isFinite(parsed)) {
return 0;
}
return clampPercent(parsed);
}
export function resolvePlatformFeePercent(overrideValue) {
const parsedOverride = parsePercentValue(overrideValue);
if (Number.isFinite(parsedOverride)) {
return clampPercent(parsedOverride);
}
return getDefaultPlatformFeePercent();
}