PluginProbe
ElasticPress / 4.2.2
ElasticPress v4.2.2
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / assets / js / instant-results / utilities.js

utilities.js in ElasticPress 4.2.2, at assets/js/instant-results/utilities.js

101 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Sanitize an argument value based on its type.
3 *
4 * @param {*} value The value.
5 * @param {object} options Sanitization options.
6 * @param {'number'|'numbers'|'string'|'strings'} options.type (optional) Value type.
7 * @param {Array} options.allowedValues (optional) Allowed values.
8 * @param {*} options.default (optional) Default value.
9 * @param {boolean} [useDefaults] Whether to return default values.
10 * @returns {*} Sanitized value.
11 */
12 export const sanitizeArg = (value, options, useDefaults = true) => {
13 let sanitizedValue = null;
14
15 switch (value && options.type) {
16 case 'number':
17 sanitizedValue = parseFloat(value, 10) || null;
18 break;
19 case 'numbers':
20 sanitizedValue = decodeURIComponent(value)
21 .split(',')
22 .map((v) => parseFloat(v, 10))
23 .filter(Boolean);
24 break;
25 case 'string':
26 sanitizedValue = value.toString();
27 break;
28 case 'strings':
29 sanitizedValue = decodeURIComponent(value)
30 .split(',')
31 .map((v) => v.toString().trim());
32 break;
33 default:
34 break;
35 }
36
37 /**
38 * If there is a list of allowed values, make sure the value is
39 * allowed.
40 */
41 if (options.allowedValues) {
42 sanitizedValue = options.allowedValues.includes(sanitizedValue) ? sanitizedValue : null;
43 }
44
45 /**
46 * Populate a default value if one is available and we still don't
47 * have a value.
48 */
49 if (useDefaults && sanitizedValue === null && typeof options.default !== 'undefined') {
50 sanitizedValue = options.default;
51 }
52
53 return sanitizedValue;
54 };
55
56 /**
57 * Sanitize a parameter value based on its type.
58 *
59 * @param {*} value The value.
60 * @param {object} options Sanitization options.
61 * @param {'number'|'numbers'|'string'|'strings'} options.type (optional) Value type.
62 * @param {Array} options.allowedValues (optional) Allowed values.
63 * @param {*} options.default (optional) Default value.
64 * @param {boolean} [useDefaults] Whether to return default values.
65 * @returns {*} Sanitized value.
66 */
67 export const sanitizeParam = (value, options, useDefaults = true) => {
68 let sanitizedValue = null;
69
70 switch (value && options.type) {
71 case 'number':
72 case 'string':
73 sanitizedValue = value;
74 break;
75 case 'numbers':
76 case 'strings':
77 sanitizedValue = value.join(',');
78 break;
79 default:
80 break;
81 }
82
83 /**
84 * If there is a list of allowed values, make sure the value is
85 * allowed.
86 */
87 if (options.allowedValues) {
88 sanitizedValue = options.allowedValues.includes(sanitizedValue) ? sanitizedValue : null;
89 }
90
91 /**
92 * Populate a default value if one is available and we still don't
93 * have a value.
94 */
95 if (useDefaults && sanitizedValue === null && typeof options.default !== 'undefined') {
96 sanitizedValue = options.default;
97 }
98
99 return sanitizedValue;
100 };
101