51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
/**
|
|
* @file lib/kis/account.ts
|
|
* @description KIS 계좌번호(8-2) 파싱/검증 유틸
|
|
*/
|
|
|
|
export interface KisAccountParts {
|
|
accountNo: string;
|
|
accountProductCode: string;
|
|
}
|
|
|
|
/**
|
|
* 입력된 계좌 문자열을 KIS 표준(8-2)으로 변환합니다.
|
|
* @param accountNoInput 계좌번호(예: 12345678-01 또는 1234567801)
|
|
* @param accountProductCodeInput 계좌상품코드(2자리, 선택)
|
|
* @returns 파싱 성공 시 accountNo/accountProductCode
|
|
* @see app/api/kis/domestic/balance/route.ts 잔고 조회 API에서 계좌 파싱에 사용합니다.
|
|
*/
|
|
export function parseKisAccountParts(
|
|
accountNoInput?: string | null,
|
|
accountProductCodeInput?: string | null,
|
|
): KisAccountParts | null {
|
|
const accountDigits = toDigits(accountNoInput);
|
|
const productDigits = toDigits(accountProductCodeInput);
|
|
|
|
if (accountDigits.length >= 10) {
|
|
return {
|
|
accountNo: accountDigits.slice(0, 8),
|
|
accountProductCode: accountDigits.slice(8, 10),
|
|
};
|
|
}
|
|
|
|
if (accountDigits.length === 8 && productDigits.length === 2) {
|
|
return {
|
|
accountNo: accountDigits,
|
|
accountProductCode: productDigits,
|
|
};
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 문자열에서 숫자만 추출합니다.
|
|
* @param value 원본 문자열
|
|
* @returns 숫자만 남긴 문자열
|
|
* @see lib/kis/account.ts parseKisAccountParts 입력 정규화 처리
|
|
*/
|
|
function toDigits(value?: string | null) {
|
|
return (value ?? "").replace(/\D/g, "");
|
|
}
|