Code Explainers

Intermediate code explainers

typescript
import slugify from "slugify";
 
interface SlugRepository {
  exists(slug: string): Promise<boolean>;

Generating unique URL slugs from titles

slugs normalization uniqueness
Intermediate 6 steps
rust
pub fn levenshtein(a: &str, b: &str) -> usize {
    let a: Vec<char> = a.chars().collect();
    let b: Vec<char> = b.chars().collect();
 

Levenshtein distance with two rows in Rust

dynamic-programming edit-distance space-optimization
Intermediate 6 steps
java
@Configuration
public class UserImportJobConfig {
 
    @Bean

How a Spring Batch CSV import job is wired

batch processing chunk oriented fault tolerance
Intermediate 10 steps
ruby
module FixedWidthParser
  FIELDS = [
    { name: :record_type, range: 0...2 },
    { name: :account_id,  range: 2...12, type: :integer },

Parsing fixed-width records in Ruby

parsing data-driven-design type-coercion
Intermediate 5 steps
python
from django.core.paginator import Paginator
from django.shortcuts import render
 
from .models import Product

Filtering and paginating a Django product list

queryset pagination filtering
Intermediate 7 steps
go
package handlers
 
import (
	"encoding/json"

Parsing search query params in a Go handler

http-handlers input-validation defaults
Intermediate 7 steps
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
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
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