Code Explainers

Browse the library

javascript
function useHashState(key, defaultValue) {
  const parse = () => {
    const params = new URLSearchParams(window.location.hash.slice(1));
    return params.has(key) ? params.get(key) : defaultValue;

A React hook backed by the URL hash

custom-hooks url-state event-listeners
Intermediate 6 steps
typescript
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { createClient, RedisClientType } from 'redis';
 

Provide a Redis client with a NestJS factory

dependency-injection async-factory redis
Intermediate 6 steps
rust
pub fn collapse_whitespace(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let mut in_whitespace = false;
 

Collapsing runs of whitespace in Rust

string-processing state-machine iteration
Beginner 5 steps
python
from werkzeug.routing import BaseConverter, ValidationError
from flask import Flask, jsonify, abort
 
 

Custom URL converters in Flask

url-routing validation converters
Intermediate 6 steps
java
public final class BomAwareReader {
 
    private static final char UTF8_BOM = '\uFEFF';
 

Skipping the UTF-8 byte-order mark in Java

file-io encoding byte-order-mark
Intermediate 6 steps
php
<?php
 
namespace App\Http\Support;
 

Adding RFC 5988 Link headers in Laravel

pagination http-headers rest-api
Intermediate 7 steps
ruby
module Authenticatable
  extend ActiveSupport::Concern
 
  included do

JWT authentication as a Rails concern

authentication jwt concerns
Intermediate 6 steps
typescript
type RGB = { r: number; g: number; b: number };
 
type WCAGLevel = "AA" | "AAA";
type TextSize = "normal" | "large";

Checking WCAG color contrast in TypeScript

accessibility color-math type-safety
Intermediate 10 steps
rust
use axum::{
    http::{header, StatusCode},
    response::{IntoResponse, Response},
    Json,

Turning errors into RFC 7807 responses in Axum

error-handling enums trait-implementation
Intermediate 7 steps
go
package middleware
 
import (
	"bytes"

Idempotent requests in Gin with Redis

idempotency caching middleware
Advanced 10 steps
javascript
function hexToRgb(hex) {
  const normalized = hex.replace(/^#/, '');
  const full = normalized.length === 3
    ? normalized.split('').map((c) => c + c).join('')

Building a two-way color picker in JS

bitwise dom-events data-conversion
Intermediate 9 steps
python
from typing import List, Tuple
 
 
def merge_intervals(intervals: List[Tuple[int, int]]) -> List[Tuple[int, int]]:

How interval merging works

sorting greedy intervals
Intermediate 6 steps
java
public final class HmacJwt {
 
    private static final ObjectMapper MAPPER = new ObjectMapper();
    private static final Base64.Encoder ENCODER = Base64.getUrlEncoder().withoutPadding();

How HMAC-signed JWTs are created and verified

jwt hmac cryptography
Intermediate 9 steps
php
<?php
 
declare(strict_types=1);
 

Streaming a fixed-width file into typed objects

generators value-objects file-parsing
Intermediate 8 steps
ruby
class Pipeline
  def initialize
    @middlewares = []
  end

Building a middleware pipeline in Ruby

closures middleware composition
Advanced 8 steps
typescript
import { Pipe, PipeTransform, Inject, LOCALE_ID } from '@angular/core';
 
interface MoneyOptions {
  currency?: string;

Building a cached money pipe in Angular

pipes memoization internationalization
Intermediate 8 steps
rust
use serde::{Deserialize, Serialize};
 
#[derive(Debug, Serialize, Deserialize)]
pub struct UserProfile {

Trimming JSON output with serde attributes

serialization json attributes
Intermediate 8 steps
go
package middleware
 
import (
	"context"

A deadline-enforcing HTTP middleware in Go

middleware context goroutines
Advanced 7 steps