25 lines
950 B
TypeScript
25 lines
950 B
TypeScript
import type { WorksTokenPointBalance } from './works-billing';
|
|
|
|
const TOKEN_POINT_VALUE_PATTERN = /^(0|[1-9]\d*)(?:\.(\d{1,2}))?$/u;
|
|
|
|
export function formatWorksTokenPointValue(value: string | null | undefined): string | null {
|
|
if (typeof value !== 'string') return null;
|
|
const match = TOKEN_POINT_VALUE_PATTERN.exec(value.trim());
|
|
if (!match) return null;
|
|
|
|
const whole = match[1].replace(/\B(?=(\d{3})+(?!\d))/gu, ',');
|
|
const fraction = (match[2] ?? '').replace(/0+$/u, '');
|
|
return fraction ? `${whole}.${fraction}` : whole;
|
|
}
|
|
|
|
export function isWorksTokenPointBalanceExhausted(
|
|
balance: WorksTokenPointBalance | null | undefined,
|
|
): boolean {
|
|
if (!balance) return false;
|
|
if (balance.family_shared && typeof balance.shared_available === 'boolean') {
|
|
return !balance.shared_available;
|
|
}
|
|
const remaining = formatWorksTokenPointValue(balance.total_remaining);
|
|
return remaining !== null && /^0(?:\.0+)?$/u.test(remaining);
|
|
}
|