Files
aliasvault/apps/mobile-app/utils/PasswordLengthSlider.ts
dependabot[bot] 423330c864 Upgrade mobile app Expo and browser extension WXT frameworks (#2067)
* Bump the npm_and_yarn group across 2 directories with 2 updates

Bumps the npm_and_yarn group with 1 update in the /apps/browser-extension directory: [ws](https://github.com/websockets/ws).
Bumps the npm_and_yarn group with 1 update in the /apps/mobile-app directory: [postcss](https://github.com/postcss/postcss).


Updates `ws` from 8.18.2 to 8.21.0
- [Release notes](https://github.com/websockets/ws/releases)
- [Commits](https://github.com/websockets/ws/compare/8.18.2...8.21.0)

Updates `ws` from 8.18.2 to 8.21.0
- [Release notes](https://github.com/websockets/ws/releases)
- [Commits](https://github.com/websockets/ws/compare/8.18.2...8.21.0)

Updates `postcss` from 8.4.49 to 8.5.15
- [Release notes](https://github.com/postcss/postcss/releases)
- [Changelog](https://github.com/postcss/postcss/blob/main/CHANGELOG.md)
- [Commits](https://github.com/postcss/postcss/compare/8.4.49...8.5.15)

Updates `postcss` from 8.4.49 to 8.5.15
- [Release notes](https://github.com/postcss/postcss/releases)
- [Changelog](https://github.com/postcss/postcss/blob/main/CHANGELOG.md)
- [Commits](https://github.com/postcss/postcss/compare/8.4.49...8.5.15)

---
updated-dependencies:
- dependency-name: postcss
  dependency-version: 8.5.15
  dependency-type: indirect
- dependency-name: ws
  dependency-version: 8.21.0
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>

* Update package-lock.json (#2067)

* Update NodeJS to latest 24.x LTS

* Revert Expo back to 53.x (#2067)

* Upgrade expo and react-native (#2067)

* Update mobile app to support Expo 56 (#2067)

* Update wxt (#2067)

* Change util filenames to be consistent PascalCase (#2067)

* Restore deep-linking behavior to work with new Expo Router (#2067)

* Update eslint.config.js (#2067)

* Update package-lock.json (#2067)

* Refactor linting (#2067)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Leendert de Borst <ldeborst@xivisoft.com>
2026-05-26 10:47:39 +02:00

58 lines
1.9 KiB
TypeScript

/**
* Utility functions for password length slider with non-linear scaling.
*
* The slider uses a power curve to provide fine-grained control at lower values
* (where most users operate, e.g., 12-32 chars) and coarser control at higher values
* (64-256 chars).
*
* This makes it easy to select common password lengths while still allowing
* very long passwords when needed.
*/
/** Minimum password length */
export const MIN_PASSWORD_LENGTH = 8;
/** Maximum password length */
export const MAX_PASSWORD_LENGTH = 256;
/** Slider minimum value (internal representation) */
export const SLIDER_MIN = 0;
/** Slider maximum value (internal representation) */
export const SLIDER_MAX = 100;
/**
* Exponent for the power curve.
* Higher values = more precision at lower lengths.
* 2.0 gives a good balance where ~50% slider = ~70 chars
*/
const EXPONENT = 2.0;
/**
* Convert a slider position (0-100) to an actual password length (8-256).
* Uses a power curve for non-linear scaling.
*
* @param sliderValue - The slider position (0-100)
* @returns The password length (8-256)
*/
export function sliderToLength(sliderValue: number): number {
const normalized = Math.max(0, Math.min(1, sliderValue / SLIDER_MAX));
const curved = Math.pow(normalized, EXPONENT);
const length = MIN_PASSWORD_LENGTH + curved * (MAX_PASSWORD_LENGTH - MIN_PASSWORD_LENGTH);
return Math.round(length);
}
/**
* Convert a password length (8-256) to a slider position (0-100).
* Inverse of sliderToLength.
*
* @param length - The password length (8-256)
* @returns The slider position (0-100)
*/
export function lengthToSlider(length: number): number {
const clampedLength = Math.max(MIN_PASSWORD_LENGTH, Math.min(MAX_PASSWORD_LENGTH, length));
const normalized = (clampedLength - MIN_PASSWORD_LENGTH) / (MAX_PASSWORD_LENGTH - MIN_PASSWORD_LENGTH);
const curved = Math.pow(normalized, 1 / EXPONENT);
return curved * SLIDER_MAX;
}