31 lines
1.2 KiB
JavaScript
31 lines
1.2 KiB
JavaScript
/**
|
|
* Resolves a possibly-localized value to a string for the given locale.
|
|
* Payload returns a localized field as a plain string (current locale) OR, with
|
|
* locale:'all' / unflattened docs, as a { pl: '…', en: '…' } map. This handles
|
|
* both, with a safe fallback to the first available value — replacing the
|
|
* hand-rolled `getSafeString` with `any` copied into every project (JSON-LD,
|
|
* rich text, form topics, …).
|
|
*
|
|
* import { resolveLocalized } from '@intecion/ipal-kit'
|
|
* const name = resolveLocalized(item.label, locale) // '' if nothing
|
|
* const city = resolveLocalized(company.city, locale, '—') // custom fallback
|
|
*/ export function resolveLocalized(value, locale, fallback = '') {
|
|
if (value == null) {
|
|
return fallback;
|
|
}
|
|
if (typeof value === 'string') {
|
|
return value;
|
|
}
|
|
if (typeof value === 'object') {
|
|
const map = value;
|
|
const forLocale = map[locale];
|
|
if (typeof forLocale === 'string') {
|
|
return forLocale;
|
|
}
|
|
const first = Object.values(map).find((v)=>typeof v === 'string');
|
|
return first ?? fallback;
|
|
}
|
|
return String(value);
|
|
}
|
|
|
|
//# sourceMappingURL=resolveLocalized.js.map
|