You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.0 KiB
33 lines
1.0 KiB
import type { PrefsDimension } from '$libs/prefs';
|
|
import { unitSystemFromLocales, UNIT_SYSTEMS, type UnitSystem } from '$libs/units';
|
|
|
|
export interface UnitSystemDimensionOptions {
|
|
readonly catalog?: readonly UnitSystem[];
|
|
readonly default?: UnitSystem;
|
|
}
|
|
|
|
export function unitSystemDimension(
|
|
options: UnitSystemDimensionOptions = {}
|
|
): PrefsDimension<UnitSystem> {
|
|
const catalog = options.catalog ?? UNIT_SYSTEMS;
|
|
const fallback = options.default ?? 'metric';
|
|
return {
|
|
defaultValue: fallback,
|
|
validate(value) {
|
|
if (typeof value !== 'string') return { ok: false, reason: 'unsupported_unit_system' };
|
|
if (!catalog.includes(value as UnitSystem)) {
|
|
return { ok: false, reason: 'unsupported_unit_system' };
|
|
}
|
|
return { ok: true, value: value as UnitSystem };
|
|
},
|
|
resolve(intent, env) {
|
|
if (intent !== undefined) return intent;
|
|
if (env.unitSystem !== undefined && catalog.includes(env.unitSystem)) {
|
|
return env.unitSystem;
|
|
}
|
|
return unitSystemFromLocales(env.locales ?? [], catalog, fallback);
|
|
},
|
|
catalog: () => catalog
|
|
};
|
|
}
|