From d7fba0964b71979ebc758a8dae6f3c8817122632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=9C=A8=20Audrey=20=E2=9C=A8?= Date: Mon, 21 Apr 2025 15:05:44 -0400 Subject: [PATCH] extract `hasRangeOfValues`; more docs --- .../src/password-settings.component.ts | 8 +++-- libs/tools/generator/components/src/util.ts | 32 ++++++++++--------- .../core/src/types/algorithm-info.ts | 2 ++ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/libs/tools/generator/components/src/password-settings.component.ts b/libs/tools/generator/components/src/password-settings.component.ts index 42eae6d1ea9..6ba345a3873 100644 --- a/libs/tools/generator/components/src/password-settings.component.ts +++ b/libs/tools/generator/components/src/password-settings.component.ts @@ -20,6 +20,8 @@ import { BuiltIn, } from "@bitwarden/generator-core"; +import { hasRangeOfValues } from "./util"; + const Controls = Object.freeze({ length: "length", uppercase: "uppercase", @@ -152,18 +154,18 @@ export class PasswordSettingsComponent implements OnInit, OnChanges, OnDestroy { this.policyInEffect = constraints.policyInEffect ?? false; const toggles = [ - [Controls.length, (constraints.length?.min ?? 0) < (constraints.length?.max ?? 1)], + [Controls.length, hasRangeOfValues(constraints.length?.min, constraints.length?.max)], [Controls.uppercase, !constraints.uppercase?.readonly], [Controls.lowercase, !constraints.lowercase?.readonly], [Controls.number, !constraints.number?.readonly], [Controls.special, !constraints.special?.readonly], [ Controls.minNumber, - (constraints.minNumber?.min ?? 0) < (constraints.minNumber?.max ?? 1), + hasRangeOfValues(constraints.minNumber?.min, constraints.minNumber?.max), ], [ Controls.minSpecial, - (constraints.minSpecial?.min ?? 0) < (constraints.minSpecial?.max ?? 1), + hasRangeOfValues(constraints.minSpecial?.min, constraints.minSpecial?.max), ], ] as [keyof typeof Controls, boolean][]; diff --git a/libs/tools/generator/components/src/util.ts b/libs/tools/generator/components/src/util.ts index 0f31742fa36..4b0ce4383de 100644 --- a/libs/tools/generator/components/src/util.ts +++ b/libs/tools/generator/components/src/util.ts @@ -1,23 +1,9 @@ -import { distinctUntilChanged, map, pairwise, pipe, skipWhile, startWith, takeWhile } from "rxjs"; - import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { I18nKeyOrLiteral } from "@bitwarden/common/tools/types"; import { isI18nKey } from "@bitwarden/common/tools/util"; -import { UserId } from "@bitwarden/common/types/guid"; import { AlgorithmInfo, AlgorithmMetadata } from "@bitwarden/generator-core"; -export function completeOnAccountSwitch() { - return pipe( - map(({ id }: { id: UserId | null }) => id), - skipWhile((id) => !id), - startWith(null as UserId | null), - pairwise(), - takeWhile(([prev, next]) => (prev ?? next) === next), - map(([_, id]) => id), - distinctUntilChanged(), - ); -} - +/** Adapts {@link AlgorithmMetadata} to legacy {@link AlgorithmInfo} structure. */ export function toAlgorithmInfo(metadata: AlgorithmMetadata, i18n: I18nService) { const info: AlgorithmInfo = { id: metadata.id, @@ -39,6 +25,22 @@ export function toAlgorithmInfo(metadata: AlgorithmMetadata, i18n: I18nService) return info; } +/** Translates an internationalization key + * @param key the key to translate + * @param i18n the service providing translations + * @returns the translated key; if the key is a literal the literal + * is returned instead. + */ export function translate(key: I18nKeyOrLiteral, i18n: I18nService) { return isI18nKey(key) ? i18n.t(key) : key.literal; } + +/** Returns true when min < max + * @param min the minimum value to check; when this is nullish it becomes 0. + * @param max the maximum value to check; when this is nullish it becomes +Infinity. + */ +export function hasRangeOfValues(min?: number, max?: number) { + const minimum = min ?? 0; + const maximum = max ?? Number.POSITIVE_INFINITY; + return minimum < maximum; +} diff --git a/libs/tools/generator/core/src/types/algorithm-info.ts b/libs/tools/generator/core/src/types/algorithm-info.ts index 9fd1939c504..a3db03600b6 100644 --- a/libs/tools/generator/core/src/types/algorithm-info.ts +++ b/libs/tools/generator/core/src/types/algorithm-info.ts @@ -1,5 +1,7 @@ import { CredentialAlgorithm, CredentialType } from "../metadata"; +// FIXME: deprecate or delete `AlgorithmInfo` once a better translation +// strategy is identified. export type AlgorithmInfo = { /** Uniquely identifies the credential configuration * @example