Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Build lookup Maps with the Map constructor and entry tuples
Prefer `new Map(items.map(x => [x.key, x]))` over an imperative for-loop with repeated map.set calls to build a lookup Map.
Bad example
| 1 | const eventsMap = new Map<number, Event>(); |
| 2 | for (const event of events) { |
| 3 | eventsMap.set(event.id, event); |
| 4 | } |
| 5 | return eventsMap; |
Explanation (EN)
A tuple constructor can shorten a simple one-to-one lookup construction.
Objašnjenje (HR)
Konstruktor iz parova može skratiti jednostavnu izgradnju lookup mape.
Good example
| 1 | return new Map(events.map((event) => [event.id, event])); |
Explanation (EN)
The Map constructor accepts an iterable of [key, value] entry tuples, so mapping the array straight into the constructor expresses the intent in one declarative line with no intermediate mutation.
Objašnjenje (HR)
Map konstruktor prima iterabilan niz [kljuc, vrijednost] parova, pa mapiranje niza direktno u konstruktor izrazava namjeru u jednoj deklarativnoj liniji bez medjumutacije.
Notes (EN)
Construct a Map from entry tuples when that directly expresses a small straightforward lookup with the intended last-value-wins duplicate policy.
Exceptions / Tradeoffs (EN)
A loop avoids an intermediate array and can be clearer for filtering, custom collision handling or large collections. Do not call it a defect solely because it uses set.
Iznimke / Tradeoffi (HR)
Petlja izbjegava međuniz i može biti jasnija za filtriranje, posebnu obradu jednakih ključeva ili velike kolekcije. Sam poziv set nije nedostatak.