chore(core): add tests for toURLSearchParams (#8322)

This commit is contained in:
fundon
2024-09-20 04:14:48 +00:00
parent 661594aec8
commit bed70cd51a
9 changed files with 87 additions and 30 deletions
-23
View File
@@ -1,5 +1,4 @@
import { appInfo } from '@affine/electron-api';
import { isNil } from 'lodash-es';
interface AppUrlOptions {
desktop?: boolean | string;
@@ -32,25 +31,3 @@ export function buildAppUrl(path: string, opts: AppUrlOptions = {}) {
return new URL(path, webBase).toString();
}
}
export function toURLSearchParams(
params?: Partial<Record<string, string | string[]>>
) {
if (!params) return;
const items = Object.entries(params)
.filter(([_, v]) => !isNil(v))
.filter(([_, v]) => {
if (typeof v === 'string') {
return v.length > 0;
}
if (Array.isArray(v)) {
return v.length > 0;
}
return false;
}) as [string, string | string[]][];
return new URLSearchParams(
items.map(([k, v]) => [k, Array.isArray(v) ? v.join(',') : v])
);
}