refactor(pure): remove duplicate regex and dead empty-string guard
This commit is contained in:
+1
-4
@@ -11,15 +11,12 @@ export function parseTarget(input) {
|
|||||||
if (typeof input !== 'string') return null;
|
if (typeof input !== 'string') return null;
|
||||||
|
|
||||||
const cleaned = input.replace(/,/g, '').trim().toLowerCase();
|
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])?$/);
|
const match = cleaned.match(/^(\d+(?:\.\d+)?)([kmbt])?$/);
|
||||||
if (!match) return null;
|
if (!match) return null;
|
||||||
|
|
||||||
const num = parseFloat(match[1]);
|
const num = parseFloat(match[1]);
|
||||||
|
// regex permits '0'; reject it
|
||||||
if (!Number.isFinite(num) || num <= 0) return null;
|
if (!Number.isFinite(num) || num <= 0) return null;
|
||||||
|
|
||||||
const suffix = match[2];
|
const suffix = match[2];
|
||||||
|
|||||||
@@ -30,4 +30,5 @@ test('parseTarget: rejects invalid input', () => {
|
|||||||
assert.equal(parseTarget(undefined), null);
|
assert.equal(parseTarget(undefined), null);
|
||||||
assert.equal(parseTarget('25M.5'), null);
|
assert.equal(parseTarget('25M.5'), null);
|
||||||
assert.equal(parseTarget('1.5.5M'), null);
|
assert.equal(parseTarget('1.5.5M'), null);
|
||||||
|
assert.equal(parseTarget('0'), null);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user