querystring

یک شیء را به رشتهٔ پرس‌وجوی نشانی تبدیل می‌کند.

API

querystring

بازگشت

آرگومان توضیح نوع
string رشتهٔ پرس‌وجوی نشانی 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'

undefined و null کنار گذاشته می‌شوند

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 کنار گذاشته می‌شوند

یادداشت‌ها

  1. کدگذاری نشانی: مقدارها خودبه‌خود برای نشانی کدگذاری می‌شوند.
  2. کنار گذاشتن مقدار خالی: مقدارهای undefined و null خودبه‌خود کنار می‌روند و در رشتهٔ پرس‌وجو نمی‌آیند.
  3. باید شیء باشد: اگر چیزی جز شیء بدهید، TypeError پرتاب می‌شود.
  4. پیش از کدگذاری: کلیدها و مقدارها هر دو از decodeURIComponent می‌گذرند.