npm i @cheprasov/url-query-params
URLQueryParams
The URLQueryParams class defines utility methods to work with the query string of a URL. It is a good replacement for standard URLSearchParams class.
An object implementing URLQueryParams can directly be used in a for...of
structure.
Why URLQueryParams is better than URLSearchParams?
- According to WHATWG, URLSearchParams uses
application/x-www-form-urlencoded
format. While it's suitable for decoding URL queries, for encoding it can lead to unexpected results such as spaces being encoded as +
and extra characters such as ~
being percent-encoded. - URLQueryParams is based on URLSearchParams class and implements all methods of URLSearchParams, but it uses correct encoding format for all characters via encodeURIComponentRFC3986 and conforms to newer RFC 3986 which defines more characters to escape, for example
*
.
1. How to install
> npm install @cheprasov/url-query-params
Import in you .js / .ts file:
import { URLQueryParams } from '@chepraspv/url-query-params';
2. Quick examples
import { URLQueryParams } from '@chepraspv/url-query-params';
new URLQueryParams('foo=bar baz').toString();
new URLQueryParams('a=1&b=2&c=3').toObject();
3. Documentation
3.1 Constructor
new URLQueryParams (string[][] | Record<string, string> | string | URLSearchParams | URLQueryParams
)
Returns a URLSearchParams object instance.
3.2 Methods
URLSearchParams.append (name: string, value: string
): void
Appends a specified key/value pair as a new search parameter.
URLSearchParams.delete (name: string
): void
Deletes the given search parameter, and its associated value, from the list of all search parameters.
URLSearchParams.entries (): IterableIterator<[string, string]>
Returns an iterator allowing iteration through all key/value pairs contained in this object.
URLSearchParams.forEach (callbackfn: (value: string, key: string, parent: URLQueryParams) => void, thisArg?: any
): void
Allows iteration through all values contained in this object via a callback function.
URLSearchParams.get (name: string
): string | null
Returns the first value associated with the given search parameter.
URLSearchParams.getAll (name: string
): string[]
Returns all the values associated with a given search parameter.
URLSearchParams.has (name: string
): boolean
Returns a boolean value indicating if such a given parameter exists.
URLSearchParams.keys (): IterableIterator
Returns an iterator allowing iteration through all keys of the key/value pairs contained in this object.
URLSearchParams.set (name: string, value: string
): void
Sets the value associated with a given search parameter to the given value. If there are several values, the others are deleted.
URLSearchParams.sort (): void
Sorts all key/value pairs, if any, by their keys.
URLSearchParams.toObject (): Record<string, string>
Returns new object created from key/values params.
URLSearchParams.toString (): string
Returns a string containing a query string suitable for use in a URL.
URLSearchParams.values (): IterableIterator
Returns an iterator allowing iteration through all values of the key/value pairs contained in this object.
Something does not work
Feel free to fork project, fix bugs, write tests and finally request for pull