Skip to content

uriToIdentifier

uriToIdentifier(uri): string

Converts a URI to a valid identifier by checking its format and stripping known prefixes.

Parameters

uri: undefined | null | string

The URI to be converted into an identifier. Can be a string, null, or undefined.

Returns

string

The validated identifier as a string.

Remarks

This function processes a URI and transforms it into a valid identifier. It validates the URI’s format, checks for known prefixes, and ensures the resulting string conforms to identifier standards.

Example

const uri = 'prefix:identifierString';
try {
const identifier = uriToIdentifier(uri);
console.log('Valid Identifier:', identifier);
} catch (error) {
console.error('Error:', error);
}

Throws

If the input is not a string or is empty.

Throws

If the processed identifier is invalid, with a detailed error message.

Description

The function first checks if the input is a valid string. If not, it throws an InvalidURIError. It then searches for known valid prefixes in the URI. If found, these prefixes are removed, leaving only the core identifier. This identifier is then validated using checkIdentifier, ensuring it adheres to the required format and checksum rules. If validation fails, an InvalidIdentifierError is thrown, indicating the specific reason for the failure.

Source

identifier/src/Identifier.ts:446