{"version":3,"file":"label_utils-DZLvqvsf.js","sources":["../../app/javascript/utils/label_utils.ts"],"sourcesContent":["import {\n Maybe,\n Program,\n ProgramCustomLabel,\n ProgramSequence,\n} from \"../components/generated/graphql\";\n\n// For a deeper explanation on the regex read the comment in app/lib/utils/label_utils.rb\nconst TRANSFORM_REGEX =\n /{{{?[^{}]+}?}}|[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b[-a-zA-Z0-9()@:%_+.~#?&/=]*|(((a|an) )*\\b(mentors?\\b(?! collective)|students?\\b|mentorships?\\b|schools?\\b|mentees?\\b))/gi;\n\ntype LocalProgramCustomLabel = Pick<\n ProgramCustomLabel,\n | \"singularMentor\"\n | \"singularMentorWithIndeterminateArticle\"\n | \"pluralMentor\"\n | \"respectMentorUppercase\"\n | \"singularMentee\"\n | \"singularMenteeWithIndeterminateArticle\"\n | \"pluralMentee\"\n | \"respectMenteeUppercase\"\n | \"singularStudent\"\n | \"singularStudentWithIndeterminateArticle\"\n | \"pluralStudent\"\n | \"respectStudentUppercase\"\n | \"singularMentorship\"\n | \"singularMentorshipWithIndeterminateArticle\"\n | \"pluralMentorship\"\n | \"respectMentorshipUppercase\"\n | \"singularSchool\"\n | \"singularSchoolWithIndeterminateArticle\"\n | \"pluralSchool\"\n | \"respectSchoolUppercase\"\n>;\ntype LocalProgramSequence = Pick;\ntype LocalProgram = Pick & {\n programSequence: LocalProgramSequence;\n programCustomLabel?: Maybe;\n};\n\n// A spin off of this code lives in app/lib/utils/label_utils.rb. Any change in here needs to be checked\n// in that file to ensure compatibility between BE and FE.\nexport class LabelTranslator {\n program?: Maybe;\n programCustomLabel?: Maybe;\n\n constructor(program: Maybe | undefined) {\n this.program = program;\n this.programCustomLabel = program?.programCustomLabel;\n }\n\n transformText(text: string): string {\n if (!this.programCustomLabel) return text;\n\n return text.replace(TRANSFORM_REGEX, (match, group) => {\n if (!group) return match;\n const lowerMatch = match.toLowerCase();\n if (lowerMatch.includes(\"mentorship\")) {\n return this.translate(match, \"mentorship\");\n } else if (lowerMatch.includes(\"school\")) {\n return this.translate(match, \"school\");\n } else if (lowerMatch.includes(\"mentor\")) {\n return this.translate(match, \"mentor\");\n } else if (lowerMatch.includes(\"student\")) {\n return this.translate(match, \"student\");\n } else if (lowerMatch.includes(\"mentee\")) {\n return this.translate(match, \"mentee\");\n }\n return match;\n });\n }\n\n translate(\n phrase: string,\n replacement: \"mentor\" | \"mentee\" | \"school\" | \"mentorship\" | \"student\"\n ): string {\n const prefix = /^.*s$/i.test(phrase) ? \"plural\" : \"singular\";\n const upCaseReplacement = `${replacement[0].toUpperCase()}${replacement.slice(1)}`;\n const respectUppercaseKey =\n `respect${upCaseReplacement}Uppercase` as keyof LocalProgramCustomLabel;\n const labelKey = `${prefix}${upCaseReplacement}` as keyof LocalProgramCustomLabel;\n const respectUppercase = this.programCustomLabel?.[respectUppercaseKey] as boolean;\n let newWord = this.programCustomLabel?.[labelKey] as string;\n\n if (!respectUppercase) newWord = newWord?.toLowerCase();\n\n if (/^[Aa]n?\\b/.test(phrase)) {\n newWord = this.#findReplacementWithIndeterminateArticle(\n phrase,\n upCaseReplacement,\n respectUppercase\n );\n }\n if (!newWord) return phrase;\n\n return this.#matchUppercase(phrase, newWord, respectUppercase);\n }\n\n #findReplacementWithIndeterminateArticle(\n phrase: string,\n replacement: string,\n respectUppercase: boolean\n ): string {\n const articleLabel =\n `singular${replacement}WithIndeterminateArticle` as keyof LocalProgramCustomLabel;\n let newWord = this.programCustomLabel?.[articleLabel] as string;\n if (!respectUppercase) newWord = newWord?.toLowerCase();\n if (phrase.startsWith(\"A\")) {\n newWord = newWord.charAt(0).toUpperCase() + newWord.slice(1);\n } else {\n newWord = newWord.charAt(0).toLowerCase() + newWord.slice(1);\n }\n return newWord;\n }\n\n #matchUppercase(originalPhrase: string, newPhrase: string, respectUppercase: boolean): string {\n if (originalPhrase === originalPhrase.toUpperCase()) {\n return newPhrase.toUpperCase();\n } else if (respectUppercase) {\n return newPhrase;\n } else if (originalPhrase.split(\" \").every((word) => /[A-Z]/.test(word.charAt(0)))) {\n return newPhrase.replace(/\\b\\w/g, (l) => l.toUpperCase());\n } else if (/[A-Z]/.test(originalPhrase.charAt(0))) {\n return newPhrase.charAt(0).toUpperCase() + newPhrase.slice(1);\n } else if (originalPhrase.split(\" \").some((word) => /[A-Z]/.test(word.charAt(0)))) {\n const allUpper = newPhrase.replace(/\\b\\w/g, (l) => l.toUpperCase());\n return allUpper.charAt(0).toLowerCase() + allUpper.slice(1);\n }\n return newPhrase;\n }\n}\n"],"names":["TRANSFORM_REGEX","LabelTranslator","program","__privateAdd","_LabelTranslator_instances","__publicField","text","match","group","lowerMatch","phrase","replacement","prefix","upCaseReplacement","respectUppercaseKey","labelKey","respectUppercase","_a","newWord","_b","__privateMethod","findReplacementWithIndeterminateArticle_fn","matchUppercase_fn","articleLabel","originalPhrase","newPhrase","word","l","allUpper"],"mappings":"8wBAQA,MAAMA,EACJ,sMAiCK,MAAMC,CAAgB,CAI3B,YAAYC,EAA0C,CAJjDC,EAAA,KAAAC,GACLC,EAAA,gBACAA,EAAA,2BAGE,KAAK,QAAUH,EACf,KAAK,mBAAqBA,GAAA,YAAAA,EAAS,kBAAA,CAGrC,cAAcI,EAAsB,CAC9B,OAAC,KAAK,mBAEHA,EAAK,QAAQN,EAAiB,CAACO,EAAOC,IAAU,CACjD,GAAA,CAACA,EAAc,OAAAD,EACb,MAAAE,EAAaF,EAAM,YAAY,EACjC,OAAAE,EAAW,SAAS,YAAY,EAC3B,KAAK,UAAUF,EAAO,YAAY,EAChCE,EAAW,SAAS,QAAQ,EAC9B,KAAK,UAAUF,EAAO,QAAQ,EAC5BE,EAAW,SAAS,QAAQ,EAC9B,KAAK,UAAUF,EAAO,QAAQ,EAC5BE,EAAW,SAAS,SAAS,EAC/B,KAAK,UAAUF,EAAO,SAAS,EAC7BE,EAAW,SAAS,QAAQ,EAC9B,KAAK,UAAUF,EAAO,QAAQ,EAEhCA,C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