Code Explainers

Intermediate code explainers

go
package storage
 
import (
	"errors"

Preventing path traversal in Go

path-traversal input-validation security
Intermediate 9 steps
rust
use std::sync::Arc;
 
use axum::{
    extract::{FromRef, State},

Substate extraction with FromRef in Axum

state-management routing middleware
Intermediate 8 steps
python
from flask import Blueprint, render_template, request, redirect, url_for, flash, abort
from .models import User, AuditLog
from .extensions import db
from .decorators import admin_required

Nesting Flask Blueprints for an admin panel

blueprints route-guards pagination
Intermediate 8 steps
javascript
const progressBar = document.getElementById('reading-progress');
const article = document.querySelector('article');
 
function updateProgress() {

Building a scroll-driven reading progress bar

dom scroll-events throttling
Intermediate 9 steps
go
package logsample
 
import (
	"sync"

A per-key rate limiter for log sampling

rate-limiting concurrency time-windows
Intermediate 7 steps
rust
use std::sync::Arc;
use std::collections::HashMap;
use axum::{
    extract::State,

Hot-reloadable config with shared state in Axum

shared-state concurrency rwlock
Intermediate 7 steps
php
<?php
 
namespace App\Rules;
 

How a custom coupon rule validates in Laravel

validation eloquent business-rules
Intermediate 7 steps
python
from xml.etree import ElementTree as ET
from xml.dom import minidom
 
 

Converting nested dicts to XML in Python

recursion serialization xml
Intermediate 9 steps
javascript
function sparkline(values, { width = 200, height = 40, stroke = '#2563eb', fill = 'rgba(37,99,235,0.12)' } = {}) {
  if (!Array.isArray(values) || values.length < 2) {
    throw new RangeError('sparkline requires at least two data points');
  }

Rendering a sparkline as SVG

svg data-visualization coordinate-mapping
Intermediate 8 steps
typescript
import { Directive, ElementRef, HostListener, Input, OnDestroy, TemplateRef, ViewContainerRef } from '@angular/core';
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { TemplatePortal } from '@angular/cdk/portal';
 

Building a tooltip directive in Angular

directives cdk-overlay hover-interaction
Intermediate 7 steps
go
func (h *ReportHandler) ExportTransactions(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
 
	from, to, err := parseDateRange(r.URL.Query())

Streaming a CSV export in Go

streaming http csv
Intermediate 8 steps
java
public final class SensitiveDataMasker {
 
    private static final Pattern CARD_NUMBER = Pattern.compile("\\b(?:\\d[ -]*?){13,19}\\b");
    private static final Pattern EMAIL = Pattern.compile("[A-Za-z0-9._%+-]+@([A-Za-z0-9.-]+\\.[A-Za-z]{2,})");

Masking sensitive data with regex in Java

regex data-masking luhn-algorithm
Intermediate 8 steps
rust
use chrono::{DateTime, Duration, Utc};
 
#[derive(Debug, Clone)]
pub struct Sample {

Forward-filling a time series in Rust

time-series forward-fill btreemap
Intermediate 7 steps
ruby
class ApplicationController < ActionController::Base
  INACTIVITY_TIMEOUT = 30.minutes
 
  before_action :expire_stale_session

Idle session timeout in a Rails controller

authentication session-management before-action
Intermediate 6 steps
javascript
import { NextResponse } from 'next/server';
import { revalidatePath } from 'next/cache';
import crypto from 'node:crypto';
 

A GitHub webhook that revalidates Next.js docs

webhooks hmac-verification cache-revalidation
Intermediate 8 steps
java
public final class UserAgentParser {
 
    private static final Pattern OS_PATTERN = Pattern.compile(
            "(Windows NT [0-9.]+|Mac OS X [0-9_]+|Android [0-9.]+|iPhone OS [0-9_]+|Linux)");

Parsing user-agent strings with regex in Java

regex parsing pattern-matching
Intermediate 9 steps
ruby
module Dotenv
  LINE = /\A\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*\z/
 
  module_function

Parsing a .env file in Ruby

parsing regular-expressions environment-variables
Intermediate 8 steps
php
<?php
 
namespace App\Mail;
 

Building a queued digest email in Laravel

mailable queueing localization
Intermediate 6 steps