UNPKG

@ministryofjustice/hmpps-digital-prison-reporting-frontend

Version:

The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.

64 lines (61 loc) 2.21 kB
import { FilterType } from '../components/_filters/filter-input/enum.js'; /* eslint-disable @typescript-eslint/no-explicit-any */ const clearFilterValue = '~clear~'; /** * Merges two query objects to produce a query string * * @param {Record<string, unknown>} currentQuery * @param {(Record<string, string | undefined>)} updates * @return {*} {string} */ const mergeAndStringifyQuery = (currentQuery, updates, fields) => { const params = new URLSearchParams(); Object.entries({ ...currentQuery, ...updates }).forEach(([key, value]) => { if (value == null || value === '') return; // Special handling for filters.* if (key.startsWith('filters.') && typeof value === 'string') { const fieldName = key.replace('filters.', ''); const fieldDef = fields.find(f => f.name === fieldName); // Only expand CSV for multiselect filters if (fieldDef?.filter?.type?.toLowerCase() === FilterType.multiselect.toLowerCase() && value.includes(',')) { value .split(',') .filter(Boolean) .forEach(v => params.append(key, v)); return; } } // Arrays = repeated params if (Array.isArray(value)) { value.forEach(v => params.append(key, String(v))); return; } // single param params.set(key, String(value)); }); return `?${params.toString()}`; }; /** * Joins two query strings together * * @param {(...Array<string | undefined>)} parts * @return {*} */ const joinQueryStrings = (...parts) => { return parts .filter((p) => Boolean(p && p.length)) .map(p => p.replace(/^\?/, '')) .join('&'); }; const getDpdPathSuffix = (dpdsPath) => { if (dpdsPath && dpdsPath !== '') { return `?dataProductDefinitionsPath=${dpdsPath}`; } return ''; }; const setNestedPath = (url, baseUrl) => { return baseUrl && baseUrl !== 'undefined' ? `${baseUrl}${url}` : url; }; export { clearFilterValue, getDpdPathSuffix, joinQueryStrings, mergeAndStringifyQuery, setNestedPath }; //# sourceMappingURL=urlHelper.js.map