javascript
function escapeCell(value) {
  if (value === null || value === undefined) return '';
  const str = String(value);
  if (/[",\n\r]/.test(str)) {
    return '"' + str.replace(/"/g, '""') + '"';
  }

Stream a Large CSV Export in Express with Backpressure and an Async Row Generator

express streaming csv
by codesnips 3 tabs
javascript
const { z } = require('zod');

const ConfigSchema = z.object({
  env: z.enum(['development', 'test', 'production']).default('development'),
  port: z.coerce.number().int().positive().default(3000),
  database: z.object({

Cached Config Singleton in Node.js: Load Once, Share Everywhere

nodejs configuration singleton
by codesnips 3 tabs
javascript
import { useState, useRef, useEffect, useCallback } from 'react';

export function useOnScreen(options = {}) {
  const [node, setNode] = useState(null);
  const [isIntersecting, setIsIntersecting] = useState(false);

Infinite Scroll in React with an IntersectionObserver useOnScreen Hook

react hooks intersection-observer
by codesnips 3 tabs
go
package httpretry

import (
	"math/rand"
	"time"
)

Exponential Backoff With Full Jitter for Flaky HTTP Calls in Go

go http retry
by codesnips 3 tabs
python
from alembic import op
import sqlalchemy as sa

revision = "20240612_add_status"
down_revision = "20240515_create_orders"
branch_labels = None

Zero-Downtime NOT NULL Column Backfill with Alembic and SQLAlchemy

alembic sqlalchemy migrations
by codesnips 2 tabs
typescript
import { EventEmitter } from "events";

export interface Job<T> {
  id: string;
  payload: T;
  attempts: number;

Typed In-Memory Job Queue With a Concurrency-Limited Worker Pool

typescript job-queue concurrency
by codesnips 3 tabs
java
package com.example.reference;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

Seed Reference Data at Boot with a Spring Boot ApplicationRunner

spring-boot seeding reference-data
by codesnips 4 tabs
javascript
'use strict';

function dedupeAsync(producer, keyFn) {
  const pending = new Map();
  const resolveKey = keyFn || ((...args) => JSON.stringify(args));

Deduplicate Concurrent Async Calls With a Promise Cache in Node.js

nodejs async promises
by codesnips 3 tabs
ruby
module Paginatable
  extend ActiveSupport::Concern

  Page = Struct.new(:records, :next_cursor, keyword_init: true)

  DEFAULT_LIMIT = 25

Cursor-Paginated Rails API with ETag and Conditional GET Caching

rails api http-caching
by codesnips 3 tabs
go
package httpx

import (
	"net/http"
	"strconv"
)

Enforce a Maximum Request Body Size in Go HTTP Handlers with MaxBytesReader

http middleware security
by codesnips 3 tabs
ruby
class Product < ApplicationRecord
  scope :by_status, ->(status) {
    status.present? ? where(status: status) : all
  }

  scope :in_category, ->(category_id) {

Composable Query Filters with Rails Scopes and a Filter Object

rails activerecord scopes
by codesnips 3 tabs
javascript
const express = require('express');
const AppError = require('./AppError');
const asyncHandler = require('./asyncHandler');
const errorMiddleware = require('./errorMiddleware');
const UserRepo = require('./UserRepo');

Centralized Express Error Handling with Typed AppError and Async Wrapper

express error-handling middleware
by codesnips 4 tabs