@bit-ui-libs/common
Version:
This library was generated with [Nx](https://nx.dev).
23 lines (21 loc) • 885 B
text/typescript
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('&');
};