Url Utils
Url Utils provides helpers for URL handling. Use it when you need to detect whether a URL is already encoded (e.g. before deciding to encode or decode) or to avoid double-encoding.
Import
import { isEncoded } from '@js-smart/react-kit';
Basic usage
isEncoded('hello'); // false
isEncoded('hello%20world'); // true
isEncoded('a%2Bb'); // true
With query or path building
const raw = searchParams.get('q');
if (raw && !isEncoded(raw)) {
// safe to encode for use in another URL
const encoded = encodeURIComponent(raw);
}
Notes
- Uses
decodeURIComponent(url)and compares the result to the original. If decoding changes the string, the URL is considered encoded. - If
decodeURIComponentthrows (e.g. malformed encoding), the function returnsfalse.
API Reference
isEncoded
Returns whether the given URL string appears to be percent-encoded. Returns false if the URL is not encoded or if decoding fails.
| Parameter | Type | Description |
|---|---|---|
url | string | The URL string to check. |
Returns: boolean