Validation utilities for currency codes, unit identifiers, timezone names, and other internationalization identifiers according to ECMA-402 standards. These functions ensure data integrity and compliance with international standards.
Validates currency codes according to ISO 4217 format (3 uppercase letters).
/**
* Validates currency codes according to ISO 4217 format
* @param currency - Currency code to validate (must be 3 uppercase letters)
* @returns true if currency code is well-formed, false otherwise
*/
function IsWellFormedCurrencyCode(currency: string): boolean;Usage Examples:
import { IsWellFormedCurrencyCode } from "@formatjs/ecma402-abstract";
// Valid currency codes
console.log(IsWellFormedCurrencyCode('USD')); // true
console.log(IsWellFormedCurrencyCode('EUR')); // true
console.log(IsWellFormedCurrencyCode('JPY')); // true
// Invalid currency codes
console.log(IsWellFormedCurrencyCode('usd')); // false (lowercase)
console.log(IsWellFormedCurrencyCode('US')); // false (too short)
console.log(IsWellFormedCurrencyCode('DOLLAR')); // false (too long)
console.log(IsWellFormedCurrencyCode('123')); // false (numbers)Validates unit identifiers including compound units (per-unit format) according to ECMA-402.
/**
* Validates unit identifiers including compound units
* @param unit - Unit identifier to validate
* @returns true if unit identifier is well-formed, false otherwise
*/
function IsWellFormedUnitIdentifier(unit: string): boolean;Usage Examples:
import { IsWellFormedUnitIdentifier } from "@formatjs/ecma402-abstract";
// Simple units
console.log(IsWellFormedUnitIdentifier('meter')); // true
console.log(IsWellFormedUnitIdentifier('kilogram')); // true
console.log(IsWellFormedUnitIdentifier('second')); // true
// Compound units
console.log(IsWellFormedUnitIdentifier('meter-per-second')); // true
console.log(IsWellFormedUnitIdentifier('kilogram-per-meter-squared')); // true
// Invalid units
console.log(IsWellFormedUnitIdentifier('invalid-unit')); // false
console.log(IsWellFormedUnitIdentifier('meter-')); // false
console.log(IsWellFormedUnitIdentifier('-per-second')); // falseValidates sanctioned simple unit identifiers against a predefined list of approved units.
/**
* Validates sanctioned simple unit identifiers
* @param unitIdentifier - Unit identifier to validate against sanctioned list
* @returns true if unit is sanctioned, false otherwise
*/
function IsSanctionedSimpleUnitIdentifier(unitIdentifier: string): boolean;
/**
* Array of sanctioned unit identifiers with namespaces
*/
const SANCTIONED_UNITS: readonly string[];
/**
* Array of simple unit names without namespaces
*/
const SIMPLE_UNITS: readonly string[];
/**
* Removes namespace from unit identifier
* @param unit - Unit identifier with potential namespace
* @returns Unit identifier without namespace
*/
function removeUnitNamespace(unit: string): string;Usage Examples:
import {
IsSanctionedSimpleUnitIdentifier,
SANCTIONED_UNITS,
removeUnitNamespace
} from "@formatjs/ecma402-abstract";
// Check sanctioned units
console.log(IsSanctionedSimpleUnitIdentifier('degree')); // true
console.log(IsSanctionedSimpleUnitIdentifier('acre')); // true
console.log(IsSanctionedSimpleUnitIdentifier('bit')); // true
// Invalid/non-sanctioned units
console.log(IsSanctionedSimpleUnitIdentifier('custom-unit')); // false
// View sanctioned units
console.log(SANCTIONED_UNITS.slice(0, 5));
// ['angle-degree', 'area-acre', 'area-hectare', 'concentr-karat', 'concentr-milligram-ofglucose-per-deciliter']
// Remove namespace
console.log(removeUnitNamespace('angle-degree')); // 'degree'
console.log(removeUnitNamespace('area-acre')); // 'acre'Validates timezone names against IANA timezone database data.
/**
* Validates timezone names against IANA timezone database
* @param tz - Timezone identifier to validate
* @param options - Timezone data and links for validation
* @returns true if timezone name is valid, false otherwise
*/
function IsValidTimeZoneName(
tz: string,
options: {
zoneNamesFromData: readonly string[];
uppercaseLinks: Record<string, string>;
}
): boolean;Usage Examples:
import { IsValidTimeZoneName } from "@formatjs/ecma402-abstract";
// Example timezone data (in practice, this comes from IANA data)
const timezoneData = {
zoneNamesFromData: [
'America/New_York',
'Europe/London',
'Asia/Tokyo',
'Australia/Sydney'
],
uppercaseLinks: {
'EST': 'America/New_York',
'UTC': 'UTC',
'GMT': 'UTC'
}
};
// Valid timezone names
console.log(IsValidTimeZoneName('America/New_York', timezoneData)); // true
console.log(IsValidTimeZoneName('Europe/London', timezoneData)); // true
console.log(IsValidTimeZoneName('UTC', timezoneData)); // true
// Invalid timezone names
console.log(IsValidTimeZoneName('Invalid/Timezone', timezoneData)); // false
console.log(IsValidTimeZoneName('America/NonExistent', timezoneData)); // falseParses patterns with placeholders into structured type/value pairs.
/**
* Parses patterns with placeholders into type/value pairs
* @param pattern - Pattern string with placeholders (e.g., "{0} of {1}")
* @returns Array of pattern parts with type and value information
*/
function PartitionPattern<T extends string>(
pattern: string
): Array<{
type: T;
value: string | undefined;
}>;Usage Examples:
import { PartitionPattern } from "@formatjs/ecma402-abstract";
// Simple pattern
const parts1 = PartitionPattern<'literal' | 'element'>('Hello {0}!');
console.log(parts1);
// [
// { type: 'literal', value: 'Hello ' },
// { type: 'element', value: '0' },
// { type: 'literal', value: '!' }
// ]
// Complex pattern
const parts2 = PartitionPattern<'literal' | 'element'>('{0} of {1} items');
console.log(parts2);
// [
// { type: 'element', value: '0' },
// { type: 'literal', value: ' of ' },
// { type: 'element', value: '1' },
// { type: 'literal', value: ' items' }
// ]
// No placeholders
const parts3 = PartitionPattern<'literal'>('Just text');
console.log(parts3);
// [{ type: 'literal', value: 'Just text' }]