| 1 |
/** |
| 2 |
* Converts a comma-separated parameter string into an array of valid values, |
| 3 |
* removing duplicates and discarding anything not in the allowed list. |
| 4 |
* |
| 5 |
* @param {string|null} params - A comma-separated string of parameters (e.g. "info,pages,layout"). |
| 6 |
* May be null or empty. |
| 7 |
* @param {string[]} allowedItems - List of allowed values (e.g. ["questions", "info", "layout", "pages"]). |
| 8 |
* @returns {string[]} - An array of unique, valid values. Returns [] if no valid values are found. |
| 9 |
* |
| 10 |
* @example |
| 11 |
* convertToValidParamsArray("info, pages,invalid", ["info","pages"]); |
| 12 |
* // => ["info", "pages"] |
| 13 |
*/ |
| 14 |
export const convertToValidParamsArray = (params, allowedItems) => { |
| 15 |
if (!params || !allowedItems?.length) return []; |
| 16 |
|
| 17 |
return Array.from( |
| 18 |
new Set( |
| 19 |
params |
| 20 |
.split(',') |
| 21 |
.map((item) => item.trim()) |
| 22 |
.filter((item) => allowedItems.includes(item)), |
| 23 |
), |
| 24 |
); |
| 25 |
}; |
| 26 |
|
| 27 |
/** |
| 28 |
* Converts a comma-separated parameter string into a normalized string, |
| 29 |
* keeping only allowed values, removing duplicates, and joining them back |
| 30 |
* into a single string. |
| 31 |
* |
| 32 |
* @param {string|null} params - A comma-separated string of parameters (e.g. "info,pages,layout"). |
| 33 |
* May be null or empty. |
| 34 |
* @param {string[]} allowedItems - List of allowed values (e.g. ["questions", "info", "layout", "pages"]). |
| 35 |
* @returns {string} - A comma-separated string of valid, unique values. Returns "" if no valid values are found. |
| 36 |
* |
| 37 |
* @example |
| 38 |
* convertToValidParamsString("info, pages,invalid", ["info","pages"]); |
| 39 |
* // => "info,pages" |
| 40 |
*/ |
| 41 |
export const convertToValidParamsString = (params, allowedItems) => { |
| 42 |
return convertToValidParamsArray(params, allowedItems).join(','); |
| 43 |
}; |
| 44 |
|
| 45 |
/** |
| 46 |
* Maps tone string values to their matching objects in the allowed list. |
| 47 |
* |
| 48 |
* - Filters out invalid values. |
| 49 |
* - Preserves input order and duplicates. |
| 50 |
* |
| 51 |
* @param {string[]|null|undefined} values - Tone values to map. |
| 52 |
* @param {{label: string, value: string}[]} allowed - Allowed tone objects. |
| 53 |
* @returns {{label: string, value: string}[]} Matching tone objects. |
| 54 |
*/ |
| 55 |
export const mapToneValuesToObjects = (siteToneParam, allowedTonesObject) => { |
| 56 |
if (!Array.isArray(siteToneParam) || siteToneParam.length === 0) return []; |
| 57 |
|
| 58 |
return siteToneParam |
| 59 |
.map((tone) => allowedTonesObject.find((t) => t.value === tone)) |
| 60 |
.filter(Boolean); |
| 61 |
}; |
| 62 |
|