URL & Path Utilities
This section provides helper functions for creating URL-friendly strings and normalizing path prefixes. These utilities are essential for generating clean, valid, and consistent routes and links within your blocklet applications.
urlPathFriendly
#
Converts an arbitrary string into a format suitable for use in a URL path. The function sanitizes the string by removing non-URL-friendly characters, collapsing consecutive separators, and ensuring a consistent format.
Parameters
Name | Type | Description |
---|---|---|
|
| The input string to be sanitized. |
|
| An optional configuration object. |
|
| If |
Returns
Type | Description |
---|---|
| The URL path-friendly version of the input string. |
Example
import { urlPathFriendly } from '@blocklet/meta';
// Basic sanitization
const rawString1 = ' My Awesome/Blocklet!!_v1.0.0 ';
const friendlyPath1 = urlPathFriendly(rawString1);
console.log(friendlyPath1);
// Output: 'My-Awesome/Blocklet-_v1.0.0'
// Collapsing consecutive slashes
const rawString2 = '/some/path/with//extra-slashes/';
const friendlyPath2 = urlPathFriendly(rawString2);
console.log(friendlyPath2);
// Output: '/some/path/with/extra-slashes/'
// Removing leading/trailing slashes
const rawString3 = '/another/path/';
const noSlashes = urlPathFriendly(rawString3, { keepSlash: false });
console.log(noSlashes);
// Output: 'another/path'
isValidUrlPath
#
Checks if a string is a valid URL path segment. It verifies that the string contains only allowed ASCII characters (a-z
, 0-9
, /
, -
, _
, .
) and does not contain consecutive slashes.
Parameters
Name | Type | Description |
---|---|---|
|
| The string to validate. |
Returns
Type | Description |
---|---|
|
|
Example
import { isValidUrlPath } from '@blocklet/meta';
console.log(isValidUrlPath('valid-path/sub-path_1.0')); // true
console.log(isValidUrlPath('/another/valid/path')); // true
console.log(isValidUrlPath('invalid//path')); // false (consecutive slashes)
console.log(isValidUrlPath('path with spaces')); // false (contains space)
console.log(isValidUrlPath('path-with-ñ')); // false (contains non-ASCII character)
checkUrlPath
#
Validates if a string is a correctly formatted URL path suitable for concatenation. It specifically checks that the path starts with a leading slash (/
) and does not contain any double slashes (//
).
Parameters
Name | Type | Description |
---|---|---|
|
| The URL path string to check. |
Returns
Type | Description |
---|---|
|
|
Example
import { checkUrlPath } from '@blocklet/meta';
console.log(checkUrlPath('/abc')); // true
console.log(checkUrlPath('/abc/bcd')); // true
console.log(checkUrlPath('/abc/bcd/')); // true
console.log(checkUrlPath('abc/bcd')); // false (missing leading slash)
console.log(checkUrlPath('/abc//bcd')); // false (contains double slash)
checkLink
#
Determines if a given string is either an absolute URL (e.g., https://...
) or a valid relative URL path as determined by checkUrlPath
.
Parameters
Name | Type | Description |
---|---|---|
|
| The string to check. |
Returns
Type | Description |
---|---|
|
|
Example
import { checkLink } from '@blocklet/meta';
console.log(checkLink('https://example.com/page')); // true
console.log(checkLink('/my/valid/path')); // true
console.log(checkLink('not/a/valid/link')); // false
normalizePathPrefix
#
Standardizes a path prefix string to ensure it always starts and ends with a single slash (e.g., my-prefix
becomes /my-prefix/
). This function gracefully handles various malformed inputs to produce a consistent output. If the input is not a string or is empty, it defaults to /
.
Parameters
Name | Type | Description |
---|---|---|
|
| The prefix string to normalize. |
Returns
Type | Description |
---|---|
| The normalized path prefix. |
Example
import { normalizePathPrefix } from '@blocklet/meta';
console.log(normalizePathPrefix('my-prefix')); // '/my-prefix/'
console.log(normalizePathPrefix('/my-prefix')); // '/my-prefix/'
console.log(normalizePathPrefix('my-prefix/')); // '/my-prefix/'
console.log(normalizePathPrefix('/my/prefix/')); // '/my/prefix/'
console.log(normalizePathPrefix('')); // '/'
console.log(normalizePathPrefix(null)); // '/'
Deprecated Utilities#
The following functions are deprecated and will be removed in a future version. It is recommended to use their modern replacements.
urlFriendly
: This function is deprecated. Please useurlPathFriendly
for more robust and accurate URL path sanitization.isValidUrl
: This function is deprecated. Please useisValidUrlPath
for more specific and correct path validation.
These utilities provide the necessary tools for handling URL and path string manipulations within the Blocklet ecosystem. For related filesystem operations, see the File Utilities section.