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).
Handle empty base explicitly when resolving URLs
When building an absolute URL with new URL(path, base), handle a missing or empty base explicitly with a ternary or a real fallback origin; do not paper over it with a trailing-slash hack, because new URL('/x', '/') throws.
Bad example
| 1 | const toAbsoluteUrl = (baseUrl, pathOrUrl) => |
| 2 | new URL(pathOrUrl, `${baseUrl}/`).toString(); |
| 3 | // baseUrl='' -> new URL('/foo', '/') throws |
Explanation (EN)
Appending a trailing slash to the base does not make an empty base valid: new URL(path, '/') still throws, so the helper crashes on empty input instead of degrading.
Objašnjenje (HR)
Dodavanje kose crte na bazu ne cini praznu bazu valjanom: new URL(path, '/') i dalje baca gresku, pa funkcija puca na praznom ulazu umjesto da se gracioznо degradira.
Good example
| 1 | const toAbsoluteUrl = (baseUrl, pathOrUrl) => |
| 2 | baseUrl ? new URL(pathOrUrl, baseUrl).href : pathOrUrl; |
Explanation (EN)
An explicit branch returns a sensible value when no base is available instead of relying on URL to accept an invalid base.
Objašnjenje (HR)
Eksplicitna grana vraca smislenu vrijednost kad baza ne postoji umjesto da se oslanja na to da URL prihvati nevaljanu bazu.