mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-17 04:55:42 +09:00
refactor(pkgs): share vitest config and dedupe helpers (#3384)
This commit is contained in:
1 parent
5695510980
commit
575a5adbef
43 files changed
+172
-874
No files matched your search
@@ -1,27 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
const ASCII_UPPER_A = 65;
|
||||
const ASCII_UPPER_Z = 90;
|
||||
const REGION_CODE_LENGTH = 2;
|
||||
|
||||
function isAsciiUpperAlpha2(value: string): boolean {
|
||||
return (
|
||||
value.length === REGION_CODE_LENGTH &&
|
||||
value.charCodeAt(0) >= ASCII_UPPER_A &&
|
||||
value.charCodeAt(0) <= ASCII_UPPER_Z &&
|
||||
value.charCodeAt(1) >= ASCII_UPPER_A &&
|
||||
value.charCodeAt(1) <= ASCII_UPPER_Z
|
||||
);
|
||||
}
|
||||
|
||||
export function normalizeRegionCode(regionCode: string): string | undefined {
|
||||
const trimmedRegionCode = regionCode.trim();
|
||||
if (trimmedRegionCode.length !== REGION_CODE_LENGTH) {
|
||||
return undefined;
|
||||
}
|
||||
const upperRegionCode = trimmedRegionCode.toUpperCase();
|
||||
if (!isAsciiUpperAlpha2(upperRegionCode)) {
|
||||
return undefined;
|
||||
}
|
||||
return upperRegionCode;
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {Locales} from '@fluxer/constants/src/Locales';
|
||||
import {normalizeRegionCode} from '@fluxer/geo_utils/src/RegionCodeValidation';
|
||||
|
||||
const DISPLAY_NAME_TYPE: Intl.DisplayNamesOptions['type'] = 'region';
|
||||
const DISPLAY_NAME_FALLBACK: Intl.DisplayNamesOptions['fallback'] = 'none';
|
||||
const displayNamesByLocale = new Map<string, Intl.DisplayNames>();
|
||||
|
||||
function resolveLocale(locale?: string): string {
|
||||
const trimmedLocale = locale?.trim();
|
||||
if (trimmedLocale && trimmedLocale.length > 0) {
|
||||
return trimmedLocale;
|
||||
}
|
||||
return Locales.EN_US;
|
||||
}
|
||||
|
||||
function getDisplayNames(locale?: string): Intl.DisplayNames {
|
||||
const localeCode = resolveLocale(locale);
|
||||
const cachedDisplayNames = displayNamesByLocale.get(localeCode);
|
||||
if (cachedDisplayNames) {
|
||||
return cachedDisplayNames;
|
||||
}
|
||||
const displayNames = new Intl.DisplayNames([localeCode], {
|
||||
type: DISPLAY_NAME_TYPE,
|
||||
fallback: DISPLAY_NAME_FALLBACK,
|
||||
});
|
||||
displayNamesByLocale.set(localeCode, displayNames);
|
||||
return displayNames;
|
||||
}
|
||||
|
||||
function resolveRegionDisplayNameFromFormatter(
|
||||
regionCode: string,
|
||||
displayNames: Intl.DisplayNames,
|
||||
): string | undefined {
|
||||
const normalizedRegionCode = normalizeRegionCode(regionCode);
|
||||
if (!normalizedRegionCode) {
|
||||
return undefined;
|
||||
}
|
||||
return displayNames.of(normalizedRegionCode) ?? undefined;
|
||||
}
|
||||
|
||||
export function resolveRegionDisplayName(regionCode: string, locale?: string): string | undefined {
|
||||
const displayNames = getDisplayNames(locale);
|
||||
return resolveRegionDisplayNameFromFormatter(regionCode, displayNames);
|
||||
}
|
||||
@@ -1,33 +1,28 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {normalizeRegionCode} from '@fluxer/geo_utils/src/RegionCodeValidation';
|
||||
import {resolveRegionDisplayName} from '@fluxer/geo_utils/src/RegionDisplayNameResolver';
|
||||
import {Locales} from '@fluxer/constants/src/Locales';
|
||||
|
||||
interface RegionDisplayNameOptions {
|
||||
locale?: string;
|
||||
fallbackToRegionCode?: boolean;
|
||||
const displayNamesByLocale = new Map<string, Intl.DisplayNames>();
|
||||
|
||||
function getDisplayNames(locale?: string): Intl.DisplayNames {
|
||||
const localeCode = locale?.trim() || Locales.EN_US;
|
||||
let displayNames = displayNamesByLocale.get(localeCode);
|
||||
if (!displayNames) {
|
||||
displayNames = new Intl.DisplayNames([localeCode], {type: 'region', fallback: 'none'});
|
||||
displayNamesByLocale.set(localeCode, displayNames);
|
||||
}
|
||||
return displayNames;
|
||||
}
|
||||
|
||||
function applyRegionCodeFallback(
|
||||
regionCode: string,
|
||||
displayName: string | undefined,
|
||||
options?: RegionDisplayNameOptions,
|
||||
): string | undefined {
|
||||
if (displayName) {
|
||||
return displayName;
|
||||
}
|
||||
if (!options?.fallbackToRegionCode) {
|
||||
export function getRegionDisplayName(regionCode: string, options?: {locale?: string}): string | undefined {
|
||||
const displayNames = getDisplayNames(options?.locale);
|
||||
const trimmedRegionCode = regionCode.trim();
|
||||
if (trimmedRegionCode.length !== 2) {
|
||||
return undefined;
|
||||
}
|
||||
const normalizedRegionCode = normalizeRegionCode(regionCode);
|
||||
if (normalizedRegionCode) {
|
||||
return normalizedRegionCode;
|
||||
const upperRegionCode = trimmedRegionCode.toUpperCase();
|
||||
if (!/^[A-Z]{2}$/.test(upperRegionCode)) {
|
||||
return undefined;
|
||||
}
|
||||
const trimmedRegionCode = regionCode.trim();
|
||||
return trimmedRegionCode.length > 0 ? trimmedRegionCode : undefined;
|
||||
}
|
||||
|
||||
export function getRegionDisplayName(regionCode: string, options?: RegionDisplayNameOptions): string | undefined {
|
||||
const displayName = resolveRegionDisplayName(regionCode, options?.locale);
|
||||
return applyRegionCodeFallback(regionCode, displayName, options);
|
||||
return displayNames.of(upperRegionCode) || undefined;
|
||||
}
|
||||
Reference in new issue
Block a user