This section provides a set of helper functions for creating URL-friendly strings and validating URL paths. These utilities are essential for generating clean, valid, and consistent routes and links within your blocklet applications.
Recommended Utilities
The following functions are provided in the url-path-friendly.ts module and are the recommended choice for handling URL paths.
urlPathFriendly(name, [options])
Converts a string into a URL path-friendly format by replacing non-standard characters with hyphens and cleaning up consecutive special characters.
Parameters
- name
string(required) — The input string to be converted. - options
object— Optional configuration object.- keepSlash
boolean(default:true) — Iftrue(default), slashes (/) are preserved. Iffalse, leading and trailing slashes are removed.
- keepSlash
Returns
string: The URL path-friendly string.
Example
import urlPathFriendly from '@blocklet/meta/lib/url-path-friendly';
// Basic usage
const messyString = ' My Awesome/Blocklet!!_v1.0. ';
const cleanPath = urlPathFriendly(messyString);
// Result: 'My-Awesome/Blocklet-v1.0.'
// Handling consecutive characters
const withExtras = 'path///with...multiple---separators';
const cleanedExtras = urlPathFriendly(withExtras);
// Result: 'path/with.multiple-separators'
// Without keeping slashes
const fullPath = '/my/blocklet/path/';
const noSlashes = urlPathFriendly(fullPath, { keepSlash: false });
// Result: 'my/blocklet/path'isValidUrlPath(name)
Checks if a given string is a valid URL path segment. It verifies that the string contains only ASCII characters, matches the pattern ^[a-z0-9/\-._]*$, and does not contain consecutive slashes (//).
Parameters
- name
string(required) — The path string to validate.
Returns
boolean:trueif the path is valid, otherwisefalse.
Example
import { isValidUrlPath } from '@blocklet/meta/lib/url-path-friendly';
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-emojis-😀')); // false (non-ASCII characters)
console.log(isValidUrlPath('invalid path')); // false (contains space)checkLink(value)
Determines if a string is either a valid absolute URL or a valid relative URL path suitable for linking.
Parameters
- value
string(required) — The URL or path to check.
Returns
boolean:trueif the link is valid, otherwisefalse.
Example
import { checkLink } from '@blocklet/meta/lib/url-path-friendly';
// Absolute URLs
console.log(checkLink('https://www.arcblock.io')); // true
console.log(checkLink('ftp://example.com')); // true
// Valid relative paths
console.log(checkLink('/my-page')); // true
console.log(checkLink('/docs/getting-started/')); // true
// Invalid paths
console.log(checkLink('not-a-link')); // false
console.log(checkLink('/invalid//path')); // falsecheckUrlPath(value)
A stricter check specifically for URL paths used in concatenation. It validates that a path starts with a slash and does not contain consecutive slashes.
Parameters
- value
string(required) — The URL path to check.
Returns
boolean:trueif the path is a valid concatenation path, otherwisefalse.
Example
import { checkUrlPath } from '@blocklet/meta/lib/url-path-friendly';
console.log(checkUrlPath('/abc')); // true
console.log(checkUrlPath('/abc/bcd')); // true
console.log(checkUrlPath('/abc/bcd/')); // true
console.log(checkUrlPath('abc')); // false (does not start with '/')
console.log(checkUrlPath('/abc//bcd')); // false (contains consecutive slashes)Deprecated Utilities
⚠️ Warning: The following functions from the
url-friendly.tsmodule are deprecated. Please use the utilities fromurl-path-friendly.tsinstead for better handling of URL paths.
urlFriendly(name, [options])
Deprecated: Use
urlPathFriendlyinstead.
Converts a string to a URL-friendly format using slugify.
Parameters
- name
string(required) — The input string. - options
object— Optional configuration object.- keepSlash
boolean(default:true) — Iftrue(default), slashes (/) are preserved. Iffalse, slashes are converted to hyphens.
- keepSlash
Example
import urlFriendly from '@blocklet/meta/lib/url-friendly';
// With slashes kept
const result1 = urlFriendly('my/awesome_blocklet');
// Result: 'my/awesome-blocklet'
// With slashes replaced
const result2 = urlFriendly('my/awesome_blocklet', { keepSlash: false });
// Result: 'my-awesome-blocklet'isValidUrl(name)
Deprecated: Use
isValidUrlPathorcheckLinkinstead.
Checks if a string contains characters that are typically problematic in URLs, such as spaces and various symbols.
Example
import { isValidUrl } from '@blocklet/meta/lib/url-friendly';
console.log(isValidUrl('valid-name')); // true
console.log(isValidUrl('invalid name')); // false
console.log(isValidUrl('another$invalid')); // false