@formatjs/intl-supportedvaluesof

中文文档
3.0.1发布于 2026/11/09MITTypeScriptES Module构建来源已验证近 30 天下载 319.7万

Intl.supportedValuesOf polyfill

原始使用文档
文档目录23 节
  1. ECMA-402 Spec Compliance
  2. Installation
  3. Usage
  4. As Polyfill
  5. Direct Import
  6. Supported Keys
  7. API
  8. supportedValuesOf(key: string): string[]
  9. Implementation Details
  10. How It Works
  11. Example: Calendar Validation
  12. TypeScript Support
  13. Browser Support
  14. Breaking Changes in v3.0
  15. Removed: Non-standard locale parameter
  16. Migration Guide
  17. Examples
  18. Check if a calendar is supported
  19. Display all supported currencies
  20. Validate user input
  21. License
  22. Related Packages
  23. Resources

@formatjs/intl-supportedvaluesof

Polyfill for Intl.supportedValuesOf()

npm Version

ECMA-402 Spec Compliance

This package implements the ECMA-402 Intl.supportedValuesOf() specification:

  • Spec Section: 8.3.2 Intl.supportedValuesOf(key)
  • Returns: Supported values for internationalization keys
  • Fully spec-compliant: No optional parameters, matches native browser behavior

Installation

npm install @formatjs/intl-supportedvaluesof

Usage

As Polyfill

Import the polyfill to make Intl.supportedValuesOf available globally:

import '@formatjs/intl-supportedvaluesof/polyfill'

// Now available on the global Intl object
const calendars = Intl.supportedValuesOf('calendar')
// ['buddhist', 'chinese', 'coptic', 'dangi', 'ethioaa', 'ethiopic', 'gregory', ...]

const currencies = Intl.supportedValuesOf('currency')
// ['AED', 'AFN', 'ALL', 'AMD', 'ANG', 'AOA', 'ARS', 'AUD', ...]

const timeZones = Intl.supportedValuesOf('timeZone')
// ['Africa/Abidjan', 'Africa/Accra', ..., 'Pacific/Wallis']

Direct Import

Import the function directly without modifying the global namespace:

import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'

const numberingSystems = supportedValuesOf('numberingSystem')
// ['adlm', 'ahom', 'arab', 'arabext', 'bali', 'beng', 'bhks', ...]

const units = supportedValuesOf('unit')
// ['acre', 'bit', 'byte', 'celsius', 'centimeter', 'day', 'degree', ...]

Supported Keys

The function accepts one of six string keys and returns an array of supported values:

KeyReturnsExample Values
'calendar'Supported calendar systems'gregory', 'islamic', 'buddhist', 'chinese'
'collation'Supported collation algorithms'emoji', 'eor', 'phonebk', 'search'
'currency'Supported ISO 4217 currency codes'USD', 'EUR', 'JPY', 'GBP'
'numberingSystem'Supported numbering systems'arab', 'latn', 'hanidec', 'thai'
'timeZone'Supported IANA time zones'America/New_York', 'Europe/London', 'Asia/Tokyo'
'unit'Supported measurement units'meter', 'celsius', 'liter', 'acre'

API

supportedValuesOf(key: string): string[]

Returns an array of supported values for the given key.

Parameters:

  • key - One of: 'calendar', 'collation', 'currency', 'numberingSystem', 'timeZone', 'unit'

Returns:

  • A sorted array of unique string values

Throws:

  • RangeError - If the key is not one of the supported values

Example:

import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'

// Valid usage
const calendars = supportedValuesOf('calendar')
console.log(calendars) // ['buddhist', 'chinese', 'coptic', ...]

// Invalid key throws RangeError
try {
  supportedValuesOf('invalid')
} catch (e) {
  console.error(e) // RangeError: Invalid key: invalid
}

Implementation Details

This polyfill dynamically validates candidate values against the actual Intl implementation rather than maintaining static lists. This approach ensures:

  • Accuracy: Results match what your JavaScript runtime actually supports
  • Runtime-specific: Different engines (V8, JavaScriptCore, SpiderMonkey) may support different values
  • Up-to-date: No need to manually update lists when new values are added to engines

How It Works

For each category, the implementation:

  1. Loads candidate values from CLDR (Unicode Common Locale Data Repository)
  2. Tests each value by attempting to create an appropriate Intl formatter with that value
  3. Validates acceptance by checking if the formatter actually uses the requested value
  4. Returns filtered list of only the values that passed validation

Example: Calendar Validation

