235 lines
7.1 KiB
TypeScript
235 lines
7.1 KiB
TypeScript
export type NorthAmericaCountry = 'US' | 'CA';
|
|
|
|
export interface ResolveIsinInput {
|
|
isin?: string | null;
|
|
cusip?: string | null;
|
|
country?: string | null;
|
|
}
|
|
|
|
export type ResolveIsinRejectionReason =
|
|
| 'missing_identifiers'
|
|
| 'invalid_isin_and_no_valid_cusip'
|
|
| 'invalid_cusip'
|
|
| 'unsupported_country_for_cusip';
|
|
|
|
export type ResolveIsinResult =
|
|
| { isin: string; source: 'isin' | 'cusip_derived' }
|
|
| { isin: null; reason: ResolveIsinRejectionReason };
|
|
|
|
const CODE_0 = 48;
|
|
const CODE_9 = 57;
|
|
const CODE_A = 65;
|
|
const CODE_Z = 90;
|
|
const CODE_STAR = 42;
|
|
const CODE_AT = 64;
|
|
const CODE_HASH = 35;
|
|
|
|
function normalizeCode(value: string | null | undefined): string | null {
|
|
if (typeof value !== 'string') {
|
|
return null;
|
|
}
|
|
const normalized = value.trim().toUpperCase();
|
|
return normalized.length === 0 ? null : normalized;
|
|
}
|
|
|
|
function isAsciiDigit(code: number): boolean {
|
|
return code >= CODE_0 && code <= CODE_9;
|
|
}
|
|
|
|
function isAsciiUpper(code: number): boolean {
|
|
return code >= CODE_A && code <= CODE_Z;
|
|
}
|
|
|
|
function isAsciiAlnum(code: number): boolean {
|
|
return isAsciiDigit(code) || isAsciiUpper(code);
|
|
}
|
|
|
|
function isNorthAmericaCountry(value: string | null): value is NorthAmericaCountry {
|
|
return value === 'US' || value === 'CA';
|
|
}
|
|
|
|
function luhnStep(digit: number, doubleDigit: boolean): number {
|
|
if (!doubleDigit) {
|
|
return digit;
|
|
}
|
|
const doubled = digit * 2;
|
|
return doubled > 9 ? doubled - 9 : doubled;
|
|
}
|
|
|
|
/**
|
|
* Computes the ISO 6166 Luhn sum scanning right-to-left from `endIndex` down to 0.
|
|
* Letters A=10..Z=35 expand to two decimal digits (units first, then tens when scanning right-to-left).
|
|
*/
|
|
function computeIsinLuhnSum(code: string, endIndex: number, startDouble: boolean): number {
|
|
let sum = 0;
|
|
let doubleNext = startDouble;
|
|
for (let i = endIndex; i >= 0; i--) {
|
|
const ch = code.charCodeAt(i);
|
|
if (ch >= CODE_0 && ch <= CODE_9) {
|
|
sum += luhnStep(ch - CODE_0, doubleNext);
|
|
doubleNext = !doubleNext;
|
|
} else {
|
|
const val = ch - 55; // 'A' (65) -> 10 .. 'Z' (90) -> 35
|
|
const units = val % 10;
|
|
const tens = (val - units) / 10;
|
|
sum += luhnStep(units, doubleNext);
|
|
doubleNext = !doubleNext;
|
|
sum += luhnStep(tens, doubleNext);
|
|
doubleNext = !doubleNext;
|
|
}
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
function computeIsinCheckDigit(base11: string): number {
|
|
const sum = computeIsinLuhnSum(base11, 10, true);
|
|
return (10 - (sum % 10)) % 10;
|
|
}
|
|
|
|
/** Validates an ISO 6166 ISIN (12 chars, `[A-Z]{2}[A-Z0-9]{9}[0-9]`, Luhn check digit). */
|
|
export function isValidIsin(isin: string | null | undefined): boolean {
|
|
const normalized = normalizeCode(isin);
|
|
if (normalized === null || normalized.length !== 12) {
|
|
return false;
|
|
}
|
|
if (!isAsciiUpper(normalized.charCodeAt(0)) || !isAsciiUpper(normalized.charCodeAt(1))) {
|
|
return false;
|
|
}
|
|
for (let i = 2; i < 11; i++) {
|
|
if (!isAsciiAlnum(normalized.charCodeAt(i))) {
|
|
return false;
|
|
}
|
|
}
|
|
if (!isAsciiDigit(normalized.charCodeAt(11))) {
|
|
return false;
|
|
}
|
|
return computeIsinLuhnSum(normalized, 11, false) % 10 === 0;
|
|
}
|
|
|
|
/** Validates an ANSI X9.6 CUSIP (9 chars, mod-10 check digit including `*`, `@`, `#` = 36..38). */
|
|
export function isValidCusip(cusip: string | null | undefined): boolean {
|
|
const normalized = normalizeCode(cusip);
|
|
if (normalized === null || normalized.length !== 9) {
|
|
return false;
|
|
}
|
|
const checkCode = normalized.charCodeAt(8);
|
|
if (!isAsciiDigit(checkCode)) {
|
|
return false;
|
|
}
|
|
let sum = 0;
|
|
for (let i = 0; i < 8; i++) {
|
|
const ch = normalized.charCodeAt(i);
|
|
let val: number;
|
|
if (ch >= CODE_0 && ch <= CODE_9) {
|
|
val = ch - CODE_0;
|
|
} else if (ch >= CODE_A && ch <= CODE_Z) {
|
|
val = ch - 55;
|
|
} else if (ch === CODE_STAR) {
|
|
val = 36;
|
|
} else if (ch === CODE_AT) {
|
|
val = 37;
|
|
} else if (ch === CODE_HASH) {
|
|
val = 38;
|
|
} else {
|
|
return false;
|
|
}
|
|
if ((i & 1) === 1) {
|
|
val *= 2;
|
|
}
|
|
sum += Math.floor(val / 10) + (val % 10);
|
|
}
|
|
const expected = (10 - (sum % 10)) % 10;
|
|
return checkCode - CODE_0 === expected;
|
|
}
|
|
|
|
/**
|
|
* Derives an ISO 6166 ISIN from a valid 9-character US or CA CUSIP.
|
|
* Returns `null` if the CUSIP is invalid, contains ISO-6166-disallowed characters (`*`, `@`, `#`),
|
|
* or if the country is not `'US'` or `'CA'`.
|
|
*/
|
|
export function cusipToIsin(
|
|
cusip: string | null | undefined,
|
|
country: NorthAmericaCountry
|
|
): string | null {
|
|
const normalizedCountry = normalizeCode(country);
|
|
if (!isNorthAmericaCountry(normalizedCountry)) {
|
|
return null;
|
|
}
|
|
const normalizedCusip = normalizeCode(cusip);
|
|
if (normalizedCusip === null || !isValidCusip(normalizedCusip)) {
|
|
return null;
|
|
}
|
|
// ISO 6166 forbids `*`, `@`, `#` in the 9-character NSIN portion.
|
|
for (let i = 0; i < 9; i++) {
|
|
if (!isAsciiAlnum(normalizedCusip.charCodeAt(i))) {
|
|
return null;
|
|
}
|
|
}
|
|
const base11 = normalizedCountry + normalizedCusip;
|
|
return base11 + String(computeIsinCheckDigit(base11));
|
|
}
|
|
|
|
/**
|
|
* Resolves an ISIN without throwing on malformed input:
|
|
* 1. Returns the ISIN directly when ISO 6166 Luhn-valid.
|
|
* 2. Otherwise derives an ISIN from an explicit valid CUSIP (using `country` or a US/CA ISIN prefix).
|
|
* 3. Otherwise attempts recovery from the 9-char CUSIP embedded in a 12-char US/CA ISIN.
|
|
*/
|
|
export function resolveIsin(input: ResolveIsinInput | null | undefined): ResolveIsinResult {
|
|
if (input === null || input === undefined || typeof input !== 'object') {
|
|
return { isin: null, reason: 'missing_identifiers' };
|
|
}
|
|
|
|
const isin = normalizeCode(input.isin);
|
|
const cusip = normalizeCode(input.cusip);
|
|
const country = normalizeCode(input.country);
|
|
|
|
if (isin === null && cusip === null) {
|
|
return { isin: null, reason: 'missing_identifiers' };
|
|
}
|
|
|
|
if (isin !== null && isValidIsin(isin)) {
|
|
return { isin, source: 'isin' };
|
|
}
|
|
|
|
const isinPrefix = isin !== null && isin.length >= 2 ? isin.slice(0, 2) : null;
|
|
if (country !== null && isNorthAmericaCountry(isinPrefix) && country !== isinPrefix) {
|
|
return { isin: null, reason: 'invalid_isin_and_no_valid_cusip' };
|
|
}
|
|
|
|
if (cusip !== null) {
|
|
if (isValidCusip(cusip)) {
|
|
if (country !== null) {
|
|
if (!isNorthAmericaCountry(country)) {
|
|
return { isin: null, reason: 'unsupported_country_for_cusip' };
|
|
}
|
|
const derived = cusipToIsin(cusip, country);
|
|
if (derived !== null) {
|
|
return { isin: derived, source: 'cusip_derived' };
|
|
}
|
|
} else {
|
|
if (!isNorthAmericaCountry(isinPrefix)) {
|
|
return { isin: null, reason: 'unsupported_country_for_cusip' };
|
|
}
|
|
const derived = cusipToIsin(cusip, isinPrefix);
|
|
if (derived !== null) {
|
|
return { isin: derived, source: 'cusip_derived' };
|
|
}
|
|
}
|
|
} else if (isin === null) {
|
|
return { isin: null, reason: 'invalid_cusip' };
|
|
}
|
|
}
|
|
|
|
if (isin !== null && isin.length === 12 && isNorthAmericaCountry(isinPrefix)) {
|
|
const derived = cusipToIsin(isin.slice(2, 11), isinPrefix);
|
|
if (derived !== null) {
|
|
return { isin: derived, source: 'cusip_derived' };
|
|
}
|
|
}
|
|
|
|
if (isin !== null) {
|
|
return { isin: null, reason: 'invalid_isin_and_no_valid_cusip' };
|
|
}
|
|
return { isin: null, reason: 'invalid_cusip' };
|
|
}
|