オブジェクトを URL のクエリ文字列に変換します。
| 引数 |
説明 |
型 |
string |
URL のクエリ文字列 |
string |
| パラメーター |
説明 |
型 |
既定値 |
data |
変換するオブジェクト |
Object |
{} |
import { querystring } from 'ranuts';
const params = {
name: 'John',
age: 30,
city: 'New York',
};
const query = querystring(params);
console.log(query); // 'name=John&age=30&city=New%20York'
import { querystring } from 'ranuts';
const baseUrl = 'https://api.example.com/users';
const params = {
page: 1,
limit: 10,
sort: 'name',
};
const url = `${baseUrl}?${querystring(params)}`;
console.log(url);
// 'https://api.example.com/users?page=1&limit=10&sort=name'
import { querystring } from 'ranuts';
const params = {
search: 'hello world',
category: 'web development',
};
const query = querystring(params);
console.log(query); // 'search=hello%20world&category=web%20development'
import { querystring } from 'ranuts';
const params = {
name: 'John',
age: undefined,
city: null,
active: true,
};
const query = querystring(params);
console.log(query); // 'name=John&active=true'
// 値が undefined や null のものは取り除かれます
- URL エンコード:値は自動で URL エンコードされます。
- 空の値の除去:値が
undefined や null のものは自動で取り除かれ、クエリ文字列には現れません。
- オブジェクトであること:オブジェクト以外を渡すと
TypeError が投げられます。
- エンコードの前処理:キーも値も、エンコードの前に
decodeURIComponent を通します。