# API of `@koobiq/components/username`

Documented on [Username](https://koobiq.io/en/components/username.md). NgModule: `KbqUsernameModule`.

## Components

### KbqUsername (component)

Displays a user's name based on profile data.
Supports different display modes and visual styles.
A custom view can be provided via `<kbq-username-custom-view>` for full control over the output.
Accepts input profile data and optional formatting options.

```ts
@Component({ selector: 'kbq-username', exportAs: 'kbqUsername' })
class KbqUsername {
    readonly userInfo = input<KbqUserInfo>();
    readonly isCompact = input<boolean>(false);
    readonly fullNameFormat = input<string>(kbqDefaultFullNameFormat);
    readonly mode = input<KbqUsernameMode>('inline');
    readonly type = input<KbqUsernameStyle>('default');
}
```

- `[userInfo]`: `KbqUserInfo` — User profile data used for display.
- `[isCompact]`: `boolean` — Enables compact display mode
- `[fullNameFormat]`: `string` — Format string for rendering the user's full name.
- `[mode]`: `KbqUsernameMode` — Display mode of the username. Defaults to `'inline'`.
- `[type]`: `KbqUsernameStyle` — Visual style of the username. Defaults to `'default'`.

## Directives

### KbqUsernameCustomView (directive)

Custom content for `KbqUsername`, overrides default view.

```ts
@Directive({
    selector: 'kbq-username-custom-view, [kbq-username-custom-view]',
    exportAs: 'kbqUsernameCustomView'
})
class KbqUsernameCustomView {}
```

### KbqUsernamePrimary (directive)

Styles the primary part of the username (e.g. full name).

```ts
@Directive({ selector: '[kbqUsernamePrimary]', exportAs: 'kbqUsernamePrimary' })
class KbqUsernamePrimary {}
```

### KbqUsernameSecondary (directive)

Styles the secondary part.

```ts
@Directive({
    selector: '[kbqUsernameSecondary]',
    exportAs: 'kbqUsernameSecondary'
})
class KbqUsernameSecondary {}
```

### KbqUsernameSecondaryHint (directive)

Styles a secondary hint.

```ts
@Directive({
    selector: '[kbqUsernameSecondaryHint]',
    exportAs: 'kbqUsernameSecondaryHint'
})
class KbqUsernameSecondaryHint {}
```

## Pipes

### KbqUsernameCustomPipe (pipe)

Formats a user profile into a name string.

A lowercase key renders an initial and an uppercase one the full value; every character with no
mapped field is emitted verbatim, so the format carries its own separators.

`KbqUsernamePipe` derives the form from a following `.` instead.

```ts
@Pipe({ name: 'kbqUsernameCustom' })
class KbqUsernameCustomPipe<T = unknown> implements PipeTransform {
    transform(
        profile: T,
        format?: string,
        customMapping?: KbqFormatKeyToProfileMappingExtended<T>
    ): string;
}
```

- `transform()`: `string` — Builds a formatted name string from the user profile using the provided format and mapping.

### KbqUsernamePipe (pipe)

Formats a user profile into a name string.

A key followed by `.` renders an initial and any other key renders the full value; the separating
spaces are inserted by the pipe, so a character that is neither a mapped key nor `.` is dropped.

`KbqUsernameCustomPipe` derives the form from the key's case instead, and keeps the format's own
punctuation.

```ts
@Pipe({ name: 'kbqUsername' })
class KbqUsernamePipe<T = unknown> implements PipeTransform {
    transform(
        profile: T,
        format?: string,
        customMapping?: KbqFormatKeyToProfileMapping
    ): string;
}
```

- `transform()`: `string` — Builds a formatted name string from the user profile using the provided format and mapping.

## Functions

### kbqBuildUsernameText (function)

Builds a full username string from a pre-formatted name plus optional login and site,
mirroring the text rendered by `kbq-username`.

Provide custom `formatLogin` / `formatSite` to tailor the output.

```ts
function kbqBuildUsernameText(
    data: { name: string } & Partial<Pick<KbqUserInfo, 'login' | 'site'>>,
    options?: KbqUsernameTextOptions
): string;
```

### kbqFormatUsername (function)

Formats a profile the way `KbqUsernamePipe` and `kbq-username` do: a key followed by
`KbqUsernameFormatKey.Dot` renders an initial, any other key renders the full value, and the
parts are joined with single spaces — a character that is neither a mapped key nor `.` is dropped.

Fields the profile does not carry are skipped one by one, so a partial profile still renders whatever
it has.

```ts
function kbqFormatUsername<T = unknown>(
    profile: T,
    format: string,
    mapping: KbqFormatKeyToProfileMapping | KbqFormatKeyToProfileMappingExtended
): string;
```

- `profile`: `T` — Profile to format. Anything that is not a record — null, a primitive, an array —
  formats as an empty string.
- `format`: `string` — Format string, e.g. `kbqDefaultFullNameFormat`.
- `mapping`: `KbqFormatKeyToProfileMapping | KbqFormatKeyToProfileMappingExtended` — Format key to profile field mapping, e.g. `kbqDefaultProfileMapping`. Resolve it
  from your own injection context — `inject(KBQ_PROFILE_MAPPING)` — to honour a scoped override.

### kbqFormatUsernameCustom (function)

Formats a profile the way `KbqUsernameCustomPipe` does: a lowercase key renders an initial, an
uppercase one the full value, and every character with no mapped field — the separators — is emitted
verbatim, which is what lets the format carry its own punctuation.

```ts
function kbqFormatUsernameCustom<T = unknown>(
    profile: T,
    format: string,
    mapping: KbqFormatKeyToProfileMapping | KbqFormatKeyToProfileMappingExtended
): string;
```

- `profile`: `T` — Profile to format. Anything that is not a record — null, a primitive, an array —
  formats as an empty string.
- `format`: `string` — Format string, e.g. `kbqDefaultFullNameFormatCustom`.
- `mapping`: `KbqFormatKeyToProfileMapping | KbqFormatKeyToProfileMappingExtended` — Format key to profile field mapping, e.g. `kbqDefaultProfileMapping`. Resolve it
  from your own injection context — `inject(KBQ_PROFILE_MAPPING)` — to honour a scoped override.

### kbqInjectUsernameFormatter (function)

Returns the formatter `kbq-username` renders with, bound to the `KBQ_PROFILE_MAPPING` visible at the
call site. Use it wherever the displayed string is needed outside a template — a search filter, an
option label — so that a scoped mapping applies to both paths.

Must run in an injection context.

```ts
function kbqInjectUsernameFormatter(): <T>(profile: T, format?: string) => string;
```

## Types

### KbqFormatKeyToProfileMapping (type)

Maps each format key to a property name in the user profile object.
Allows flexible formatting regardless of profile field names.

Excludes the uppercase keys: `KbqUsernamePipe` decides shortness from a following
`KbqUsernameFormatKey.Dot`, so a case pair would carry no meaning for it. The mapping that does
carry them is `KbqFormatKeyToProfileMappingExtended`.

```ts
type KbqFormatKeyToProfileMapping<T = any> = {
    [
        key in Exclude<
            KbqUsernameFormatKey,
            KbqUsernameFormatKey.FirstNameFull | KbqUsernameFormatKey.MiddleNameFull | KbqUsernameFormatKey.LastNameFull
        >
    ]: keyof T | undefined;
};
```

### KbqFormatKeyToProfileMappingExtended (type)

Maps each format key to a property name in the user profile object.
Allows flexible formatting regardless of profile field names.

Covers the uppercase keys as well, which is the pair `KbqUsernameCustomPipe` derives the form from.

```ts
type KbqFormatKeyToProfileMappingExtended<T = any> = {
    [key in KbqUsernameFormatKey]: keyof T | undefined;
};
```

### KbqUserInfo (type)

Basic user info rendered by `kbq-username`.

```ts
type KbqUserInfo = {
    firstName?: string;
    lastName?: string;
    middleName?: string;
    login?: string;
    site?: string;
};
```

- `firstName?`: `string` — Given name.
- `lastName?`: `string` — Family name.
- `middleName?`: `string` — Middle name or patronymic.
- `login?`: `string` — Account name, rendered as the secondary part.
- `site?`: `string` — Site the account belongs to, rendered in parentheses after the login.

### KbqUsernameMode (type)

Layout mode for displaying a username and applying text-ellipsis.

- `stacked`: Elements shown vertically.
- `inline`: Elements shown in one line. Text ellipsis is applied to both parts.
- `text`: `display: inline`, so the username flows inside a sentence; nothing clips it, hence no title.

```ts
type KbqUsernameMode = 'stacked' | 'inline' | 'text';
```

### KbqUsernameStyle (type)

Visual style of the username.

- `default`: standard styling with primary and secondary colors.
- `error`: error colors (e.g., red).
- `accented`: no color theming; emphasizes via typography only.
- `inherit`: inherits parent styles, no theming. For example, useful when using inside links.

```ts
type KbqUsernameStyle = 'default' | 'error' | 'accented' | 'inherit';
```

### KbqUsernameTextOptions (interface)

```ts
interface KbqUsernameTextOptions {
    formatLogin?: (login: string) => string;
    formatSite?: (site: string) => string;
    bidiIsolate?: boolean;
}
```

- `formatLogin?`: `(login: string) => string` — Formats the login segment. Defaults to identity.
- `formatSite?`: `(site: string) => string` — Formats the site segment.
  Defaults to wrapping in parentheses, matching kbq-username display.
- `bidiIsolate?`: `boolean` — Wraps every segment in `U+2068`/`U+2069` so that a right-to-left name keeps its order next to a
  left-to-right login, and the parentheses around the site stay attached to it.

  Off by default: the isolates are invisible but real characters, and this string is commonly fed to
  an `includes()` filter that would stop matching.

## Enums

### KbqUsernameFormatKey (enum)

Keys naming the profile field a format character stands for.

The key identifies the *field* only. Whether it renders in full or as an initial is decided by the
pipe that reads the format: `KbqUsernamePipe` abbreviates a key that is followed by `Dot`,
`KbqUsernameCustomPipe` abbreviates the lowercase keys. The two upper/lower pairs therefore exist so
that a single mapping serves both rules.

```ts
enum KbqUsernameFormatKey {
    FirstNameShort = 'f',
    FirstNameFull = 'F',
    MiddleNameShort = 'm',
    MiddleNameFull = 'M',
    LastNameShort = 'l',
    LastNameFull = 'L',
    Dot = '.'
}
```

- `FirstNameShort`: `'f'` — First name, abbreviated by `KbqUsernameCustomPipe`.
- `FirstNameFull`: `'F'` — First name, rendered in full by `KbqUsernameCustomPipe`.
- `MiddleNameShort`: `'m'` — Middle name, abbreviated by `KbqUsernameCustomPipe`.
- `MiddleNameFull`: `'M'` — Middle name, rendered in full by `KbqUsernameCustomPipe`.
- `LastNameShort`: `'l'` — Last name, abbreviated by `KbqUsernameCustomPipe`.
- `LastNameFull`: `'L'` — Last name, rendered in full by `KbqUsernameCustomPipe`.
- `Dot`: `'.'` — Abbreviation mark. `KbqUsernamePipe` abbreviates the key it follows; never maps to a field.

## Constants

### KBQ_PROFILE_MAPPING (const)

Injection token for providing a global username format-to-profile mapping.

Resolved where the `kbq-username` component or the pipe lives, so a component-level or route-level
provider applies to that subtree. Outside a template, resolve it from your own injection context —
`kbqInjectUsernameFormatter()` does exactly that.

```ts
const KBQ_PROFILE_MAPPING: InjectionToken<KbqFormatKeyToProfileMapping | KbqFormatKeyToProfileMappingExtended>;
```

### KBQ_USERNAME_DEFAULT_LOCALE_CONFIGURATION (const)

Default localized strings of `kbq-username`.

```ts
const KBQ_USERNAME_DEFAULT_LOCALE_CONFIGURATION: KbqUsernameLocaleConfiguration;
```

### KBQ_USERNAME_LOCALE_CONFIGURATION (const)

Localization configuration provider for `kbq-username`.

```ts
const KBQ_USERNAME_LOCALE_CONFIGURATION: InjectionToken<KbqUsernameLocaleConfiguration>;
```

### kbqDefaultFullNameFormat (const)

Default format of `KbqUsernamePipe` and of the `kbq-username` component: full last name, first and
middle names as initials.

Written in that pipe's own syntax — a key followed by `.` renders an initial, any other key renders
the full value, and the separating spaces are inserted by the pipe rather than by the format.

```ts
const kbqDefaultFullNameFormat: string;
```

### kbqDefaultFullNameFormatCustom (const)

Default format of `KbqUsernameCustomPipe`: full last name, first and middle names as initials.

Written in that pipe's own syntax — uppercase keys render the full value, lowercase ones an initial,
and every other character is copied verbatim, which is where the spaces and the dots come from.

```ts
const kbqDefaultFullNameFormatCustom: string;
```

### kbqDefaultProfileMapping (const)

Mapping of every format key onto a field of `KbqUserInfo`. Backs `KBQ_PROFILE_MAPPING`
and is the fallback of both pipes, so a profile shaped like `KbqUserInfo` needs no mapping at all.

Covers the uppercase keys as well, which is what makes `kbqDefaultFullNameFormatCustom` work out of
the box; spread it to map extra fields without losing the defaults.

```ts
const kbqDefaultProfileMapping: { f: "firstName"; F: "firstName"; m: "middleName"; M: "middleName"; l: "lastName"; L: "lastName"; ".": undefined; };
```

### kbqUsernameLocaleConfigurationProvider (const)

Utility provider. Only the strings you pass are overridden; the rest keep following the active locale.

Provides `KBQ_USERNAME_LOCALE_CONFIGURATION`.

```ts
const kbqUsernameLocaleConfigurationProvider: (
    configuration: KbqDeepPartial<KbqUsernameLocaleConfiguration>
) => Provider;
```
