Dev Helpers

Developer journal

JavaScript

Patterns for transforming data, managing asynchronous work and writing browser code.

← All categories

JavaScript

Sum distances with a Euclidean norm

A diagonal is longer than either of its components. Math.hypot combines perpendicular distances without making the caller square each input.

Build an OS-independent path

Path conventions belong to the destination. Use an explicit path flavour when generating portable asset names.

Resolve ranked ties

A leaderboard needs an explicit tie policy. Ranking tied scores and sorting their display order are separate decisions.

Get the last day of a month

Day zero of the following month falls on the final day of the requested month. Date handles the year rollover for December.

Replace every occurrence

Replacing just the first delimiter leaves mixed formatting in an identifier. replaceAll makes the intended scope explicit.

Encode a Unicode JSON document

JSON text and transport bytes are separate representations. Encode the text as UTF-8 before measuring or sending a byte payload.

Iterate Unicode safely with for-of

String length counts UTF-16 units. A for-of loop advances by Unicode code point, which keeps a supplementary character together.

Normalize a compass bearing

JavaScript's remainder can be negative. Normalize a bearing into one full turn before choosing a compass label.

Round an invoice to a nickel

Cash rounding belongs at the payment boundary. Working in cents makes a five-cent increment easier to reason about.

Detect an overnight shift

A clock time after midnight may belong to yesterday's shift. Compare the interval endpoints before calculating its duration.

Apply locale-sensitive uppercasing

Capitalization depends on the language of the text. Pass the intended locale at the boundary where a display label is created.

Iterate indexed entries

Array indexes are useful when a value occurs twice. entries() keeps the position and the value together while the loop advances.

Strip combining accents

Removing combining marks can make a secondary search index more forgiving. Preserve the original spelling in display and storage.

Read repeated query values

Repeated URL keys carry multiple selections. Reading a single value can silently discard filters chosen by the visitor.

Chunk an array

Batching work by array slices gives the last batch permission to be shorter. Guard the requested size before using it as the loop increment.

Check every item against a rule

Use every when the question is whether any record violates a rule. It stops at the first failure and accepts an empty list.

Compute greatest common divisor

The Euclidean algorithm reduces a pair by taking successive remainders. Each step produces a smaller divisor until the remainder reaches zero.

Read an internationalized hostname

A URL parser exposes the canonical host representation of an internationalized name. Keep display text separate from comparison keys.

Await a resolved value

Awaiting an already fulfilled promise still yields control to the microtask queue. That matters when debugging the order of UI updates.

Curry a binary function

A curried function lets one call establish a policy and later calls supply the values. The returned function retains its first argument.

Flatten nested arrays

Choose the flattening depth from the data contract. Flattening another level can erase a meaningful grouping.

Remove duplicate values

Set preserves insertion order and collapses repeated primitive values. Objects still compare by reference.

Find saddle stitch blank pages

A folded sheet contributes four printed pages. Padding a booklet is a remainder calculation, with zero padding when the count already fits.

Validate finite numbers

A numeric field can contain NaN or infinity even after it has become a number. Number.isFinite checks the value without coercing a string.

Convert a number to another radix

A radix changes how an integer is written. The numeric value stays the same while its string representation changes.

Shadow a block-local binding

A nested lexical binding can hide an outer value without mutating it. Logging before and after the block reveals which value each reader sees.

Freeze a configuration

Freezing a settings object locks its own properties. A referenced nested array or object can still change.