@formatjs/intl-supportedvaluesof

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

内置支持的多填充值

中文使用文档
文档目录23 节
  1. ECMA-402 光谱遵守
  2. 安装
  3. 使用量
  4. 作为多填充
  5. 直接导入
  6. 支持的密钥
  7. API 密码
  8. supportedValuesOf(key: string): string[]
  9. 执行细节
  10. 如何运作
  11. 示例:日历验证
  12. 类型脚本支持
  13. 浏览器支持
  14. 正在突破 v3. 0 的更改
  15. 删除:非标准 locale 参数
  16. 移徙指南
  17. 实例
  18. 检查是否支持日历
  19. 显示所有支持的货币
  20. 验证用户输入
  21. 许可证
  22. 相关软件包
  23. 资源

@formatjs/intl-支持值f

多元填充 Intl.supportedValuesOf()

npm Version

ECMA-402 光谱遵守

此套件执行 ECMA-402 Intl.supportedValuesOf() 规格 :

安装

npm install @formatjs/intl-supportedvaluesof

使用量

作为多填充

导入多填充器 Intl.supportedValuesOf 全球现有:

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']

直接导入

直接导入函数而不修改全局命名空间 :

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', ...]

支持的密钥

该函数接受六个字符串密钥中的一个并返回一系列支持的值:

密钥回返示例数值
'calendar'支持的日历系统'gregory', (中文(简体) ), 'islamic', (中文(简体) ), 'buddhist', (中文(简体) ), 'chinese'
'collation'支持的整理算法'emoji', (中文(简体) ), 'eor', (中文(简体) ), 'phonebk', (中文(简体) ), 'search'
'currency'支持的 ISO 4217 货币代码'USD', (中文(简体) ), 'EUR', (中文(简体) ), 'JPY', (中文(简体) ), 'GBP'
'numberingSystem'支持的编号系统'arab', (中文(简体) ), 'latn', (中文(简体) ), 'hanidec', (中文(简体) ), 'thai'
'timeZone'支持的IANA时区'America/New_York', (中文(简体) ), 'Europe/London', (中文(简体) ), 'Asia/Tokyo'
'unit'支助的计量单位'meter', (中文(简体) ), 'celsius', (中文(简体) ), 'liter', (中文(简体) ), 'acre'

API 密码

supportedValuesOf(key: string): string[]

返回给定密钥的一系列支持值.

参数:

  • key - 一个: 'calendar', (中文(简体) ), 'collation', (中文(简体) ), 'currency', (中文(简体) ), 'numberingSystem', (中文(简体) ), 'timeZone', (中文(简体) ), 'unit'

回返情况:

  • 排序的独有字符串值阵列

掷出 :

  • RangeError - 如果密钥不是支持的值之一

示例

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
}

执行细节

这种多填充 动态验证 候选人数值与实际 Intl 而不是维持静态清单。 这种办法确保:

  • 准确性: 结果匹配您的 JavaScript 运行时间
  • 特定运行时间: 不同的引擎( V8, JavaScriptCore, SpiderMonkey) 可能支持不同的值
  • 最新数据: 引擎添加新值时不需要手动更新列表

如何运作

每一类别的执行情况:

  1. 装入候选值 出自QQR( Unicode 通用地块数据存储器)
  2. 测试每个值 试图创建适当的 Intl 用于含有该值的事项
  3. 验证接受 检查是否实际使用请求的值
  4. 返回过滤列表 只有通过验证的值

示例:日历验证

// 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
  }
}

这只保证日历 实际支持 由您的运行时间返回,而不仅仅是一个静态列表.

类型脚本支持

此包包括了TypeScript类型定义. 当使用多填充时,全局 Intl 命名空间自动增强 :

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...

浏览器支持

这种多填充只针对没有本地支持的更老浏览器 Intl.supportedValuesOf()土著支助有:

  • 99克罗姆克
  • 边缘99+
  • Firefox 93+ (英语)
  • 萨法里15.4+

混合填充需要下列条件: Intl 可用的 API :

  • Intl.DateTimeFormat (计: calendar, (中文(简体) ), timeZone, (中文(简体) ), numberingSystem(中文(简体) )
  • Intl.NumberFormat (计: currency, (中文(简体) ), unit, (中文(简体) ), numberingSystem(中文(简体) )
  • Intl.Collator (计: collation(中文(简体) )

正在突破 v3. 0 的更改

删除:非标准 locale 参数

v2.x 及更早时 有非标准选项 locale 按语言过滤结果的参数。 这是 删除 与ECMA-402规格相匹配.

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

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

函数现在总是返回 全体 支持的值,匹配本地浏览器行为.

移徙指南

如果您在 v3. 0+ 中需要指定本地端过滤, 请自行执行 :

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
  }
})

然而,请注意,大多数使用案例不需要特定本地化过滤,因为光谱的设计返回了所有值 任意 当地人可以使用.

实例

检查是否支持日历

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()))
}

显示所有支持的货币

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)}`)
})

验证用户输入

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

许可证

麻省理工学院

资源

版本与发布记录

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

版本发布日期距今
3.0.1latest2026/11/096 天前
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 是什么?

内置支持的多填充值

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