mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2024-12-15 13:25:44 +01:00
c1514ce91d
Fix #13290
23 lines
835 B
TypeScript
23 lines
835 B
TypeScript
/*
|
||
* SPDX-FileCopyrightText: syuilo and misskey-project
|
||
* SPDX-License-Identifier: AGPL-3.0-only
|
||
*/
|
||
|
||
/* objを検査して
|
||
* 1. 配列に何も入っていない時はクエリを付けない
|
||
* 2. プロパティがundefinedの時はクエリを付けない
|
||
* (new URLSearchParams(obj)ではそこまで丁寧なことをしてくれない)
|
||
*/
|
||
export function query(obj: Record<string, any>): string {
|
||
const params = Object.entries(obj)
|
||
.filter(([, v]) => Array.isArray(v) ? v.length : v !== undefined)
|
||
.reduce((a, [k, v]) => (a[k] = v, a), {} as Record<string, any>);
|
||
|
||
return Object.entries(params)
|
||
.map((p) => `${p[0]}=${encodeURIComponent(p[1])}`)
|
||
.join('&');
|
||
}
|
||
|
||
export function appendQuery(url: string, query: string): string {
|
||
return `${url}${/\?/.test(url) ? url.endsWith('?') ? '' : '&' : '?'}${query}`;
|
||
}
|