Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する
API Reference

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

name

string

The input string to be sanitized.

options

object

An optional configuration object.

options.keepSlash

boolean

If true (default), slashes are preserved as path separators. If false, leading and trailing slashes are removed from the final string.

Returns

Type

Description

string

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

name

string

The string to validate.

Returns

Type

Description

boolean

true if the string is a valid URL path, otherwise false.

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

value

string

The URL path string to check.

Returns

Type

Description

boolean

true if the path format is valid for concatenation, otherwise false.

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)


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

value

string

The string to check.

Returns

Type

Description

boolean

true if the string is a valid link (absolute or relative path), otherwise false.

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

prefix

string

The prefix string to normalize.

Returns

Type

Description

string

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 use urlPathFriendly for more robust and accurate URL path sanitization.
  • isValidUrl: This function is deprecated. Please use isValidUrlPath 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.