Search Docs
parseQueryWith
此函数允许你使用自定义的序列化函数,将一个扁平对象转换为 URL 编码的查询字符串。这在你需要对特定类型的值进行特殊处理,或者想要自定义查询字符串的格式时非常有用。
import { parseQueryWith } from 'funtool'; // 自定义序列化函数 const customSerializer = (key, value) => { if (typeof value === 'boolean') { return `${key}=${value ? 'yes' : 'no'}`; } return `${key}=${encodeURIComponent(value)}`; }; // 使用自定义序列化函数 const query = parseQueryWith({ name: 'John', isActive: true, age: 30 }, customSerializer); // 输出: 'name=John&isActive=yes&age=30'
function parseQueryWith( obj: Record<string | number, any>, serializer: (key: string, value: any) => string ): string
obj
serializer
返回一个由 & 连接的 URL 编码的查询字符串,使用自定义序列化函数生成。
&