Files
rudder-js-sdk/src/transport/url.ts
T

24 lines
776 B
TypeScript
Raw Normal View History

2026-08-12 14:02:25 +03:00
/**
* 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('&')}`;
}