24 lines
776 B
TypeScript
24 lines
776 B
TypeScript
/**
|
|
* URL utility helpers for encoding and query string construction.
|
|
*/
|
|
|
|
/** URL-encodes a single value for use in path/query segments. */
|
|
export function encodeUrl(value: string): string {
|
|
return encodeURIComponent(value);
|
|
}
|
|
|
|
/**
|
|
* Builds a query string from an array of key-value pairs.
|
|
* Null/undefined/empty values are skipped.
|
|
* Returns an empty string if no valid params, or a leading `?` followed by the query.
|
|
*/
|
|
export function buildQuery(params: Array<[string, string | null | undefined]>): string {
|
|
const parts: string[] = [];
|
|
for (const [key, value] of params) {
|
|
if (value != null && value !== '') {
|
|
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
}
|
|
}
|
|
return parts.length === 0 ? '' : `?${parts.join('&')}`;
|
|
}
|