@financica/iso4217

中文文档
0.2.0发布于 2026/08/18MITTypeScriptESM + CJS零依赖近 30 天下载 11万

ISO 4217 currency codes, numeric codes, minor units, and entities — fully typed, zero dependencies, O(1) lookups.

原始使用文档
文档目录13 节
  1. Install
  2. Usage
  3. Type guards
  4. Historic currencies
  5. API
  6. Lookups
  7. Type guards
  8. Formatting
  9. Data
  10. Types
  11. Data provenance
  12. Releasing a new revision
  13. License

@financica/iso4217

Strictly typed, zero-dependency ISO 4217 currency dataset with O(1) lookups.

Built from SIX Group's official list-one.xml, revision 2026-01-01: alphabetic code, numeric code, minor units, name, fund flag and kind. Country associations are ISO 3166-1 alpha-2 codes, never opaque name strings. Every code is a literal union, so the compiler tells "USD" from "usd" and "US" from "USA". All exported data is Object.freezed and every array is readonly. Ships ESM + CJS and tree-shakes.

Withdrawn currencies back to the 1970s ship in an opt-in /historic subpath.

Install

npm install @financica/iso4217

Requires Node ≥ 24.

Usage

import {
    getByCode,
    getByNumericCode,
    getByCountry,
    formatAmount,
    CURRENCIES,
    PUBLISHED_AT,
} from "@financica/iso4217";

getByCode("USD");
// → {
//     alphabeticCode: "USD",
//     numericCode: "840",
//     minorUnits: 2,
//     name: "US Dollar",
//     countryCodes: ["AS", "BQ", "EC", "GU", "HT", "IO", "MH", "MP", "PA",
//                    "PR", "PW", "SV", "TC", "TL", "UM", "US", "VG", "VI"],
//     isFund: false,
//     kind: "fiat",
//   }

getByCode("usd")?.alphabeticCode; // "USD"  — case-insensitive
getByNumericCode("840")?.alphabeticCode; // "USD"
getByNumericCode(840)?.alphabeticCode; // "USD"
getByNumericCode(8)?.alphabeticCode; // "ALL" — unpadded input is normalised

getByCountry("CH").map((c) => c.alphabeticCode);
// ["CHE", "CHF", "CHW"]

formatAmount(1234.5, "USD", { locale: "en-US" }); // "$1,234.50"
formatAmount(1234.5, "JPY", { locale: "en-US" }); // "¥1,235"     (0 minor units)
formatAmount(1.5, "BHD", { locale: "en-US" }); // "BHD 1.500"  (3 minor units)

CURRENCIES.length; // 178 (as of 2026-01-01)
PUBLISHED_AT; // "2026-01-01"

Type guards

import { isAlphabeticCode, isNumericCode, isCountryCode } from "@financica/iso4217";

function logCurrency(code: string) {
    if (!isAlphabeticCode(code)) return;
    // `code` is now narrowed to AlphabeticCode.
    console.log(getByCode(code)?.name);
}

Historic currencies

Withdrawn currencies (Italian Lira, Zaire Zaïre, Netherlands Antillean Guilder, etc.) live behind a subpath import so they don't bloat the main bundle.

import { getHistoricByCode, HISTORIC_CURRENCIES } from "@financica/iso4217/historic";

getHistoricByCode("ANG").map((c) => c.withdrawalDate);
// ["2010-10", "2025-03"]  — ANG was withdrawn twice.

API

Lookups

FunctionReturns
getByCode(code: string)Currency | undefined
getByNumericCode(code: string | number)Currency | undefined
getByCountry(countryCode: string)readonly Currency[] (possibly empty)

Type guards

FunctionSignature
isAlphabeticCode(s)s is AlphabeticCode
isNumericCode(s)s is NumericCode
isCountryCode(s)s is CountryCode

Formatting

FunctionSignature
formatAmount(amount, code, options?)string (wraps Intl.NumberFormat)

Stripe's minor-unit classification is not ISO 4217 (e.g. UGX, ISK, HUF); for amounts crossing the Stripe boundary use the helpers in @financica/stripe-hosted-invoices.

Data

ExportShape
CURRENCIESreadonly Currency[] — sorted by alphabetic code
ALPHABETIC_CODESreadonly AlphabeticCode[]
NUMERIC_CODESreadonly NumericCode[]
COUNTRY_CODESreadonly CountryCode[] (ISO 3166-1 alpha-2 union)
PUBLISHED_AT"YYYY-MM-DD" date of the embedded SIX Group revision

Types

interface Currency {
    readonly alphabeticCode: AlphabeticCode; // e.g. "USD"
    readonly numericCode: NumericCode; // e.g. "840"
    readonly minorUnits: number | null; // null for metals and N.A. codes
    readonly name: string;
    readonly countryCodes: readonly CountryCode[];
    readonly isFund: boolean;
    readonly kind: "fiat" | "fund" | "metal" | "special";
}

type CurrencyKind = "fiat" | "fund" | "metal" | "special";

kind classifies each entry:

  • fiat — sovereign or supranational general-tender currency (USD, EUR, GBP, XAF, XOF, XCD, XCG, XPF, …).
  • fund — accounting/investment unit (CHE, CHW, BOV, CLF, COU, MXV, USN, UYI, UYW).
  • metal — precious metal commodity code (XAU, XAG, XPT, XPD).
  • special — reserved codes: bond units (XBA–XBD), IMF SDR (XDR), ADB unit (XUA), Sucre (XSU), Arab Accounting Dinar (XAD), testing (XTS), no-currency sentinel (XXX).

Data provenance

Data is generated from the two official SIX Group XML lists:

  • list-one.xml — active currencies
  • list-three.xml — historic / withdrawn currencies

Both are downloaded into data/ by bun run fetch-data. bun run generate then re-parses the XML, normalises entity rows to ISO 3166-1 alpha-2 country codes (via the mapping in scripts/iso-3166.ts), and emits the three generated source files in src/:

  • src/codes.ts
  • src/data.ts
  • src/historic-data.ts

The generator throws on any entity name it does not know how to map, so a new or renamed country in the upstream list fails the build rather than silently dropping an association.

Releasing a new revision

  1. bun run fetch-data
  2. bun run generate
  3. Review git diff src/
  4. bun run test
  5. Publish per the repository README.

License

MIT

版本与发布记录

@financica/iso4217 共发布 2 个版本,首次发布于 2026/08/03;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.2.0latest2026/08/182 个月前
0.1.02026/08/033 个月前

关于 @financica/iso4217 的常见问题

@financica/iso4217 是什么?

ISO 4217 currency codes, numeric codes, minor units, and entities — fully typed, zero dependencies, O(1) lookups.

如何安装 @financica/iso4217?

在项目目录运行 npm install @financica/iso4217,或使用 pnpm add / yarn add / bun add。

@financica/iso4217 支持 TypeScript 吗?

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

@financica/iso4217 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@financica/iso4217 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@financica/iso4217 可以商用吗?

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

@financica/iso4217 的下载量有多少?

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

@financica/iso4217 还在维护吗?

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