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 native Set methods for set arithmetic instead of manual filter loops
Prefer Set.prototype.difference/intersection/union over hand-rolled filter + has() loops when computing set relationships.
Bad example
| 1 | const oldSet = new Set(current); |
| 2 | const newSet = new Set(next); |
| 3 | const toAdd = next.filter((x) => !oldSet.has(x)); |
| 4 | const toRemove = current.filter((x) => !newSet.has(x)); |
Explanation (EN)
Manual filter/has loops obscure the intent (set difference) and allocate extra intermediate arrays from the source lists.
Objašnjenje (HR)
Rucne filter/has petlje zamagljuju namjeru (razlika skupova) i alociraju dodatne medjurezultatske nizove iz izvornih lista.
Good example
| 1 | const oldSet = new Set(current); |
| 2 | const newSet = new Set(next); |
| 3 | const toAdd = newSet.difference(oldSet); |
| 4 | const toRemove = oldSet.difference(newSet); |
Explanation (EN)
Set.prototype.difference states the intent directly and returns a Set, making the add/remove computation self-documenting.
Objašnjenje (HR)
Set.prototype.difference izravno iskazuje namjeru i vraca Set, cineci izracun dodavanja/uklanjanja samodokumentirajucim.
Notes (EN)
Ref: developer.mozilla.org Set.prototype.difference.
Bilješke (HR)
Ref: developer.mozilla.org Set.prototype.difference.
Exceptions / Tradeoffs (EN)
Confirm the target Node/runtime version supports Set methods (Node 22+); fall back to filter loops on older runtimes.
Iznimke / Tradeoffi (HR)
Provjerite podrzava li ciljana Node/runtime verzija Set metode (Node 22+); na starijim runtimeima koristite filter petlje.