-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #338 from matematikk-mooc/TheresePersen-KURSP-920-…
…filter-label-text-given-language-selected KURSP-920 filter label text in module selector given language selected
- Loading branch information
Showing
6 changed files
with
106 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Cleans a string to extract content based on a specified language code. | ||
* @param {string} label - The input string containing language-specific content. | ||
* @param {string} param - The language code ('nb' or 'nn') to match. | ||
* @returns {string} The extracted content based on the specified language code, the first alternative, or the label as is. | ||
*/ | ||
export function extractLabelForSelectedLanguage(label, param) { | ||
// Check if the label contains language codes or separators. | ||
if (/\w\w:/.test(label)) { | ||
// Define a regular expression pattern to match the desired language code and text. | ||
const pattern = new RegExp(`${param}: (.+?)(?:\\||$)`); | ||
|
||
// Execute the regular expression to find the matching content. | ||
const match = pattern.exec(label); | ||
|
||
// Check if a match was found. | ||
if (match) { | ||
// Extract and return the matched text. | ||
return match[1]; | ||
} | ||
|
||
// Check if there are alternative languages. | ||
const alternatives = label.split("|"); | ||
if (alternatives.length > 1) { | ||
// Return the content of the first alternative. | ||
const firstAlternative = alternatives[0].trim(); | ||
// Remove any leading language code if present. | ||
return firstAlternative.replace(/^\w\w:/, "").trim(); | ||
} | ||
} | ||
|
||
// If there's no language codes or matches, return the label as is. | ||
return label; | ||
} |