Skip to content

Commit

Permalink
Add convert utility function and update imports
Browse files Browse the repository at this point in the history
  • Loading branch information
MuttakinHasib committed Feb 17, 2024
1 parent 299422a commit 47c8eec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions apps/web/src/utils/convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type Filter = { [key: string]: string | string[] };

export function ensureArrayValues<T = Filter>(filters: Filter): T {
const result: Filter = {};

for (const key in filters) {
if (Array.isArray(filters[key])) {
result[key] = filters[key]; // If it's already an array, keep it as is
} else {
result[key] = [filters[key] as string]; // Convert string value to array
}
}

return result as T;
}
1 change: 1 addition & 0 deletions apps/web/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./cn";
export * from "./cookie";
export * from "./name";
export * from "./query-builder";
export * from "./convert";
7 changes: 6 additions & 1 deletion apps/web/src/utils/query-builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ReadonlyURLSearchParams } from "next/navigation";
import queryString from "query-string";

export const getQueries = (searchParams: ReadonlyURLSearchParams) =>
export const getQueries = (
searchParams: ReadonlyURLSearchParams
): Record<string, any> =>
queryString.parse(searchParams.toString(), { arrayFormat: "comma" });

export const buildQuery = (queries: Record<string, string | string[]>) =>
queryString.stringify(queries, { arrayFormat: "comma" });

0 comments on commit 47c8eec

Please sign in to comment.