// Implementation tests if a calendar is supported:
function isSupportedCalendar(calendar) {
  try {
    // Attempt to create DateTimeFormat with this calendar
    const formatter = new Intl.DateTimeFormat(`en-u-ca-${calendar}`)
    // Verify the calendar was actually accepted
    const resolvedOptions = formatter.resolvedOptions()
    return resolvedOptions.calendar === calendar
  } catch {
    return false
  }
}

This ensures that only calendars actually supported by your runtime are returned, not just a static list.

TypeScript Support

This package includes TypeScript type definitions. When using as a polyfill, the global Intl namespace is automatically augmented:

import '@formatjs/intl-supportedvaluesof/polyfill'

// TypeScript knows about Intl.supportedValuesOf
const calendars: string[] = Intl.supportedValuesOf('calendar')

// TypeScript will error on invalid keys
Intl.supportedValuesOf('invalid') // Error: Argument of type '"invalid"' is not assignable...

Browser Support

This polyfill is only needed for older browsers that don't have native support for Intl.supportedValuesOf(). Native support is available in:

  • Chrome 99+
  • Edge 99+
  • Firefox 93+
  • Safari 15.4+

The polyfill requires the following Intl APIs to be available:

  • Intl.DateTimeFormat (for calendar, timeZone, numberingSystem)
  • Intl.NumberFormat (for currency, unit, numberingSystem)
  • Intl.Collator (for collation)

Breaking Changes in v3.0

Removed: Non-standard locale parameter

v2.x and earlier had a non-standard optional locale parameter that filtered results by locale. This has been removed to match the ECMA-402 specification.

// ❌ v2.x (non-standard, no longer works)
const calendars = supportedValuesOf('calendar', 'en-US')

// ✅ v3.0 (spec-compliant)
const calendars = supportedValuesOf('calendar')

The function now always returns ALL supported values, matching native browser behavior.

Migration Guide

If you need locale-specific filtering in v3.0+, implement it yourself:

import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'

// Get all supported calendars
const allCalendars = supportedValuesOf('calendar')

// Filter by what a specific locale actually uses/accepts
const enUSCalendars = allCalendars.filter(calendar => {
  try {
    const formatter = new Intl.DateTimeFormat('en-US', {calendar})
    return formatter.resolvedOptions().calendar === calendar
  } catch {
    return false
  }
})

However, note that most use cases don't need locale-specific filtering, as the spec's design returns all values that any locale could use.

Examples

Check if a calendar is supported

import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'

const calendars = supportedValuesOf('calendar')

if (calendars.includes('islamic')) {
  console.log('Islamic calendar is supported')
  const formatter = new Intl.DateTimeFormat('en-US', {calendar: 'islamic'})
  console.log(formatter.format(new Date()))
}

Display all supported currencies

import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'

const currencies = supportedValuesOf('currency')

console.log(`This runtime supports ${currencies.length} currencies:`)
currencies.forEach(currency => {
  const formatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency,
  })
  console.log(`${currency}: ${formatter.format(100)}`)
})

Validate user input

import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'

function isValidTimeZone(timeZone) {
  const supportedTimeZones = supportedValuesOf('timeZone')
  return supportedTimeZones.includes(timeZone)
}

console.log(isValidTimeZone('America/New_York')) // true
console.log(isValidTimeZone('Invalid/TimeZone')) // false

License

MIT

Resources

版本与发布记录

@formatjs/intl-supportedvaluesof 共发布 20 个版本,首次发布于 2026/01/15;过去 12 个月发布了 20 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.1latest2026/11/097 天前
3.0.02026/11/0115 天前
2.5.02026/10/3116 天前
2.4.02026/10/3016 天前
2.3.112026/09/241 个月前
2.3.102026/09/122 个月前

关于 @formatjs/intl-supportedvaluesof 的常见问题

@formatjs/intl-supportedvaluesof 是什么?

Intl.supportedValuesOf polyfill

如何安装 @formatjs/intl-supportedvaluesof?

在项目目录运行 npm install @formatjs/intl-supportedvaluesof,或使用 pnpm add / yarn add / bun add。

@formatjs/intl-supportedvaluesof 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@formatjs/intl-supportedvaluesof 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@formatjs/intl-supportedvaluesof 可以商用吗?

@formatjs/intl-supportedvaluesof 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@formatjs/intl-supportedvaluesof 的下载量有多少?

根据 npm 官方下载统计,@formatjs/intl-supportedvaluesof 在 2026/10/16 – 2026/11/14 这 30 天内被下载 3,197,270 次,日均约 106,576 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@formatjs/intl-supportedvaluesof 还在维护吗?

最新版本 3.0.1 发布于 2026/11/09(7 天前);至今共发布 20 个版本,其中过去 12 个月发布了 20 个;首次发布于 2026/01/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。