-
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.
KURSP-920-filter label text, refactor code as labels cant reliably be…
… used to check if an item is selected, that is why I know use ids
- Loading branch information
1 parent
9b28df4
commit c2a5b6f
Showing
7 changed files
with
106 additions
and
56 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 was deleted.
Oops, something went wrong.
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; | ||
} |