UNPKG

@formatjs/intl-relativetimeformat

Version:

Formats JavaScript dates to relative time strings.

22 lines (21 loc) 770 B
import { match } from "@formatjs/intl-localematcher"; import { supportedLocales } from "./supported-locales.generated.js"; function supportedLocalesOf(locale) { if (!locale) { return true; } const locales = Array.isArray(locale) ? locale : [locale]; return Intl.RelativeTimeFormat.supportedLocalesOf(locales).length === locales.length; } function hasResolvedOptionsNumberingSystem(locale) { try { return "numberingSystem" in new Intl.RelativeTimeFormat(locale || "en", { numeric: "auto" }).resolvedOptions(); } catch { return false; } } export function shouldPolyfill(locale = "en") { if (!("RelativeTimeFormat" in Intl) || !supportedLocalesOf(locale) || !hasResolvedOptionsNumberingSystem(locale)) { return match([locale], supportedLocales, "en"); } }