UNPKG

@bit-ui-libs/common

Version:
23 lines (21 loc) 885 B
import { SerializableValue } from '../interfaces'; // eslint-disable-next-line @typescript-eslint/no-explicit-any export const createQueryParams = (params?: Record<any, SerializableValue>) => { if (!params) return ''; return Object.keys(params) .filter((k) => typeof params[k] !== 'undefined') .map((k) => { // This assertion is fine because we have the undefined check above // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const value = params[k]!; if (Array.isArray(value)) { if (value.length === 0) return ''; return encodeURIComponent(k) + '=' + encodeURIComponent(value.join(',')); } else if (typeof value === 'object') { return encodeURIComponent(k) + '=' + JSON.stringify(value); } return encodeURIComponent(k) + '=' + encodeURIComponent(value); }) .join('&'); };