PluginProbe
Extendify / 3.2.1
Extendify v3.2.1
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Shared / utils / convert-to-valid-params.js

convert-to-valid-params.js in Extendify 3.2.1, at src/Shared/utils/convert-to-valid-params.js

62 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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