nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.
43 lines • 2.19 kB
TypeScript
import type { QueryObject, StrictObject } from '../object/types';
import type { QueryString } from '../string/types';
/**
* * Utility to generate query parameters from an object.
*
* @param params - Object containing query parameters.
* @returns A query string as a URL-encoded string, e.g., `?key1=value1&key2=value2`.
*
* @example
* generateQueryParams({ key1: 'value1', key2: 42 }); // "?key1=value1&key2=42"
* generateQueryParams({ key1: ['value1', 'value2'], key2: 42 }); // "?key1=value1&key1=value2&key2=42"
* generateQueryParams({ key1: '', key2: null }); // ""
* generateQueryParams({ key1: true, key2: false }); // "?key1=true&key2=false"
* generateQueryParams({ filters: { category: 'laptop', price: 1000 } }); // "?category=laptop&price=1000"
*/
export declare const generateQueryParams: <T extends QueryObject>(params?: T) => QueryString;
/**
* * Get query params as standard `JavaScript` Object `Record<string, string>`.
*
* - **Note:** *Extracts query parameters from the current URL (window.location.search).*
*
* @returns Query string as key-value paired object. `Record<string, string>`.
*/
export declare function getQueryParams(): Record<string, string>;
/**
* * Update query params in the browser URL with given key and value.
* @param key Key for the query to update.
* @param value Value to updated against the given key.
*/
export declare function updateQueryParam(key: string, value: string): void;
/**
* Parses a query string (with optional `?` prefix) into an object.
* Supports multiple values for the same key by returning arrays.
* Optionally parses primitive string values into actual types (e.g., "1" → 1, "true" → true).
*
* - **Note:** *This function does **not** access or depend on `current URL` a.k.a `window.location.search`.*
*
* @param query - The query string to parse.
* @param parsePrimitives - Whether to convert stringified primitives into real values (default: true).
* @returns An object where keys are strings and values can be string, array, number, boolean, or null.
*/
export declare const parseQueryString: (query: string, parsePrimitives?: boolean) => StrictObject;
//# sourceMappingURL=query.d.ts.map