generated from ConductionNL/product-website-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from ConductionNL/feature/XW-95/queryParams
feature/XW-95/queryParams
- Loading branch information
Showing
4 changed files
with
43 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,43 @@ | ||
export const filtersToQueryParams = (filters: any): string => { | ||
Object.keys(filters) | ||
.filter((key) => filterKeysToRemove.includes(key)) | ||
.forEach((key) => { | ||
delete filters[key]; | ||
}); | ||
const cleanedFilters = Object.fromEntries( | ||
Object.entries(filters).filter(([key]) => !filterKeysToRemove.includes(key)), | ||
); | ||
|
||
let params: string = ""; | ||
const params = Object.entries(cleanedFilters) | ||
.map(([key, value]) => { | ||
if (!value) return null; | ||
|
||
for (const [key, value] of Object.entries(filters)) { | ||
if (!value) continue; | ||
const formattedValue = Array.isArray(value) | ||
? value.map((v: string) => v.replace(/\s+/g, "_")).join(`&${key}[]=`) | ||
: (value as string).replace(/\s+/g, "_"); | ||
|
||
if (typeof value === "string") { | ||
params += `&${key}=${value}`; | ||
} | ||
return `${Array.isArray(value) ? `${key}[]` : key}=${formattedValue}`; | ||
}) | ||
.filter(Boolean) | ||
.join("&"); | ||
|
||
if (Array.isArray(value)) { | ||
let arrayParams = ""; | ||
|
||
value.forEach((value) => { | ||
arrayParams += `&${key}[]=${value}`; | ||
}); | ||
|
||
params += arrayParams; | ||
} | ||
} | ||
|
||
return params; | ||
return params ? `&${params}` : ""; | ||
}; | ||
|
||
export const filtersToUrlQueryParams = (filters: any): string => { | ||
Object.keys(filters) | ||
.filter((key) => filterKeysToRemove.includes(key)) | ||
.forEach((key) => { | ||
delete filters[key]; | ||
}); | ||
|
||
let params: string = ""; | ||
|
||
var first_iteration = true; | ||
for (const [key, value] of Object.entries(filters)) { | ||
if (!value) continue; | ||
|
||
if (first_iteration) { | ||
if (typeof value === "string") { | ||
params += `?${key}=${value.replace(/\s+/g, "_")}`; | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
let arrayParams = ""; | ||
|
||
value.forEach((value) => { | ||
arrayParams += `?${key}[]=${value.replace(/\s+/g, "_")}`; | ||
}); | ||
|
||
params += arrayParams; | ||
} | ||
|
||
first_iteration = false; | ||
} else { | ||
if (typeof value === "string") { | ||
params += `&${key}=${value.replace(/\s+/g, "_")}`; | ||
} | ||
export const filtersToUrlQueryParams = (filters: Record<string, any>): string => { | ||
const cleanedFilters = Object.fromEntries( | ||
Object.entries(filters).filter(([key]) => !filterKeysToRemove.includes(key)), | ||
); | ||
|
||
if (Array.isArray(value)) { | ||
let arrayParams = ""; | ||
const params = Object.entries(cleanedFilters) | ||
.map(([key, value]) => { | ||
if (!value) return null; | ||
|
||
value.forEach((value) => { | ||
arrayParams += `&${key}[]=${value.replace(/\s+/g, "_")}`; | ||
}); | ||
const formattedValue = Array.isArray(value) | ||
? value.map((v: string) => v.replace(/\s+/g, "_")).join(`&${key}[]=`) | ||
: (value as string).replace(/\s+/g, "_"); | ||
|
||
params += arrayParams; | ||
} | ||
} | ||
} | ||
return `${Array.isArray(value) ? `${key}[]` : key}=${formattedValue}`; | ||
}) | ||
.filter(Boolean) | ||
.join("&"); | ||
|
||
return params; | ||
return params ? `?${params}` : ""; | ||
}; | ||
|
||
const filterKeysToRemove: string[] = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters