Cheat Sheet
cheat-sheet
Code Snippets
Safely Read the Last Element
Reading `array[array.length - 1]` is the line every JavaScript developer writes a thousand times, and a chunk of those calls hide bugs on empty arrays or computed expressions. Modern `Array.prototype.at(-1)` makes the intent obvious and supports negative indexes natively. This snippet shows the modern form, the safe-default helper for empty inputs, and the typed-array story so you pick the right tool.
TypeScript Generic Utility Types Tour
TypeScript ships with a rich set of utility types (`Partial`, `Pick`, `Awaited`) and the language is expressive enough that you can build the rest yourself. This snippet tours the three most useful custom utilities (`DeepPartial`, `ValueOf`, and an `Awaited` recap), each with a runtime sentinel that proves the type lines up with the value. Use it as a one-page cheat sheet before reaching for individual deep dives in the rest of the catalog.
Truncate Text with Ellipsis
Single-line text truncation is one of the most common UI requirements: titles, table cells, breadcrumbs. Done wrong, the text wraps or overflows. Done right, it shrinks with a clean ellipsis. This snippet covers the three-line single-line recipe, an inline variant that needs `min-width: 0` inside flex parents, and a tooltip-friendly version that exposes the full text on hover.
Multi-line Text Truncation
Single-line truncation is solved with `text-overflow: ellipsis`, but truncating to N lines requires the WebKit line-clamp API. This snippet covers the standard three-line ellipsis using the modern `line-clamp` shorthand, the older `-webkit-line-clamp` form for broader support, and a JS-free fade-out variant for browsers without line-clamp.
Python List Comprehension Cheat Sheet
List comprehensions are Python's most distinctive feature: they pack filter, map, and flatten into a single expression. This cheat sheet covers the basic map-and-filter form, the nested form for cartesian products and matrix flattening, and the conditional-expression form that branches inside the output. Each pattern shows up many times per Python file in real codebases.
Python Dict Comprehension Cheat Sheet
Dict comprehensions build mappings without explicit loops, the same way list comprehensions build lists. They are the right tool for inverting a dict, projecting a list of tuples into a key-value map, and filtering an existing dict by predicate. This snippet covers the basic build form, the invert and merge patterns, and the filtering form for trimming an existing dict.
Python Set Comprehension Patterns
Set comprehensions are the underused sibling of list and dict comprehensions. They build deduplicated collections in one line and excel at unique-by-key extraction, set algebra, and quick membership filters. This snippet covers the basic uniqueness pattern, the unique-by-projection form for objects, and set algebra (intersection, difference) expressed as comprehensions.
Walrus Operator Patterns
The walrus operator `:=` (Python 3.8+) lets you assign and use a value in the same expression. It is the right tool for caching expensive expressions inside comprehensions, reading until a sentinel, and tightening the common 'compute, then check' pattern. This snippet covers the loop-with-sentinel use, the comprehension-cache use, and the regex-match conditional that is the most-cited textbook example.
ChainMap for Layered Configs
`collections.ChainMap` lets you stack multiple dicts and treat them as a single read-through view, with later dicts shadowing earlier ones. It is the right primitive for layered configs (defaults, environment, user overrides), nested scopes, and anywhere you would otherwise merge dicts repeatedly. This snippet covers the basic stacking, lookup-with-fallback semantics, and how new keys land in the first map by default.
