The UrlSearchParams
object makes working with URL query parameters a little easier.
import { UrlSearchParams } from '@dojo/core/UrlSearchParams';
const searchParams = new UrlSearchParams('a=b&b=c&c=a');
import { UrlSearchParams } from '@dojo/core/UrlSearchParams';
const searchParams = new UrlSearchParams({
a: 'b',
b: 'c',
c: 'a'
});
Adds the value to the values that are associated with the key.
import { UrlSearchParams } from '@dojo/core/UrlSearchParams';
const searchParams = new UrlSearchParams('a=b&b=c&c=a');
const key = 'a';
const value = 'e';
searchParams.append(key, value);
Removes the key from the search params.
import { UrlSearchParams } from '@dojo/core/UrlSearchParams';
const searchParams = new UrlSearchParams('a=b&b=c&c=a');
const key = 'a';
searchParams.delete(key);
Retrieves the first value for the key provided.
import { UrlSearchParams } from '@dojo/core/UrlSearchParams';
const searchParams = new UrlSearchParams('a=first&a=second');
const key = 'a';
const result = searchParams.get(key);
result === 'first'; // true
Retrieves all the values for the key provided.
import { UrlSearchParams } from '@dojo/core/UrlSearchParams';
const searchParams = new UrlSearchParams('a=first&a=second');
const key = 'a';
const result = searchParams.getAll(key);
result[0] === 'first'; // true
result[1] === 'second'; // true
Returns true if the key exists within the search params and false if it is not.
import { UrlSearchParams } from '@dojo/core/UrlSearchParams';
const searchParams = new UrlSearchParams('a=b&b=c&c=a');
const key = 'd';
const result = searchParams.has(key);
result === false; // true
Returns an array of the keys of the search params.
import { UrlSearchParams } from '@dojo/core/UrlSearchParams';
const searchParams = new UrlSearchParams('a=b&b=c&c=a');
const result = searchParams.keys();
result instanceof Array; // true
result[0] === 'a'; // true
result[1] === 'b'; // true
result[2] === 'c'; // true
Sets the value of a key (clears previous values).
import { UrlSearchParams } from '@dojo/core/UrlSearchParams';
const searchParams = new UrlSearchParams('a=b&b=c&c=a');
const key = 'a';
const value = 'e';
searchParams.set(key, value);
Return a string of the search params.
import { UrlSearchParams } from '@dojo/core/UrlSearchParams';
const searchParams = new UrlSearchParams('a=b&b=c&c=a');
const result = searchParams.toString();
result === 'a=b&b=c&c=a'; // true