@xmtp/content-type-primitives
Version:
Primitives for building custom XMTP content types
40 lines (38 loc) • 1.46 kB
JavaScript
/**
* Compares two content type IDs for equality
*
* @param a - First content type ID
* @param b - Second content type ID
* @returns True if the content type IDs are equal
*/
const contentTypesAreEqual = (a, b) => a.authorityId === b.authorityId && a.typeId === b.typeId;
/**
* Converts a content type ID to a string
*
* @param contentType - Content type ID
* @returns String representation of the content type ID
*/
const contentTypeToString = (contentType) => `${contentType.authorityId}/${contentType.typeId}:${contentType.versionMajor}.${contentType.versionMinor}`;
const contentTypeStringRegex = /^([^/]+)\/([^:]+):(\d+)\.(\d+)$/;
/**
* Converts a string to a content type ID
*
* @param contentTypeString - String representation of the content type ID
* @returns Content type ID
* @throws Error if the string does not match the expected format
*/
const contentTypeFromString = (contentTypeString) => {
const match = contentTypeString.match(contentTypeStringRegex);
if (!match) {
throw new Error(`Invalid content type string: "${contentTypeString}". Expected format: "authorityId/typeId:majorVersion.minorVersion"`);
}
const [, authorityId, typeId, major, minor] = match;
return {
authorityId,
typeId,
versionMajor: Number(major),
versionMinor: Number(minor),
};
};
export { contentTypeFromString, contentTypeToString, contentTypesAreEqual };
//# sourceMappingURL=index.js.map