URL.SearchParams
A list of query parameters, stored as { name, value } pairs in order.
Names may repeat. Pairs are parsed as application/x-www-form-urlencoded: empty segments are skipped, a pair without = gets an empty value.
Serializing always writes name=value and encodes spaces as +, so "flag" becomes "flag=".
When taken from url.searchParams, every change is written back to the URL.
tostring( params ) returns the serialized query without the leading "?", #params is the number of pairs.
local url = URL( "https://example.com/?a=1&b=2" )
local params = url.searchParams
params:get( "a" ) -- "1"
params:append( "c", "x y" )
url.search -- "?a=1&b=2&c=x+y"Constructor
URL.SearchParams(init: string | { [ 1 ]: string, [ 2 ]: string | nil }[] | nil, url: URL | nil): URL.SearchParamsFields
| Name | Type | Description |
|---|---|---|
url | URL | nil | The URL this list writes changes back to, nil for standalone lists. |
[ integer ] | { [ 1 ]: string, [ 2 ]: string | nil } | The { name, value } pairs, in order. |
Operators
| Operator | Operand | Result |
|---|---|---|
len | | integer |
Methods
append | Appends a name-value pair to the end of the list. |
delete | Removes every pair with the given name. |
get | Returns the value of the first pair with the given name. |
getAll | Returns the values of every pair with the given name, in order. |
has | Checks if there is a pair with the given name. |
iterator | Returns an iterator over the name-value pairs, in order. |
keys | Returns an iterator over the names, a name that repeats is returned every time. |
set | Sets the value of the first pair with the given name and removes the other pairs with that name. |
sort | Sorts the pairs by name in byte order (stable), pairs with the same name keep their relative order. |
values | Returns an iterator over the values, a nil value gives "". |
#SearchParams:append
function URL.SearchParams:append(name: string, value?: string)
Appends a name-value pair to the end of the list.
| Name | Type | Description |
|---|---|---|
name | string | The name of the parameter. |
valueoptional | string | The value of the parameter, nil is serialized the same as "". |
#SearchParams:delete
function URL.SearchParams:delete(name: string, value?: string)
Removes every pair with the given name.
If value is given, only pairs with both the given name and value are removed.
| Name | Type | Description |
|---|---|---|
name | string | The name of the parameters to remove. |
valueoptional | string | The value the removed pairs must have. |
#SearchParams:get
function URL.SearchParams:get(name: string): string | nil
Returns the value of the first pair with the given name.
| Name | Type | Description |
|---|---|---|
name | string | The name of the parameter. |
| Type | Name | Description |
|---|---|---|
string | nil | value | The value, nil if there is no such pair. |
#SearchParams:getAll
function URL.SearchParams:getAll(name: string): string[]
Returns the values of every pair with the given name, in order.
URL.SearchParams( "a=1&b=2&a=3" ):getAll( "a" ) -- { "1", "3" }| Name | Type | Description |
|---|---|---|
name | string | The name of the parameters. |
| Type | Name | Description |
|---|---|---|
string[] | values | The values, empty if there are no such pairs. |
#SearchParams:has
function URL.SearchParams:has(name: string, value?: string): boolean
Checks if there is a pair with the given name.
If value is given, the pair must also have the given value.
| Name | Type | Description |
|---|---|---|
name | string | The name of the parameter. |
valueoptional | string | The value the pair must have. |
| Type | Name | Description |
|---|---|---|
boolean | exists | true if such a pair exists, otherwise false. |
#SearchParams:iterator
function URL.SearchParams:iterator(): (fun(): string?), string?
Returns an iterator over the name-value pairs, in order.
for name, value in url.searchParams:iterator() do
print( name, value )
end| Type | Name | Description |
|---|---|---|
fun(): string? | ||
string? | iterator | The iterator function, returns nil when the list ends. |
#SearchParams:keys
function URL.SearchParams:keys(): fun(): string?
Returns an iterator over the names, a name that repeats is returned every time.
| Type | Name | Description |
|---|---|---|
fun(): string? | iterator | The iterator function, returns nil when the list ends. |
#SearchParams:set
function URL.SearchParams:set(name: string, value?: string)
Sets the value of the first pair with the given name and removes the other pairs with that name.
Appends a new pair if there is no pair with the given name.
local params = URL.SearchParams( "a=1&b=2&a=3" )
params:set( "a", "9" )
tostring( params ) -- "a=9&b=2"| Name | Type | Description |
|---|---|---|
name | string | The name of the parameter. |
valueoptional | string | The new value, nil is serialized the same as "". |
#SearchParams:sort
function URL.SearchParams:sort()
Sorts the pairs by name in byte order (stable), pairs with the same name keep their relative order.
local params = URL.SearchParams( "b=1&a=2&b=0" )
params:sort()
tostring( params ) -- "a=2&b=1&b=0"#SearchParams:values
function URL.SearchParams:values(): fun(): string?
Returns an iterator over the values, a nil value gives "".
| Type | Name | Description |
|---|---|---|
fun(): string? | iterator | The iterator function, returns nil when the list ends. |