All files / src/utils/helpers getUserSchoolName.js

100% Statements 17/17
95.45% Branches 21/22
100% Functions 2/2
100% Lines 15/15

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 3910x   10x 217x   22x 17x 17x 6x   11x     5x 4x               1x       10x 85x     76x       85x        
const EMPTY_SCHOOL_LABELS = new Set(["unknown", "n/a", "na", ""]);
 
const resolveSchoolValue = (value) => {
    if (value == null) return null;
 
    if (typeof value === "string") {
        const trimmed = value.trim();
        if (!trimmed || EMPTY_SCHOOL_LABELS.has(trimmed.toLowerCase())) {
            return null;
        }
        return trimmed;
    }
 
    if (typeof value === "object") {
        return (
            resolveSchoolValue(value.school_name) ||
            resolveSchoolValue(value.name) ||
            resolveSchoolValue(value.schoolName) ||
            null
        );
    }
 
    return null;
};
 
/** Normalize school display from account/profile payloads (string, object, or missing). */
export const getUserSchoolName = (user, fallback = "N/A") => {
    if (!user) return fallback;
 
    const resolved =
        resolveSchoolValue(user.school) ||
        resolveSchoolValue(user.school_name) ||
        resolveSchoolValue(user.schoolName);
 
    return resolved || fallback;
};
 
export default getUserSchoolName;