diff --git a/packages/browser/src/index.ts b/packages/browser/src/index.ts index 6031e81..023d1d2 100644 --- a/packages/browser/src/index.ts +++ b/packages/browser/src/index.ts @@ -57,6 +57,7 @@ export {default as resolveEmojiUrisInHtml} from './utils/resolveEmojiUrisInHtml' export {default as isAvatarUri, AVATAR_URI_SCHEME} from './utils/isAvatarUri'; export {default as extractAvatarParamsFromUri} from './utils/extractAvatarParamsFromUri'; export type {AvatarParams, AvatarShape, AvatarVariant} from './utils/extractAvatarParamsFromUri'; +export {default as buildAvatarSpec} from './utils/buildAvatarSpec'; export { default as generateAvatarDataUri, deriveAvatarContent, diff --git a/packages/browser/src/utils/buildAvatarSpec.ts b/packages/browser/src/utils/buildAvatarSpec.ts new file mode 100644 index 0000000..14155e9 --- /dev/null +++ b/packages/browser/src/utils/buildAvatarSpec.ts @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import type {AvatarParams} from './extractAvatarParamsFromUri'; +import {AVATAR_URI_SCHEME} from './isAvatarUri'; + +/** + * Serializes {@link AvatarParams} into an `avatar:` URI, the inverse of + * {@link extractAvatarParamsFromUri}. + * + * @param params - The avatar parameters to serialize. + * @returns An `"avatar:shape=...,variant=...,content=...,colors=..."` spec string, with a + * trailing `,bg=...` when {@link AvatarParams.bg} is set. + * + * @example + * ```typescript + * buildAvatarSpec({shape: 'circle', variant: 'two_letter', content: 'BM', colors: 2}); + * // "avatar:shape=circle,variant=two_letter,content=BM,colors=2" + * ``` + */ +const buildAvatarSpec = ({shape, variant, content, colors, bg}: AvatarParams): string => { + const base = `${AVATAR_URI_SCHEME}shape=${shape},variant=${variant},content=${content},colors=${colors}`; + return bg ? `${base},bg=${bg}` : base; +}; + +export default buildAvatarSpec; diff --git a/packages/react/src/index.ts b/packages/react/src/index.ts index 4480a78..796daaa 100644 --- a/packages/react/src/index.ts +++ b/packages/react/src/index.ts @@ -257,6 +257,22 @@ export { extractEmojiFromUri, resolveLogoUri, type ResolvedLogo, + isAvatarUri, + AVATAR_URI_SCHEME, + extractAvatarParamsFromUri, + type AvatarParams, + type AvatarShape, + type AvatarVariant, + buildAvatarSpec, + generateAvatarDataUri, + deriveAvatarContent, + AVATAR_GRADIENT_COUNT, + pickAnonymousAvatarName, + pickAnonymousEntityName, + ANONYMOUS_ANIMAL_ICONS, + type AnonymousAnimalIcon, + ANONYMOUS_ENTITY_ICONS, + type AnonymousEntityIcon, resolveMeta, resolveFlowTemplateLiterals, countryCodeToFlagEmoji,