FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

125 snippets foundClear filters
TypeScriptArray restructuring

Split an array into bounded batches

TypeScript: Advance by a positive batch size and allow a shorter final chunk.

TypeScript✓ Tested
TypeScriptRegular expressions & URL encoding

Split text on several separator characters

TypeScript: A regex character class can recognize alternative simple delimiters.

TypeScript✓ Tested
TypeScriptGetting Started

Store a value in a named variable

TypeScript: Give a value a descriptive name and print it.

TypeScript✓ Tested
TypeScriptCollections & records

Sum typed values with reduce

Accumulate numeric array elements.

reduce✓ Tested
TypeScriptPromises & iteration protocols

Transform a resolved promise result

TypeScript: A then handler returns the value passed to the next promise in the chain.

TypeScript✓ Tested
TypeScriptText handling & Unicode

Transform each regex match with a callback

TypeScript: A replacement callback computes the replacement separately for each match.

TypeScript✓ Tested
TypeScriptText handling & Unicode

Trim and discard blank lines

TypeScript: Split a text block, trim each line, and keep nonempty values.

TypeScript✓ Tested
TypeScriptCollections & records

Type a dictionary with Record

Limit a lookup table to a known set of keys.

Record✓ Tested
TypeScriptTypes & functions

Type a function’s inputs and output

Catch mismatched arguments during compilation.

function✓ Tested
TypeScriptUtilities & async

Type an object update with Partial

Allow a caller to provide just the fields to change.

Partial✓ Tested
TypeScriptGetting Started

Update a running count

TypeScript: Change a variable after its initial value has been assigned.

TypeScript✓ Tested
TypeScriptSafe data handling

Update an object with a readonly input

Create a changed copy while retaining the original.

readonly✓ Tested