Tags

Cheat Sheet

Cheat Sheet

0 lessons
9 code snippets

cheat-sheet

Code Snippets

9 snippets
Code Snippet

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.

JavaScript
arrays
utility
cheat-sheet

522

13

Easy
Code Snippet

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.

TypeScript
ts-utility-types
ts-generics
ts-mapped-types
cheat-sheet

663

20

Easy
Code Snippet

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.

CSS
css-selectors
css-overflow
code-template
cheat-sheet

808

20

Easy
Code Snippet

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.

CSS
css-selectors
css-overflow
code-template
cheat-sheet

585

13

Medium
Code Snippet

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
py-list-comprehensions
py-comprehensions
code-template
cheat-sheet

451

11

Easy
Code Snippet

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
py-comprehensions
py-list-comprehensions
code-template
cheat-sheet

1.1k

15

Easy
Code Snippet

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.

Python
py-comprehensions
py-list-comprehensions
code-template
cheat-sheet

1k

20

Easy
Code Snippet

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.

Python
py-walrus-operator
code-template
cheat-sheet
py-standard-library

605

11

Medium
Code Snippet

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.

Python
py-collections
py-standard-library
code-template
cheat-sheet

451

10

Medium