All files / src/utils/helpers organizationDisplay.js

100% Statements 63/63
100% Branches 72/72
100% Functions 14/14
100% Lines 53/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99    10x     10x 32x 30x 30x 23x 18x       10x 179x 172x 172x 32x   158x       10x 106x 103x 103x       10x 67x 67x 2x   65x       10x 67x 67x 1x   66x       10x 66x     10x 66x     16x     10x 72x   10x 72x 72x   72x 72x       10x 70x 8x 2x 2x   6x           24x 12x 70x 70x       10x 66x 2x 2x        
import { getUserSchoolName } from "@/utils/helpers/getUserSchoolName";
 
const HEX_RUN = /[a-f0-9]{12,}/i;
 
/** Hide noisy seed/E2E sub-lines (long hashes, oversized strings). */
export const isPresentableDetailLine = (value) => {
    if (value == null) return false;
    const text = String(value).trim();
    if (!text || text.length > 72) return false;
    if (HEX_RUN.test(text)) return false;
    return true;
};
 
/** Title-case ALL CAPS names for readable profile headers. */
export const formatProfileDisplayName = (name) => {
    if (!name) return "";
    const trimmed = String(name).trim();
    if (trimmed === trimmed.toUpperCase() && /[A-Z]/.test(trimmed)) {
        return trimmed.toLowerCase().replace(/\b[\w']/g, (char) => char.toUpperCase());
    }
    return trimmed;
};
 
/** Plan chip label without duplicating "Plan". */
export const formatProfilePlanLabel = (label) => {
    if (!label) return null;
    const trimmed = String(label).trim();
    return /plan/i.test(trimmed) ? trimmed : `${trimmed} Plan`;
};
 
/** School display name from organization payload or legacy account profile. */
export const getOrganizationSchoolName = (organization, userData, fallback = "N/A") => {
    const fromOrg = organization?.school?.school_name;
    if (fromOrg && String(fromOrg).trim()) {
        return formatProfileDisplayName(String(fromOrg).trim());
    }
    return formatProfileDisplayName(getUserSchoolName(userData, fallback));
};
 
/** District display name from organization payload. */
export const getOrganizationDistrictName = (organization, fallback = "N/A") => {
    const name = organization?.district?.district_name;
    if (name && String(name).trim()) {
        return formatProfileDisplayName(String(name).trim());
    }
    return fallback;
};
 
/** Plan slug from organization payload (feature flags + district). */
export const getOrganizationPlanSlug = (organization) =>
    organization?.current_plan || organization?.district?.feature_plan || organization?.feature_plan || null;
 
/** Plan label from organization (prefers district plan_name, then root plan_name). */
export const getOrganizationPlanLabel = (organization) => {
    return organization?.district?.plan_name || organization?.plan_name || organization?.district?.feature_plan || null;
};
 
const isLikelyStateCode = (value) => /^[A-Za-z]{2,3}$/.test(String(value || "").trim());
 
/** City / state line for school — skips invalid state values. State codes (e.g. "TX") stay uppercase intentionally. */
export const formatSchoolLocation = (school) => {
    if (!school) return null;
 
    const city = school.city ? formatProfileDisplayName(String(school.city).trim()) : "";
    const state = school.state ? String(school.state).trim() : "";
    const statePart = isLikelyStateCode(state) ? state.toUpperCase() : "";
 
    const line = [city, statePart].filter(Boolean).join(", ");
    return isPresentableDetailLine(line) ? line : city || null;
};
 
/** Full school address when city/state/address are available. */
export const formatSchoolAddress = (school) => {
    if (!school) return null;
    if (school.address && String(school.address).trim()) {
        const address = formatProfileDisplayName(String(school.address).trim());
        return isPresentableDetailLine(address) ? address : null;
    }
    const parts = [
        school.street ? formatProfileDisplayName(String(school.street).trim()) : null,
        school.city ? formatProfileDisplayName(String(school.city).trim()) : null,
        isLikelyStateCode(school.state) ? String(school.state).trim() : null,
        school.zip_code,
    ]
        .filter((part) => part && String(part).trim())
        .map((part) => String(part).trim());
    const line = parts.join(", ");
    return isPresentableDetailLine(line) ? line : null;
};
 
/** District address sub-line — only when readable. */
export const formatDistrictAddress = (district) => {
    if (!district?.district_address) return null;
    const address = formatProfileDisplayName(String(district.district_address).trim());
    return isPresentableDetailLine(address) ? address : null;
};
 
export default getOrganizationSchoolName;