feat: integrate token point usage v2
This commit is contained in:
1 parent
9fed0cc7c4
commit
83ad10d95e
14 files changed
+907
-201
No files matched your search
@@ -0,0 +1,24 @@
|
||||
import type { WorksTokenPointBalance } from './works-square';
|
||||
|
||||
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.can_manage_membership && typeof balance.shared_available === 'boolean') {
|
||||
return !balance.shared_available;
|
||||
}
|
||||
const remaining = formatWorksTokenPointValue(balance.total_remaining);
|
||||
return remaining !== null && /^0(?:\.0+)?$/u.test(remaining);
|
||||
}
|
||||
Reference in new issue
Block a user