@feugene/mu
Version:
Helpful TS utilities without dependencies
40 lines • 1.66 kB
TypeScript
type ToQueryStringOptions = {
encodeName: boolean;
};
/**
* Takes an object and converts it to an encoded query string.
*
* Non-recursive:
*
* toQueryString({foo: 1, bar: 2}); // returns "foo=1&bar=2"
* toQueryString({foo: null, bar: 2}); // returns "foo=&bar=2"
* toQueryString({'some price': '$300'}); // returns "some%20price=%24300"
* toQueryString({date: new Date(2011, 0, 1)}); // returns "date=%222011-01-01T00%3A00%3A00%22"
* toQueryString({colors: ['red', 'green', 'blue']}); // returns "colors=red&colors=green&colors=blue"
*
* Recursive:
*
* toQueryString({
* username: 'Jacky',
* dateOfBirth: {
* day: 1,
* month: 2,
* year: 1911,
* },
* hobbies: ['coding', 'eating', 'sleeping', ['nested', 'stuff']]
* }, true); // returns the following string (broken down and url-decoded for ease of reading purpose):
* // username=Jacky
* // &dateOfBirth[day]=1&dateOfBirth[month]=2&dateOfBirth[year]=1911
* // &hobbies[0]=coding&hobbies[1]=eating&hobbies[2]=sleeping&hobbies[3][0]=nested&hobbies[3][1]=stuff
*
* @param {Object} object The object to encode
* @param {Boolean} [recursive=false] Whether or not to interpret the object in recursive format.
* @param {Object} options = {
* - encodeName {Boolean} Encode each KeyName in the object
* }
* (PHP / Ruby on Rails servers and similar).
* @return {String} queryString
*/
export default function toQueryString(object: Record<string, any>, recursive?: boolean, options?: ToQueryStringOptions): string;
export {};
//# sourceMappingURL=toQueryString.d.ts.map