Skip to content

uriToIdentifier

uriToIdentifier(uri): string

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

uri: undefined | null | string

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

string

The validated identifier as a string.

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.

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

If the input is not a string or is empty.

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

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.

identifier/src/Identifier.ts:483