Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Use Number() + isNaN to validate a whole numeric string
When the entire string must be a valid number, use Number() and an explicit Number.isNaN check rather than parseInt, which silently truncates at the first invalid char.
Bad example
| 1 | const parsed = Number.parseInt(value, 10); |
| 2 | const next = Number.isFinite(parsed) ? parsed + 1 : 1; |
| 3 | // value = '32_1' -> parseInt -> 32 (silently truncated, looks valid) |
Explanation (EN)
parseInt stops at the first non-numeric character, so '32_1' becomes 32 and passes Number.isFinite. Malformed input is silently accepted instead of being detected.
Objašnjenje (HR)
parseInt staje na prvom ne-numerickom znaku, pa '32_1' postaje 32 i prolazi Number.isFinite. Neispravan ulaz se tiho prihvaca umjesto da se otkrije.
Good example
| 1 | const parsed = value === null ? 0 : Number(value); |
| 2 | const next = Number.isNaN(parsed) ? 1 : parsed + 1; |
| 3 | // value = '32_1' -> Number -> NaN -> handled explicitly |
Explanation (EN)
Number() returns NaN for any non-fully-numeric string, and Number.isNaN makes the invalid-input branch explicit, so malformed values are handled deliberately.
Objašnjenje (HR)
Number() vraca NaN za svaki niz koji nije u potpunosti numericki, a Number.isNaN cini granu za neispravan ulaz eksplicitnom, pa se neispravne vrijednosti namjerno obraduju.
Notes (EN)
Use parseInt deliberately only when you actually want to read a leading integer and ignore a known suffix. If you reach for Number.isFinite, document which non-finite case you're guarding against.
Bilješke (HR)
parseInt koristi namjerno samo kad stvarno zelis procitati pocetni cijeli broj i ignorirati poznati sufiks. Ako posegnes za Number.isFinite, dokumentiraj koji ne-konacni slucaj cuvas.
Exceptions / Tradeoffs (EN)
parseInt is appropriate when parsing a number with an intentional unit/suffix (e.g. '16px').
Iznimke / Tradeoffi (HR)
parseInt je prikladan kad parsiras broj s namjernom jedinicom/sufiksom (npr. '16px').