feat(pure): parseTarget with comma and magnitude suffix support

This commit is contained in:
dev
2026-06-01 15:16:12 -05:00
parent 07ef131d65
commit e0540468c3
3 changed files with 62 additions and 1 deletions
+28
View File
@@ -0,0 +1,28 @@
const SUFFIXES = { k: 1e3, m: 1e6, b: 1e9, t: 1e12 };
export function parseTarget(input) {
if (input === null || input === undefined || input === '') return null;
if (typeof input === 'number') {
if (!Number.isFinite(input) || input <= 0) return null;
return Math.floor(input);
}
if (typeof input !== 'string') return null;
const cleaned = input.replace(/,/g, '').trim().toLowerCase();
if (cleaned === '') return null;
// Reject anything other than digits, optional decimal, and optional single trailing suffix letter
if (!/^\d+(\.\d+)?[kmbt]?$/.test(cleaned)) return null;
const match = cleaned.match(/^(\d+(?:\.\d+)?)([kmbt])?$/);
if (!match) return null;
const num = parseFloat(match[1]);
if (!Number.isFinite(num) || num <= 0) return null;
const suffix = match[2];
const multiplier = suffix ? SUFFIXES[suffix] : 1;
return Math.floor(num * multiplier);
}