PluginProbe
ElasticPress / 4.7.2
ElasticPress v4.7.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 / api-search / src / utilities.js

utilities.js in ElasticPress 4.7.2, at assets/js/api-search/src/utilities.js

224 lines 5.9 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
102 /**
103 * Get permalink URL parameters from args.
104 *
105 * @typedef {object} ArgSchema
106 * @property {string} type Arg type.
107 * @property {any} [default] Default arg value.
108 * @property {Array} [allowedValues] Array of allowed values.
109 *
110 * @param {object} args Args
111 * @param {ArgSchema} schema Args schema.
112 * @param {string} [prefix] Prefix to prepend to args.
113 * @returns {URLSearchParams} URLSearchParams instance.
114 */
115 export const getUrlParamsFromArgs = (args, schema, prefix = '') => {
116 const urlParams = new URLSearchParams();
117
118 Object.entries(schema).forEach(([arg, options]) => {
119 const param = prefix + arg;
120 const value = typeof args[arg] !== 'undefined' ? sanitizeParam(args[arg], options) : null;
121
122 if (value !== null) {
123 urlParams.set(param, value);
124 }
125 });
126
127 return urlParams;
128 };
129
130 /**
131 * Build request args from URL parameters using a given schema.
132 *
133 * @typedef {object} ArgSchema
134 * @property {string} type Arg type.
135 * @property {any} [default] Default arg value.
136 * @property {Array} [allowedValues] Array of allowed values.
137 *
138 * @param {Object<string, ArgSchema>} argsSchema Schema to build args from.
139 * @param {string} [paramPrefix] Parameter prefix.
140 * @returns {Object<string, any>} Query args.
141 */
142 export const getArgsFromUrlParams = (argsSchema, paramPrefix = '') => {
143 const urlParams = new URLSearchParams(window.location.search);
144
145 const args = Object.entries(argsSchema).reduce((args, [arg, options]) => {
146 const param = urlParams.get(paramPrefix + arg);
147 const value = typeof param !== 'undefined' ? sanitizeArg(param, options, false) : null;
148
149 if (value !== null) {
150 args[arg] = value;
151 }
152
153 return args;
154 }, {});
155
156 return args;
157 };
158
159 /**
160 * Build request args from defaults provided in a given schema.
161 *
162 * @param {Object<string, ArgSchema>} argsSchema Schema to build args from.
163 * @returns {Object<string, any>} Query args.
164 */
165 export const getDefaultArgsFromSchema = (argsSchema) => {
166 return Object.entries(argsSchema).reduce((args, [arg, schema]) => {
167 const hasDefault = Object.hasOwnProperty.call(schema, 'default');
168
169 if (hasDefault) {
170 args[arg] = schema.default;
171 }
172
173 return args;
174 }, {});
175 };
176
177 /**
178 * Get the current URL, including parameters, with any prefixed parameters
179 * replaced with the given parameters.
180 *
181 * @param {string} paramPrefix Prefix of parameters to replace.
182 * @param {object} params Parameters to add, if any.
183 * @returns {string} URL.
184 */
185 export const getUrlWithParams = (paramPrefix, params) => {
186 const url = new URL(window.location.href);
187 const keys = Array.from(url.searchParams.keys());
188
189 for (const key of keys) {
190 if (key.startsWith(paramPrefix)) {
191 url.searchParams.delete(key);
192 }
193 }
194
195 if (params) {
196 params.forEach((value, key) => {
197 url.searchParams.set(key, value);
198 });
199 }
200
201 return url.toString();
202 };
203
204 /**
205 * Clear facet filters from a set of args.
206 *
207 * @param {object} args Args to clear facets from.
208 * @param {object} argsSchema Args schema.
209 * @returns {object} Cleared args.
210 */
211 export const getArgsWithoutConstraints = (args, argsSchema) => {
212 const clearedArgs = { ...args };
213
214 Object.entries(argsSchema).forEach(([arg, schema]) => {
215 const hasDefault = Object.hasOwnProperty.call(schema, 'default');
216
217 if (!hasDefault) {
218 delete clearedArgs[arg];
219 }
220 });
221
222 return clearedArgs;
223 };
224