UNPKG

@tanstack/router-core

Version:

Modern and scalable routing for React applications

70 lines (69 loc) 2.21 kB
//#region src/qss.ts /** * Program is a reimplementation of the `qss` package: * Copyright (c) Luke Edwards luke.edwards05@gmail.com, MIT License * https://github.com/lukeed/qss/blob/master/license.md * * This reimplementation uses modern browser APIs * (namely URLSearchParams) and TypeScript while still * maintaining the original functionality and interface. * * Update: this implementation has also been mangled to * fit exactly our use-case (single value per key in encoding). */ /** * Encodes an object into a query string. * @param obj - The object to encode into a query string. * @param stringify - An optional custom stringify function. * @returns The encoded query string. * @example * ``` * // Example input: encode({ token: 'foo', key: 'value' }) * // Expected output: "token=foo&key=value" * ``` */ function encode(obj, stringify = String) { const result = new URLSearchParams(); for (const key in obj) { const val = obj[key]; if (val !== void 0) result.set(key, stringify(val)); } return result.toString(); } /** * Converts a string value to its appropriate type (string, number, boolean). * @param mix - The string value to convert. * @returns The converted value. * @example * // Example input: toValue("123") * // Expected output: 123 */ function toValue(str) { if (!str) return ""; if (str === "false") return false; if (str === "true") return true; return +str * 0 === 0 && +str + "" === str ? +str : str; } /** * Decodes a query string into an object. * @param str - The query string to decode. * @returns The decoded key-value pairs in an object format. * @example * // Example input: decode("token=foo&key=value") * // Expected output: { "token": "foo", "key": "value" } */ function decode(str) { const searchParams = new URLSearchParams(str); const result = Object.create(null); for (const [key, value] of searchParams.entries()) { const previousValue = result[key]; if (previousValue == null) result[key] = toValue(value); else if (Array.isArray(previousValue)) previousValue.push(toValue(value)); else result[key] = [previousValue, toValue(value)]; } return result; } //#endregion exports.decode = decode; exports.encode = encode; //# sourceMappingURL=qss.cjs.map